我是 PRISM 的新手,正在尝试一些东西。我在 MVVM 上遇到了一些困难。
“连接” View 与 View 模型的方式很明确:
通过unity注入(inject),或者
手动设置数据上下文(ServiceLocator)
如果我向区域添加 View ( View 模型是自动创建的),一切正常。但这不是用例。
让我们看一下这个例子:
public class MyViewModel : NotificationObject
{
public ObservableCollection<AnotherViewModel> OrderModel { get; private set; }
}
我必须创建 View 模型并将它们添加到集合中。此 View 模型必须在区域 (OrderRegion) 中显示 (AnotherView)。我的问题是,当我将 View 模型添加到区域时,现在如何创建 View 。该区域是一个 TabControl,因此可能会出现必须显示不同 View 的情况。
我已经看过 PRISM Quickstarts 和 StockTrader 示例。我正在寻找的是非常相似的
virtual protected void StartOrder(string tickerSymbol, TransactionType transactionType)
{
if (String.IsNullOrEmpty(tickerSymbol))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.StringCannotBeNullOrEmpty, "tickerSymbol"));
}
this.ShowOrdersView();
IRegion ordersRegion = _regionManager.Regions[RegionNames.OrdersRegion];
var orderCompositeViewModel = ServiceLocator.Current.GetInstance<IOrderCompositeViewModel>();
orderCompositeViewModel.TransactionInfo = new TransactionInfo(tickerSymbol, transactionType);
orderCompositeViewModel.CloseViewRequested += delegate
{
OrderModels.Remove(orderCompositeViewModel);
commandProxy.SubmitAllOrdersCommand.UnregisterCommand(orderCompositeViewModel.SubmitCommand);
commandProxy.CancelAllOrdersCommand.UnregisterCommand(orderCompositeViewModel.CancelCommand);
commandProxy.SubmitOrderCommand.UnregisterCommand(orderCompositeViewModel.SubmitCommand);
commandProxy.CancelOrderCommand.UnregisterCommand(orderCompositeViewModel.CancelCommand);
ordersRegion.Remove(orderCompositeViewModel);
if (ordersRegion.Views.Count() == 0)
{
this.RemoveOrdersView();
}
};
ordersRegion.Add(orderCompositeViewModel);
OrderModels.Add(orderCompositeViewModel);
commandProxy.SubmitAllOrdersCommand.RegisterCommand(orderCompositeViewModel.SubmitCommand);
commandProxy.CancelAllOrdersCommand.RegisterCommand(orderCompositeViewModel.CancelCommand);
commandProxy.SubmitOrderCommand.RegisterCommand(orderCompositeViewModel.SubmitCommand);
commandProxy.CancelOrderCommand.RegisterCommand(orderCompositeViewModel.CancelCommand);
ordersRegion.Activate(orderCompositeViewModel);
}
View 模型在代码中创建并添加到区域中。整个类型 regestring 是通过“ViewExportAttribute”发生的,因此更难理解其背后的模式。
编辑:
我找到了一种手动执行此操作的方法,但它不是很好:
var view = (FrameworkElement) ServiceLocator.Current.GetInstance<AnotherView>();
var model = ServiceLocator.Current.GetInstance<AnotherViewModel>();
view.DataContext = model;
regionManager.Regions["OrderRegion"].Add(view, null, true);
regionManager.Regions["OrderRegion"].Activate(view);
罗马
编辑2:
嗨,对不起,可能我不够清楚。
我的目标是创建一个 View 模型,然后像上面 StockTrader 的示例一样配置它:订阅事件、命令等。之后我想将这个 View 模型添加到该区域,这样就可以了被显示。该区域可能是一个选项卡控件,其中显示具有不同 View 模型的不同 View 。 顺序是:
- 在 Controller 类中创建 View 模型
- 配置 View 模型
- 将 View 模型添加到 Controller 内的本地集合
- 将 View 模型添加到区域
我一直在寻找的缺失部分是如何实现它, View 是“自动”创建的,包含绑定(bind)等所有内容。我在本文 (http://www.codeproject.com/Articles/229931/Understand-MVVM-Using-PRISM-by-Hello-World-Silverl) 中找到了一种方法。我必须为 View 和 View 模型(IAnotherViewModel、IAnotherView)创建自己的接口(interface)。
另一种方法可以在这里找到:http://paulstovell.com/blog/viewmodel-first-prism
请您参考如下方法:
有什么理由不为此使用隐式 DataTemplates 吗?
它们是 DataTemplates,定义了 DataType 属性,但不是 Key 属性,它们在 WPF 试图绘制对象的任何时候使用指定数据类型的
例如,
<TabControl ItemsSource="{Binding MyViewModelCollection}"
SelectedItem="{Binding SelectedViewModel}">
<!-- This could also go elsewhere, like Application.Resources -->
<TabControl.Resources>
<DataTemplate DataType="{x:Type local:ViewModelA}">
<local:ViewA />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ViewModelB}">
<local:ViewB />
</DataTemplate>
</TabControl.Resources>
</TabControl>
如果 TabControl 正在显示类型为 ViewModelA 的对象,它将使用 ViewA 绘制它,如果它正在显示 ViewModelB ,它将使用 ViewB
