繁体   English   中英

如何在wp 8.1中使用mvvvm在导航页面之间传递数据?

[英]How to pass data between pages on navigation using mvvvm in wp 8.1?

我是Windows Phone 8.1的新手。 我有一个视图列表,当用户单击某个项目时,他应该移动到该项目的项目详细信息页面,作为视图模型内部的结果,我绑定了一个执行以下命令的命令:

 ((Frame)Window.Current.Content).Navigate(typeof(ItemView), item);

到现在为止,一切正常。 但是如何在商品详细信息页面的视图模型中接收商品对象?

我可以在后面的代码中访问它,但是该问题的mvvm最佳实践是什么。

假设您有以下列表。 我省略了项目模板和itemsource的声明:

<ListView IsItemClickEnabled="True" x:Name="lvItems">
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="ItemClick">
            <Core:InvokeCommandAction Command="{Binding NavigateDetailsCommand, Mode=OneWay}" InputConverter="{StaticResource ItemClickConverter}" CommandParameter="{Binding SelectedItem, ElementName=lvItems}"/>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</ListView>

然后,您将需要一个将click事件转换为实际项目的转换器:

public class ItemClickConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        var icea = (ItemClickEventArgs)value;
        if (icea != null)
            return icea.ClickedItem;
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

最后一件事是将所有内容传递给命令的viewmodel:

public RelayCommand<MyEntity> NavigateDetailsCommand;

// In your constructor, do this:
this.NavigateDetailsCommand= new RelayCommand<MyEntity>(navigateDetails);
// I'm assuming you injected a navigation service into your viewmodel..
this._navigationService.Configure("itemviewpage", typeof(ItemView));


// Declare a method that does the navigation:
private void navigateDetails(MyEntity item) {
    this._navigationService.NavigateTo("itemviewpage", item);
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM