我没有使用 Prism,我使用的是模板 10 的修改版本。
我只是快速浏览了 Prism 源代码。看起来模板 10 借鉴了 Prism 的很多想法。
我会尝试从两个角度回答你的问题:
1) AFAIK,在 Prism 中有一个静态类,您可以使用该类设置在自动查找相应视图时如何创建/解析视图模型。类是ViewModelLocationProvider,在文件ViewModelLocationProvider.cs中,您可以使用以下方法设置“视图模型工厂”
/// <summary>
/// Sets the default view model factory.
/// </summary>
/// <param name="viewModelFactory">The view model factory which provides the ViewModel type as a parameter.</param>
public static void SetDefaultViewModelFactory(Func<Type, object> viewModelFactory)
{
_defaultViewModelFactory = viewModelFactory;
}
/// <summary>
/// Sets the default view model factory.
/// </summary>
/// <param name="viewModelFactory">The view model factory that provides the View instance and ViewModel type as parameters.</param>
public static void SetDefaultViewModelFactory(Func<object, Type, object> viewModelFactory)
{
_defaultViewModelFactoryWithViewParameter = viewModelFactory;
}
/// <summary>
/// Registers the view model factory for the specified view type name.
/// </summary>
/// <param name="viewTypeName">The name of the view type.</param>
/// <param name="factory">The viewmodel factory.</param>
public static void Register(string viewTypeName, Func<object> factory)
{
_factories[viewTypeName] = factory;
}
那么获取视图模型实例的所有逻辑都在下面,注意这里的注释摘要,它描述了逻辑/策略
/// <summary>
/// Automatically looks up the viewmodel that corresponds to the current view, using two strategies:
/// It first looks to see if there is a mapping registered for that view, if not it will fallback to the convention based approach.
/// </summary>
/// <param name="view">The dependency object, typically a view.</param>
/// <param name="setDataContextCallback">The call back to use to create the binding between the View and ViewModel</param>
public static void AutoWireViewModelChanged(object view, Action<object, object> setDataContextCallback)
{
// Try mappings first
object viewModel = GetViewModelForView(view);
// Fallback to convention based
if (viewModel == null)
{
var viewModelType = _defaultViewTypeToViewModelTypeResolver(view.GetType());
if (viewModelType == null)
return;
viewModel = _defaultViewModelFactoryWithViewParameter != null ? _defaultViewModelFactoryWithViewParameter(view, viewModelType) : _defaultViewModelFactory(viewModelType);
}
setDataContextCallback(view, viewModel);
}
在第 87 行和第 96 行,您可以获得相应视图的视图模型实例。
这意味着,如果您不调用任何这些方法来设置工厂,它将回退到默认工厂,即
/// <summary>
/// The default view model factory whic provides the ViewModel type as a parameter.
/// </summary>
static Func<Type, object> _defaultViewModelFactory = type => Activator.CreateInstance(type);
很明显,您总会得到一个新实例。
关于Unity,我没看到什么特别的,唯一的线索是在PrismApplication.cs的PrismApplication类中,它的工厂设置如下:
/// <summary>
/// Configures the <see cref="ViewModelLocator"/> used by Prism.
/// </summary>
protected virtual void ConfigureViewModelLocator()
{
ViewModelLocationProvider.SetDefaultViewModelFactory((type) => Resolve(type));
}
这意味着工厂现在正在使用
/// <summary>
/// Resolves the specified type.
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A concrete instance of the specified type.</returns>
protected virtual object Resolve(Type type)
{
return Activator.CreateInstance(type);
}
你可以用你自己的实现覆盖它。
在PrismUnityApplication 类PrismUnityApplication.cs 中,它提供了一个默认实现来使用Unity 解析实例
/// <summary>
/// Implements the Resolves method to be handled by the Unity Container.
/// Use the container to resolve types (e.g. ViewModels and Flyouts)
/// so their dependencies get injected
/// </summary>
/// <param name="type">The type.</param>
/// <returns>A concrete instance of the specified type.</returns>
protected override object Resolve(Type type)
{
return Container.Resolve(type);
}
是的,就像其他人提到的那样,您可以通过 Unity 自己控制视图模型的生命周期。
2) 对不起,答案很长,
但我觉得最好向您展示一些可以使事情变得清晰的代码。
我会保持第二个简短。
在我看来,当您的视图消失时,您不需要视图模型。
我不确定框架堆栈是如何在 UWP 中实现的,以及它们如何管理视图/页面实例。我假设一旦您导航到不同的页面,之前的视图/页面应该被释放或可以在 GC 上释放,并且您具有能够导航回的参数和页面类型,但这将是一个新实例并且您通过恢复视图模型来恢复视图状态。
真的,我认为你在正确的轨道上。并且您应该尽可能保存/保留您的用户数据,并且您的解决方案在应用暂停然后恢复时有效,您仍然可以恢复视图的状态。
感谢阅读。