[英]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: 基本上:
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
)(由ViewModel
的Service
填充) And I want it so: 我想要这样:
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
的某些属性确定)。 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
,将其传递到Service
和Property
如下所示:
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. 尝试创建的实例
ViewModel2
在ViewModelLocator
键为一些默认值。 In the ViewModel's constructor, register to receive a string
property (assuming item.Name
is a string
) like this: 在ViewModel的构造函数中,注册以接收如下
string
属性(假设item.Name
是string
):
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.