簡體   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