简体   繁体   English

MVVM-Light将数据传递到新的ViewModel

[英]MVVM-Light pass data to new ViewModel

Just fiddling around with some WP8.1 development + MVVM-Light toolkit and am having trouble trying to figure out how to achieve something.. 只是随便摆弄一些WP8.1开发+ MVVM-Light工具包,在尝试弄清楚如何实现目标时遇到了麻烦。

Basically: 基本上:

  • I have a View (let's call it View1 ) which has a control ( LongListSelector in this case) that is databound to a Collection of items (let's call them DataItem ) (which is populated by a Service from a ViewModel ) 我有一个View (在我们的情况下,将其称为View1 ),它具有一个控件(在这种情况下为LongListSelector ),该控件数据绑定到项目的集合(我们将其称为DataItem )(由ViewModelService填充)

And I want it so: 我想要这样:

  • When the user taps on a particular item in this control, it passes the item tapped (or a property of this item) to a new View (called View2 ), which will either create a new ViewModel for View2 or re-use an existing one (depending on the Key of the instances in SimpleIoC , determined by some property in the DataItem tapped). 当用户点击在该控制的特定项目,并将其传递给新的分接的项目(或该项目的属性) View (称为View2 ),这将创建一个新的ViewModel用于View2或重新使用现有的(取决于SimpleIoC实例的Key ,该实例由所窃听的DataItem的某些属性确定)。
  • This new ViewModel then uses the passed property of the DataItem tapped in its' constructor to fetch data from a different Service 然后,此新ViewModel使用在其构造函数中点击的DataItem的通过属性来从其他Service提取数据

So how can I achieve this? 那么我该如何实现呢? I am thinking of Creating/Registering the new ViewModel on the SelectionChanged event of the control, passing it in the Service and Property like so: 我正在考虑在控件的SelectionChanged事件上创建/注册新的ViewModel ,将其传递到ServiceProperty如下所示:

    private void LongListSelector_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataItem item = e.AddedItems[0] as DataItem;

        SimpleIoc.Default.Register(() => new ViewModel2(new Model2Service(), item.Name));

        NavigationService.Navigate(new Uri("/View2.xaml", UriKind.Relative));
    }

Which works fine for the first DataItem tapped, but not when a second is tapped. 对于一个被点击的DataItem ,它工作正常,但是第二个被点击时却不能。 Note : I couldn't register ViewModel2 in ViewModelLocator as I couldn't get DataItem properties passed to the constructor for ViewModel2 , which is why I'm trying to Register it elsewhere. 注意 :我无法在ViewModelLocator注册ViewModel2 ,因为我无法将DataItem属性传递给ViewModel2的构造函数,这就是为什么我尝试在其他地方注册它。

Not sure if this is abiding by MVVM architecture, I suppose not as this answer states that I shouldn't be handling this in my View . 不知道这是否符合MVVM体系结构,我想不要这样做,因为该答案表明我不应该在View处理此问题。

So to recap, I want a user to be able to tap on an item in a LongListSelector which will then navigate the user to a new View which is bound to a new (or existing) ViewModel according to a property of the selected item. 回顾一下,我希望用户能够在LongListSelector点击一个项目,然后将其导航到一个新View ,该View根据所选项目的属性绑定到新的(或现有的) ViewModel How can I achieve this? 我该如何实现?

Any help would be greatly appreciated. 任何帮助将不胜感激。

Have you tried using the Messenger in MVVMLight? 您是否尝试过在MVVMLight中使用Messenger Try creating the instance of the ViewModel2 in ViewModelLocator with the key being some default value. 尝试创建的实例ViewModel2ViewModelLocator键为一些默认值。 In the ViewModel's constructor, register to receive a string property (assuming item.Name is a string ) like this: 在ViewModel的构造函数中,注册以接收如下string属性(假设item.Namestring ):

MessengerInstance.Register<string>(this,name=>{NameProperty=name;});

Then in the LongListSelector_SelectionChanged , send the item.Name like this: 然后在LongListSelector_SelectionChanged ,发送项目。 item.Name如下:

Messenger.Default.Send<string>(item.Name);

And then navigate to the page. 然后导航到页面。


Marked as solution but whole comment thread below reveals more details 标记为解决方案,但下面的整个注释主题揭示了更多细节

If you are using UWP with Template Studio 10 and MVVM Light and want to either: 如果您将UWP与Template Studio 10和MVVM Light一起使用,并希望:

  • Access the parameter that is passed in the NavigationServicesEx.Navigate method 访问NavigationServicesEx.Navigate方法中传递的参数

  • Call a method in your ViewModel when a page is navigated to. 导航到页面时,在ViewModel中调用一个方法。

This blog by Marco Minerva is the only guidance I could find that is up to up to date with UWP, Templates Studio 10 and MVVM Light 5.0 (thanks again Marco!) Marco Minerva撰写的此博客是我可以找到的唯一有关UWP,Templates Studio 10和MVVM Light 5.0的最新指南(再次感谢Marco!)

tl;dr It works by hooking in to the Frame_Navigating event that it missing from the vanilla NavigationServiceEx class. tl; dr可以通过挂钩到普通NavigationServiceEx类中缺少的Frame_Navigating事件来工作。

Create the INavigable interface described in the blog: 创建博客中描述的INavigable接口:

public interface INavigable
{
    Task OnNavigatedToAsync(object parameter, NavigationMode mode);
    void OnNavigatingFrom(NavigatingCancelEventArgs e);
    void OnNavigatedFrom();
}

Add a handler for the Frame.Navigating event in the NavigationServicesEx class (with some additional plumbing, see blog) then realise the INavigable interface in your ViewModels. 在NavigationServicesEx类中添加Frame.Navigating事件的处理程序(还有一些其他功能,请参阅博客),然后在ViewModels中实现INavigable接口。

You will then be able to access the parameter that you passed in your Navigate call: 然后,您将可以访问在Navigate调用中传递的参数:

NavigationServiceEx.Navigate(typeof(DestinationPage).FullName, yourParameter);

In the OnNavigatedToAsync method that you implement in your ViewModel: 在您在ViewModel中实现的OnNavigatedToAsync方法中:

public Task OnNavigatedToAsync(object parameter, NavigationMode mode)
{
    if (parameter != null)
    {
        YourThing thing = parameter as YourThing;
        this.UseYourThing(thing);
    }
    return Task.CompletedTask;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有相同ViewModel的多个实例的MVVM-Light Messenger - MVVM-Light Messenger With Multiple instances of the Same ViewModel MVVM-Light =>将命令参数和EventArgs传递给Command - MVVM-Light => Pass Command Parameter AND EventArgs to Command MenuItem通过RelayCommand ala MVVM-Light&Header将选定的项目传递到ViewModel - MenuItem Passing selected item to ViewModel via RelayCommand ala MVVM-Light & Header 如何覆盖ViewModel DataContext,以便绑定到View中的对象(Mvvm-Light)? - How do I override the ViewModel DataContext so I can bind to objects in the View (Mvvm-Light)? MVVM Light通过ViewModelLocator将ViewModel传递给ObjectDataProvider - MVVM Light pass ViewModel to ObjectDataProvider via ViewModelLocator Mvvm-Light用户控制RelayCommand模板绑定 - Mvvm-Light User Control RelayCommand TemplateBinding 如何在MVVM Light xamrin中清除ViewModel的数据? - How to clear data of ViewModel in MVVM Light xamrin? 使用RadGridView,DataForm和MVVM-light的CRUD方法 - CRUD approach with RadGridView, DataForm & MVVM-light EventToCommand可能出现的Mvvm-light错误 - Possible Mvvm-light bug with EventToCommand 如何在IMessenger中使用MVVM-Light,以在Windows 8.1中的2个VM之间传递值 - How to use MVVM-Light with IMessenger to pass a value between 2 VMs in WIndows 8.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM