创建新视图时,如何在视图模型中初始化属性? [英] How can I initialize a property in a viewmodel when creating a new view?
问题描述
我有一个应用程序,可以打开一个视图,使您可以搜索数据.但是,为了搜索,用户必须选择他要搜索的类别.目前,我正在尝试找出如何将所选类别从主视图模型(作为一个int)传递到新搜索视图的视图模型.目前,我正在尝试在主视图中使用以下内容:
I have an application that opens a view that allows you to search for data. However, in order to to search, the user has to select what category he wants to search under. Currently, I'm trying to figure out how to pass that selected category from the main viewmodel(as an int) to the new search view's view model. Currently I'm trying to use something like this in the main view:
比方说,我有两个视图View1
和View2
,每个视图都有各自的视图模型. View2ViewlModel
看起来像这样:
Let's say I have two views View1
and View2
an respective viewmodels for each. View2ViewlModel
looks like this:
public class View2ViewlModel : ViewModelBase
{
private IDataService _dataService;
public int DivisionIdnt {get; set;}
public View2ViewModel(IDataService dataService)
{
_dataService = dataService;
}
}
在View1
内部,我们创建并在收到消息时打开View2
.
And inside View1
we create and open View2
when a message is received.
public View2()
{
InitializeComponent();
Messenger.Default.Register<NotificationMessage<int>>(this, (m) => NotificationMesageReceived(m, m.Content));
}
private void NotificationMesageReceived(NotificationMessage<int> msg, int divisionIdnt)
{
if (msg.Notification == "SearchCred")
{
var findCredentialView = new View2();
findCredentialView.ShowDialog();
}
}
消息get作为在用户单击搜索按钮时发生的操作的一部分传递到View1ViewModel
中. 问题是我想将View2ViewModel
中新的View2
的DivisionIdnt
属性初始化为消息中的divisionIdnt
值.我该如何实现?我考虑过在代码中实例化View2ViewModel
,将DivisionIdnt
设置为message参数,然后将新View2
的DataContext
设置为新创建的视图模型,如下所示:
The message get's passed in View1ViewModel
as part as an action that happens when the user clicks a search button. The problem is I want to initialize the DivisionIdnt
property in the View2ViewModel
for the new View2
to the value of divisionIdnt
form the message. How can I achieve that? I thought about instantiating a View2ViewModel
in code, setting DivisionIdnt
to the message parameter and then setting the DataContext
of the new View2
to the newly created viewmodel, like this:
private void NotificationMesageReceived(NotificationMessage<int> msg, int divisionIdnt)
{
if (msg.Notification == "SearchCred")
{
var findCredentialView = new View2();
var vm = new View2ViewModel();
vm.DivisionIdnt = divisionIdnt;
findCredentialView.DataContext = vm;
findCredentialView.ShowDialog();
}
}
但是,这不起作用,因为在View2ViewModel
中,构造函数在运行时由DI注入了IDataService
.
However, that doesn't work because in View2ViewModel
, the constructor has an IDataService
injected by DI at runtime.
推荐答案
在实例化View2ViewModel之前,您能否仅从容器中解析IDataService的实例?
Can you not just resolve the instance of the IDataService from your container, before instantiating the View2ViewModel?
如何执行此操作将取决于您所使用的DI容器,但是使用AutoFac时将是:
How you do this will depend on which DI container you are using but with AutoFac it would be:
var findCredentialView = new View2();
var dataService = Container.Resolve<IDataService>();
var vm = new View2ViewModel(dataService);
vm.DivisionIdnt = divisionIdnt;
findCredentialView.DataContext = vm;
findCredentialView.ShowDialog();
或者,您可以从容器中解析View2ViewModel:
Alternatively you could resolve the View2ViewModel from the container:
var findCredentialView = new View2();
var vm = Container.Resolve<View2ViewModel>();
vm.DivisionIdnt = divisionIdnt;
findCredentialView.DataContext = vm;
findCredentialView.ShowDialog();
由谁来为您进行ctor注射.
Which would take care of the ctor injection for you.
很显然,这两种方法都要求您在运行时具有对容器的引用,但是大多数应用程序/容器都为此提供了引用.如果没有,只需按照您的喜好通过服务/单例/静态在引导时将您的容器暴露出来.
Obviously both of these approaches require that you have a reference to your container at runtime, but most applications/containers give you this. If they do not, simply expose your container at bootstrap time via a service/singleton/static as is your preference.
您可能还会发现此答案有帮助:如何在WPF/MVVM应用程序中处理依赖项注入
You may also find this answer helpful: How to handle dependency injection in a WPF/MVVM application
这篇关于创建新视图时,如何在视图模型中初始化属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!