我有一个实现以下界面的用户控件:
public interface IView
{
object DataContext { get; set; }
}
..及其对应的 View 模型,实现以下接口(interface):
public interface ICertificationViewModel
{
string NumOfCertification { get; set; }
}
我还有一个称为NavigationService的服务,它实现了以下接口(interface):
public interface INavigationService<TView,TViewModel>
{
void ShowView<TView,TViewModel,T>(T model) where T:class;
}
我正在使用unity,每当调用导航服务上的ShowView方法时,都需要将新的( transient ) View 和viewmodel放在一起。我不能将View和ViewModel作为构造函数依赖项注入(inject)(因为应该创建新实例),并且我不想采用服务定位器路线(即在ShowView内部调用unity来解析 View 和ViewModel)。有没有办法我可以使用团结或其他方式解决此问题?我到处搜索,找不到确切的答案。我使用的是Prism和.NET 3.5。我还想对此保持通用,以便可以使用NavigationService ShowView方法解析所有 View 和 View 模型。
您能帮忙解决一个问题吗?
最佳答案
Prism 库提供了一个 RegionNavigationService ,用于控制 View 之间的导航。 RegionNavigationService 实现 IRegionNavigationService ,并定义了 RequestNavigate()方法。您可以为在指定区域中注册的任何 View 解析导航(在不同区域中的 View 之间导航是没有意义的)。
/// <summary>
/// Provides navigation for regions.
/// </summary>
public interface IRegionNavigationService : INavigateAsync
{
...
}
INavigateAsync:
/// <summary>
/// Provides methods to perform navigation.
/// </summary>
/// <remarks>
/// Convenience overloads for the methods in this interface can be found as extension methods on the
/// <see cref="NavigationAsyncExtensions"/> class.
/// </remarks>
public interface INavigateAsync
{
/// <summary>
/// Initiates navigation to the target specified by the <see cref="Uri"/>.
/// </summary>
/// <param name="target">The navigation target</param>
/// <param name="navigationCallback">The callback executed when the navigation request is completed.</param>
/// <remarks>
/// Convenience overloads for this method can be found as extension methods on the
/// <see cref="NavigationAsyncExtensions"/> class.
/// </remarks>
void RequestNavigate(Uri target, Action<NavigationResult> navigationCallback);
}
如果您在 ShowView()方法中完成了一些自定义的前后导航工作,则可以在 View 或ViewModel上实现 INavigationAware ,它定义了 OnNavigatedTo()和 OnNavigatedFrom()ojis_rstrong。
要查看这些方法如何工作,以下快速入门使用 OnNavigatedTo()方法来读取和设置新的导航 View 上的任何上下文参数:
INavigationAware 文档:
如果这样做没有帮助,那么了解 ShowView()方法的预期行为将很有用。
将有。
问候。
关于c# - 使用IOC解决View和ViewModel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19114097/