Browse Source
Use AppBuilder to configure the Application and make sure that everything is set up in the correct order.pull/537/head
22 changed files with 195 additions and 110 deletions
@ -1,16 +1,18 @@ |
|||
using Avalonia; |
|||
|
|||
using Avalonia.Markup.Xaml; |
|||
|
|||
namespace ControlCatalog |
|||
{ |
|||
// Eventually we should move this into a PCL library so we can access
|
|||
// from mobile platforms
|
|||
//
|
|||
public class App : Application |
|||
{ |
|||
public App() |
|||
{ |
|||
RegisterServices(); |
|||
} |
|||
|
|||
public override void Initialize() |
|||
{ |
|||
AvaloniaXamlLoader.Load(this); |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,80 @@ |
|||
// Copyright (c) The Avalonia Project. All rights reserved.
|
|||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
|||
|
|||
using Avalonia.Input; |
|||
using System; |
|||
|
|||
namespace Avalonia.Controls |
|||
{ |
|||
public class AppBuilder |
|||
{ |
|||
public Application Instance { get; set; } |
|||
|
|||
public Action WindowingSubsystem { get; set; } |
|||
|
|||
public Action RenderingSubsystem { get; set; } |
|||
|
|||
public Action<AppBuilder> BeforeStartCallback { get; set; } |
|||
|
|||
public static AppBuilder Configure<TApp>() |
|||
where TApp : Application, new() |
|||
{ |
|||
return Configure(new TApp()); |
|||
} |
|||
|
|||
public static AppBuilder Configure(Application app) |
|||
{ |
|||
AvaloniaLocator.CurrentMutable.BindToSelf(app); |
|||
|
|||
return new AppBuilder() |
|||
{ |
|||
Instance = app, |
|||
}; |
|||
} |
|||
|
|||
public AppBuilder BeforeStarting(Action<AppBuilder> callback) |
|||
{ |
|||
BeforeStartCallback = callback; |
|||
return this; |
|||
} |
|||
|
|||
public void Start<TMainWindow>() |
|||
where TMainWindow : Window, new() |
|||
{ |
|||
Setup(); |
|||
BeforeStartCallback?.Invoke(this); |
|||
|
|||
var window = new TMainWindow(); |
|||
window.Show(); |
|||
Instance.Run(window); |
|||
} |
|||
|
|||
public AppBuilder SetupWithoutStarting() |
|||
{ |
|||
Setup(); |
|||
return this; |
|||
} |
|||
|
|||
public void Setup() |
|||
{ |
|||
if (Instance == null) |
|||
{ |
|||
throw new InvalidOperationException("No App instance configured."); |
|||
} |
|||
|
|||
if (WindowingSubsystem == null) |
|||
{ |
|||
throw new InvalidOperationException("No windowing system configured."); |
|||
} |
|||
|
|||
if (RenderingSubsystem == null) |
|||
{ |
|||
throw new InvalidOperationException("No rendering system configured."); |
|||
} |
|||
|
|||
WindowingSubsystem(); |
|||
RenderingSubsystem(); |
|||
Instance.Initialize(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue