Skip to content

Commit 6140131

Browse files
committed
feat: support for avalonia of ISingleViewApplicationLifetime
1 parent a83aa63 commit 6140131

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

src/Caliburn.Micro.Platform/Platforms/netcore-avalonia/BootstrapperBase.cs

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,13 @@ protected virtual void StartRuntime()
9191
AssemblySource.AddRange(SelectAssemblies());
9292

9393
Application = Application.Current;
94-
PrepareApplication();
95-
96-
Configure();
9794
IoC.GetInstance = GetInstance;
9895
IoC.GetAllInstances = GetAllInstances;
9996
IoC.BuildUp = BuildUp;
97+
PrepareApplication();
98+
99+
Configure();
100+
100101
}
101102

102103
/// <summary>
@@ -110,6 +111,27 @@ protected virtual void PrepareApplication()
110111

111112
controlledApplicationLifetime.Exit += OnExit;
112113
}
114+
if (Application.ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
115+
{
116+
OnSingleViewPlatformStartup(singleViewPlatform);
117+
}
118+
}
119+
120+
/// <summary>
121+
/// Called when the application lifetime is a single-view platform (for example Web Assembly or other single-view hosts).
122+
/// Override this method to configure and display the application's main view for single-view platforms.
123+
/// </summary>
124+
/// <param name="singleViewPlatform">The single-view application lifetime instance.</param>
125+
/// <remarks>
126+
/// Implementations should locate and bind the view for the root view model and assign it to
127+
/// <c>singleViewPlatform.MainView</c>. Use the provided helper DisplaySingleViewFor(...) for convenience.
128+
/// </remarks>
129+
/// <example>
130+
/// // Example usage in an override:
131+
/// // DisplaySingleViewFor<MainViewModel>(singleViewPlatform);
132+
/// </example>
133+
protected virtual void OnSingleViewPlatformStartup(ISingleViewApplicationLifetime singleViewPlatform)
134+
{
113135
}
114136

115137
/// <summary>
@@ -202,5 +224,58 @@ protected async Task DisplayRootViewFor<TViewModel>(IDictionary<string, object>
202224
{
203225
await DisplayRootViewForAsync(typeof(TViewModel), settings);
204226
}
227+
228+
/// <summary>
229+
/// Locates the view model, locates the associated view, applies optional settings,
230+
/// binds the view model to the view and sets it as the MainView on single-view platforms.
231+
/// </summary>
232+
/// <param name="singleViewPlatform">The single-view application lifetime to host the view.</param>
233+
/// <param name="viewModelType">The type of the view model to display.</param>
234+
/// <param name="context">An optional context object passed to the view locator and binder.</param>
235+
/// <param name="settings">Optional settings to apply to the view (property name/value pairs).</param>
236+
protected void DisplaySingleViewFor(ISingleViewApplicationLifetime singleViewPlatform, Type viewModelType, object context = null, IDictionary<string, object> settings = null)
237+
{
238+
//Log.Info("Displaying SingleView: {0}", viewModelType.FullName);
239+
var rootModel = _container.GetInstance(viewModelType, null);
240+
Control view = ViewLocator.LocateForModel(rootModel, null, context);
241+
ApplySettings(view, settings);
242+
ViewModelBinder.Bind(rootModel, view, context);
243+
singleViewPlatform.MainView = view;
244+
}
245+
246+
/// <summary>
247+
/// Generic overload. Locates the view model and view for TViewModel, applies settings,
248+
/// binds the view model to the view and sets it as the MainView on single-view platforms.
249+
/// </summary>
250+
/// <typeparam name="TViewModel">The view model type to display.</typeparam>
251+
/// <param name="singleViewPlatform">The single-view application lifetime to host the view.</param>
252+
/// <param name="context">An optional context object passed to the view locator and binder.</param>
253+
/// <param name="settings">Optional settings to apply to the view (property name/value pairs).</param>
254+
protected void DisplaySingleViewFor<TViewModel>(ISingleViewApplicationLifetime singleViewPlatform, object context = null, IDictionary<string, object> settings = null)
255+
{
256+
DisplaySingleViewFor(singleViewPlatform, typeof(TViewModel), context, settings);
257+
}
258+
259+
private bool ApplySettings(object target, IEnumerable<KeyValuePair<string, object>> settings)
260+
{
261+
if (settings != null)
262+
{
263+
var type = target.GetType();
264+
265+
foreach (var pair in settings)
266+
{
267+
var propertyInfo = type.GetProperty(pair.Key);
268+
269+
if (propertyInfo != null)
270+
{
271+
propertyInfo.SetValue(target, pair.Value, null);
272+
}
273+
}
274+
275+
return true;
276+
}
277+
278+
return false;
279+
}
205280
}
206281
}

0 commit comments

Comments
 (0)