Skip to content

Commit bd6ff3c

Browse files
authored
Merge pull request #1009 from emanonhero/feat/Avalonia/SingleViewApp
feat: support for avalonia of ISingleViewApplicationLifetime (Web Assembly)
2 parents a83aa63 + 0f468bb commit bd6ff3c

File tree

1 file changed

+80
-3
lines changed

1 file changed

+80
-3
lines changed

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

Lines changed: 80 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,29 @@ 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+
/// <code>
132+
/// DisplaySingleViewFor&lt;MainViewModel&gt;(singleViewPlatform);
133+
/// </code>
134+
/// </example>
135+
protected virtual void OnSingleViewPlatformStartup(ISingleViewApplicationLifetime singleViewPlatform)
136+
{
113137
}
114138

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

0 commit comments

Comments
 (0)