I need to pass data from ViewModel 1 to ViewModel 2 using Prism.
TodoItem
is my Model with the string:
public TodoItem _todotItem { get; set
In order to capture the parameters on the second page, you must use Prism.Navigation.INavigationAware
. Please, check the code below, maybe will help you out.
./Views/ItemView.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms"
prism:ViewModelLocator.AutowireViewModel="True"
x:Class="Project.Views.ItemView"
Title="{Binding Title}">
<StackLayout>
<!-- List View -->
<ListView ItemsSource="{Binding Items}"
CachingStrategy="RecycleElement"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
HasUnevenRows="True"
SelectionMode="None">
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemTapped"
Command="{Binding ItemTappedCommand}"
EventArgsParameterPath="Item" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<!-- Data Template Cell -->
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ContentPage>
./ViewModels/ViewModelBase.cs
using Prism;
using Prism.Mvvm;
using Prism.Navigation;
using System;
namespace Project.ViewModels
{
// INavigationAware provides a way for objects involved in navigation to be notified of navigation activities.
public class ViewModelBase : BindableBase, INavigationAware, IDestructible
{
// ...
protected INavigationService NavigationService { get; private set; }
public ViewModelBase(INavigationService navigationService) => NavigationService = navigationService;
/// <summary>
/// Called when the implementer is being navigated away from.
/// </summary>
public virtual void OnNavigatedFrom(INavigationParameters parameters)
{
}
/// <summary>
/// Called when the implementer has been navigated to.
/// </summary>
public virtual void OnNavigatedTo(INavigationParameters parameters)
{
}
// ...
}
}
./ViewModels/ItemViewModel.cs
using Prism.Navigation;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace Project.ViewModels
{
public class ItemViewModel : ViewModelBase
{
// ...
private ObservableCollection<ItemList> _items;
public ObservableCollection<ItemList> Items
{
get => _items;
set => SetProperty(ref _items, value);
}
public ICommand ItemTappedCommand => new AsyncCommand(ItemTappedCommandAsync);
public ItemViewModel(INavigationService navigationService)
: base(navigationService)
{
Items = new ObservableCollection<ItemList>();
// Load the data
}
// ...
private async Task ItemTappedCommandAsync(object item)
{
var navParams = new NavigationParameters
{
{ "ItemSelected", (Item)item }
};
await NavigationService.NavigateAsync(nameof(ItemDetailView), navParams);
}
}
}
./ViewModels/ItemDetailViewModel.cs
using Prism.Navigation;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;
namespace Project.ViewModels
{
public class ItemDetailViewModel : ViewModelBase
{
// ...
public override void OnNavigatingTo(INavigationParameters parameters)
{
// Capture the parameter
System.Diagnostics.Debug.WriteLine(parameters);
}
// ...
}
}