简体   繁体   中英

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

I am new to windows phone 8.1. I have a view List and when user click on an item he should move to the item details pages for that item as a result inside view model I bind a command that perform the following :

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

Till now every thing is working fine. But how can I receive the item object in the view model of item details page?

I can access it in the code behind but what is the mvvm best practices for that problem.

Let's assume you have the following list. I left out the declaration of the item template and the 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>

Then you'll need a converter that converts the click event to the actual item:

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();
    }
}

The last thing is the viewmodel where you're passing everything to a command:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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