Silverlight PRISM,实例化视图并将数据传递到视图模型中

发布于 2024-08-05 07:05:14 字数 529 浏览 6 评论 0原文

我一直在四处寻找,但还没有找到太多这方面的信息。我设置了一个 PRISM 项目,到目前为止,一切似乎都在工作,在模块的主类中,我正在以编程方式创建我的视图。

我想做的是将对象发送到视图的每个实例的视图模型。目前,我正在尝试将对象传递到视图构造函数中并执行类似以下操作:

   public MyView(IUnityContainer container, List<string> myDataObject)
    {
        InitializeComponent();

        MyViewViewModel vm = LayoutRoot.DataContext as MyViewViewModel;
        vm.DataObject = myDataObject;
    }

这会导致 vm.DataObject 出现 NullReferenceObject,我假设此时视图模型尚未正确实例化。

谁能指导我应该如何完成此操作?数据对象是否应该通过视图发送?

感谢您抽出时间

I have been looking around and haven't found much information on this yet. I have a PRISM project set up and it all seems to be working so far, within the main class of a module I am programatically creating my views.

What I am trying to do is get an object sent through to the viewmodel for each instance of the view. At the moment I am trying to pass the object into the views constructor and do something like this:

   public MyView(IUnityContainer container, List<string> myDataObject)
    {
        InitializeComponent();

        MyViewViewModel vm = LayoutRoot.DataContext as MyViewViewModel;
        vm.DataObject = myDataObject;
    }

This causes a NullReferenceObject for vm.DataObject, I assume the viewmodel hasn't been properly instantiated at this point.

Can anyone guide me on how this should be done? should the data object even be send via the view?

Thanks for your time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

得不到的就毁灭 2024-08-12 07:05:14

由于您使用的是 Unity 容器,因此您可以使用它通过视图传递 DataContext 对象。创建 DataContext 时使用 RegisterInstance,然后在每个适当的视图上使用 Resolve。如果您有多个 DataContext,请使用命名注册。

Since you are using a Unity container, you could use it to pass the DataContext object through views. Use RegisterInstance when you create the DataContext, then use Resolve on each appropriate view. If you have more than one DataContext, use named registration.

爱已欠费 2024-08-12 07:05:14

在我们的代码以及我观看过的所有视频中,您都进行了设置,以便视图采用 ViewModel 作为参数。

    public ThisView(ThisVM vm)
    {
        InitializeComponent();
        DataContext = vm;
    }

如果您需要从其他地方传递东西,您可以使用 eventAggregator 来订阅和发布它们。
在你拥有 DataObject 的地方有点像这样:

    _EventAggregator.GetEvent<PushModelEvent>().Subscribe(ReceiveModel, true);

在你想要访问它的地方(可能是虚拟机):

    _EventAggregator.GetEvent<PushModelEvent>().Publish(_Model);

仍然有一些同步的事情需要处理,但是,如果这是你想要的,这应该让你继续前进。

In our code, and in all the videos that I've watched, you set it up so that the view takes, as a parameter, the ViewModel.

    public ThisView(ThisVM vm)
    {
        InitializeComponent();
        DataContext = vm;
    }

If you need to pass things around from somewhere else, you can use the eventAggregator to subscribe and publish to them.
Kind of like this in the spot where you have the DataObject:

    _EventAggregator.GetEvent<PushModelEvent>().Subscribe(ReceiveModel, true);

And like this where you want to have access to it (probably the VM):

    _EventAggregator.GetEvent<PushModelEvent>().Publish(_Model);

There are still some synchronous things to deal with, but, if this is what you want, this should get you going.

在我编写的示例应用程序中,我创建了一个由视图实现的界面。该接口有 1 个成员:SetViewModel。该方法接受一个对象,即 ViewModel。 ViewModel 为视图提供了绑定位置并处理命令。

public interface IDemoView
{
    void SetViewModel(object viewModel);
}

该接口在实际视图中的实现如下所示:

public partial class DemoView : UserControl, IDemoView
{
    public DemoView()
    {
        InitializeComponent();
    }

    public void SetViewModel(object viewModel)
    {
        DataContext = viewModel;
    }
}

视图的构造函数保持默认状态,并且代码隐藏中的唯一代码是此 SetViewModel 方法。

在 ViewModel 类的构造函数中,它调用此方法并将其自身传递给视图。

public IDemoView View { get; private set; }
private IDemoModel _model;
public DemoViewModel( IDemoView view, IDemoModel model)
{            
    _model = model;            
    View = view;
    View.SetViewModel(this);
}

Module类中的initialize方法向UnityContainer注册类型并将视图添加到区域

public void Initialize()
{
    _container.RegisterType<IDemoView, DemoView>();
    _container.RegisterType<IDemoModel, DemoModel>();
    _container.RegisterType<IDemoViewModel, DemoViewModel>();

    var viewModel = _container.Resolve<IDemoViewModel>();

    _regionManager.Regions["MainRegion"].Add(viewModel.View);
}

In a sample app I wrote I created an interface which was implemented by the view. This interface has 1 member, SetViewModel. This method takes an object, which is the ViewModel. The ViewModel provides the View with places to bind to and handles commands.

public interface IDemoView
{
    void SetViewModel(object viewModel);
}

the implementation of this interface in the actual view looks like this:

public partial class DemoView : UserControl, IDemoView
{
    public DemoView()
    {
        InitializeComponent();
    }

    public void SetViewModel(object viewModel)
    {
        DataContext = viewModel;
    }
}

The constructor of the View remains default, and the only code that will ever be in the codebehind is this SetViewModel method.

In the constructor of the ViewModel class, it calls this method and passes itself to the view.

public IDemoView View { get; private set; }
private IDemoModel _model;
public DemoViewModel( IDemoView view, IDemoModel model)
{            
    _model = model;            
    View = view;
    View.SetViewModel(this);
}

The initialize method in the Module class registers the types with the UnityContainer and adds the view to the region

public void Initialize()
{
    _container.RegisterType<IDemoView, DemoView>();
    _container.RegisterType<IDemoModel, DemoModel>();
    _container.RegisterType<IDemoViewModel, DemoViewModel>();

    var viewModel = _container.Resolve<IDemoViewModel>();

    _regionManager.Regions["MainRegion"].Add(viewModel.View);
}
梦中的蝴蝶 2024-08-12 07:05:14

有没有办法手动创建视图及其视图模型,并在视图模型上设置相关数据对象,以便它们可以进行数据绑定,然后在添加到区域之前将两者绑定在一起?

Is there a way to manually create the view and it's view model and set the relevant data objects on the viewmodel so that they can be data bound, then tie both together before adding to a region?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文