我有一个实现以下接口(interface)的用户控件:
public interface IView
{
object DataContext { get; set; }
}
..它是实现以下接口(interface)的相应 View 模型:
public interface ICertificationViewModel
{
string NumOfCertification { get; set; }
}
我有另一个名为 NavigationService 的服务,它实现了以下接口(interface):
public interface INavigationService<TView,TViewModel>
{
void ShowView<TView,TViewModel,T>(T model) where T:class;
}
我正在使用统一,每当调用导航服务上的 ShowView 方法时,我都需要将一个新的( transient ) View 和 View 模型放在一起。我无法将 View 和 ViewModel 作为构造函数依赖项注入(inject)(因为应该创建新实例),并且我不想采用服务定位器路由(即在 ShowView 中调用 unity 来解析 View 和 View 模型)。有没有办法可以使用统一或其他任何方法来解决这个问题?我到处搜索,但找不到明确的答案。我使用的是 Prism 和 .NET 3.5。我还想保持这种通用性,以便可以使用 NavigationService ShowView 方法解决任何 View 和 View 模型。
你能帮忙解决一下吗?
最佳答案
Prism 库提供了 区域导航服务控制 View 之间的导航。 区域导航服务实现 IRegionNavigationService 和 请求导航() 方法已定义。您可以为在指定区域中注册的任何 View 解析导航(在不同区域的 View 中导航是没有意义的)。
/// <summary>
/// Provides navigation for regions.
/// </summary>
public interface IRegionNavigationService : 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);
}
如果您在 中完成了一些自定义的前后导航工作显示 View () 方法,可以实现 INavigationAware ,它定义了 OnNavigatedTo() 和 OnNavigatedFrom() ,在所涉及的 View 或 View 模型上。
要了解这些方法的工作原理,请访问以下 快速入门 使用 OnNavigatedTo() 方法,以便在新导航 View 上读取和设置任何上下文参数:
INavigationAware 文档:
如果这没有帮助,了解 的预期行为会很有用。显示 View () 方法
将有。
问候。
关于c# - 使用 IOC 解析 View 和 ViewModel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19114097/