Pass data parameter from model to next page Prism Xamarin Forms












0















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

private readonly INavigationService _navigationService;


I assigned it in the constructor:



public MainPageViewModel(INavigationService navigationService, TodoItem todotItem)
{
_navigationService = navigationService;
_todotItem = todotItem;
}


And this is the code I use to navigate to next page (including the parameter):



NavigationParameters navParams = new NavigationParameters();
navParams.Add("PassedValue", _todoItem.name);

_navigationService.NavigateAsync("SecondPage", navParams);


When I set the breakpoint on _todoItem.name it says null. The data is fetched before I click the listview. What am i missing?



Edit:



This is how I navigate (click from listview):



private EventItem _selectedEvent { get; set; }
public EventItem SelectedEvent
{
get { return _selectedEvent; }
set
{
if (_selectedEvent != value)
{
if (Device.RuntimePlatform == Device.iOS)
{
_selectedEvent = null;
}
else
{
_selectedEvent = value;
}
NavigationParameters navParams = new NavigationParameters();

navParams.Add("PassedValue", _todoItem.name);


_navigationService.NavigateAsync("SecondPage", navParams);
}
}
}


I do use the proper ways to catch the parameter on the second ViewModel (However, I get null in the first ViewModel):



public void OnNavigatingTo(INavigationParameters parameters)
{
if (parameters.ContainsKey("PassedValue"))
{
_todo = (string)parameters["PassedValue"];

OnPropertyChanged("Todo");
}

}


I use HTTPClient to fetch data.










share|improve this question

























  • If you had ReSharper installed it would tell you that your naming convention for _todoItem isn't correct.

    – samis
    Nov 28 '18 at 13:33











  • @samis You mean to uppercase? I changed that but still null.

    – KalleP
    Nov 28 '18 at 13:38











  • You can use var navParams = new NavigationParameters(); instead.

    – samis
    Nov 28 '18 at 14:59













  • The TodoItem member is a property, so public TodoItem TodotItem { get; set; }, and EventItem a field, so private EventItem _selectedEvent;

    – samis
    Nov 28 '18 at 15:03













  • @samis Still null after changing this: var navParams = new NavigationParameters();

    – KalleP
    Nov 28 '18 at 15:31
















0















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

private readonly INavigationService _navigationService;


I assigned it in the constructor:



public MainPageViewModel(INavigationService navigationService, TodoItem todotItem)
{
_navigationService = navigationService;
_todotItem = todotItem;
}


And this is the code I use to navigate to next page (including the parameter):



NavigationParameters navParams = new NavigationParameters();
navParams.Add("PassedValue", _todoItem.name);

_navigationService.NavigateAsync("SecondPage", navParams);


When I set the breakpoint on _todoItem.name it says null. The data is fetched before I click the listview. What am i missing?



Edit:



This is how I navigate (click from listview):



private EventItem _selectedEvent { get; set; }
public EventItem SelectedEvent
{
get { return _selectedEvent; }
set
{
if (_selectedEvent != value)
{
if (Device.RuntimePlatform == Device.iOS)
{
_selectedEvent = null;
}
else
{
_selectedEvent = value;
}
NavigationParameters navParams = new NavigationParameters();

navParams.Add("PassedValue", _todoItem.name);


_navigationService.NavigateAsync("SecondPage", navParams);
}
}
}


I do use the proper ways to catch the parameter on the second ViewModel (However, I get null in the first ViewModel):



public void OnNavigatingTo(INavigationParameters parameters)
{
if (parameters.ContainsKey("PassedValue"))
{
_todo = (string)parameters["PassedValue"];

OnPropertyChanged("Todo");
}

}


I use HTTPClient to fetch data.










share|improve this question

























  • If you had ReSharper installed it would tell you that your naming convention for _todoItem isn't correct.

    – samis
    Nov 28 '18 at 13:33











  • @samis You mean to uppercase? I changed that but still null.

    – KalleP
    Nov 28 '18 at 13:38











  • You can use var navParams = new NavigationParameters(); instead.

    – samis
    Nov 28 '18 at 14:59













  • The TodoItem member is a property, so public TodoItem TodotItem { get; set; }, and EventItem a field, so private EventItem _selectedEvent;

    – samis
    Nov 28 '18 at 15:03













  • @samis Still null after changing this: var navParams = new NavigationParameters();

    – KalleP
    Nov 28 '18 at 15:31














0












0








0








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

private readonly INavigationService _navigationService;


I assigned it in the constructor:



public MainPageViewModel(INavigationService navigationService, TodoItem todotItem)
{
_navigationService = navigationService;
_todotItem = todotItem;
}


And this is the code I use to navigate to next page (including the parameter):



NavigationParameters navParams = new NavigationParameters();
navParams.Add("PassedValue", _todoItem.name);

_navigationService.NavigateAsync("SecondPage", navParams);


When I set the breakpoint on _todoItem.name it says null. The data is fetched before I click the listview. What am i missing?



Edit:



This is how I navigate (click from listview):



private EventItem _selectedEvent { get; set; }
public EventItem SelectedEvent
{
get { return _selectedEvent; }
set
{
if (_selectedEvent != value)
{
if (Device.RuntimePlatform == Device.iOS)
{
_selectedEvent = null;
}
else
{
_selectedEvent = value;
}
NavigationParameters navParams = new NavigationParameters();

navParams.Add("PassedValue", _todoItem.name);


_navigationService.NavigateAsync("SecondPage", navParams);
}
}
}


I do use the proper ways to catch the parameter on the second ViewModel (However, I get null in the first ViewModel):



public void OnNavigatingTo(INavigationParameters parameters)
{
if (parameters.ContainsKey("PassedValue"))
{
_todo = (string)parameters["PassedValue"];

OnPropertyChanged("Todo");
}

}


I use HTTPClient to fetch data.










share|improve this question
















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

private readonly INavigationService _navigationService;


I assigned it in the constructor:



public MainPageViewModel(INavigationService navigationService, TodoItem todotItem)
{
_navigationService = navigationService;
_todotItem = todotItem;
}


And this is the code I use to navigate to next page (including the parameter):



NavigationParameters navParams = new NavigationParameters();
navParams.Add("PassedValue", _todoItem.name);

_navigationService.NavigateAsync("SecondPage", navParams);


When I set the breakpoint on _todoItem.name it says null. The data is fetched before I click the listview. What am i missing?



Edit:



This is how I navigate (click from listview):



private EventItem _selectedEvent { get; set; }
public EventItem SelectedEvent
{
get { return _selectedEvent; }
set
{
if (_selectedEvent != value)
{
if (Device.RuntimePlatform == Device.iOS)
{
_selectedEvent = null;
}
else
{
_selectedEvent = value;
}
NavigationParameters navParams = new NavigationParameters();

navParams.Add("PassedValue", _todoItem.name);


_navigationService.NavigateAsync("SecondPage", navParams);
}
}
}


I do use the proper ways to catch the parameter on the second ViewModel (However, I get null in the first ViewModel):



public void OnNavigatingTo(INavigationParameters parameters)
{
if (parameters.ContainsKey("PassedValue"))
{
_todo = (string)parameters["PassedValue"];

OnPropertyChanged("Todo");
}

}


I use HTTPClient to fetch data.







c# xamarin xamarin.forms prism






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '18 at 22:22









samis

3,52362251




3,52362251










asked Nov 28 '18 at 13:29









KallePKalleP

14119




14119













  • If you had ReSharper installed it would tell you that your naming convention for _todoItem isn't correct.

    – samis
    Nov 28 '18 at 13:33











  • @samis You mean to uppercase? I changed that but still null.

    – KalleP
    Nov 28 '18 at 13:38











  • You can use var navParams = new NavigationParameters(); instead.

    – samis
    Nov 28 '18 at 14:59













  • The TodoItem member is a property, so public TodoItem TodotItem { get; set; }, and EventItem a field, so private EventItem _selectedEvent;

    – samis
    Nov 28 '18 at 15:03













  • @samis Still null after changing this: var navParams = new NavigationParameters();

    – KalleP
    Nov 28 '18 at 15:31



















  • If you had ReSharper installed it would tell you that your naming convention for _todoItem isn't correct.

    – samis
    Nov 28 '18 at 13:33











  • @samis You mean to uppercase? I changed that but still null.

    – KalleP
    Nov 28 '18 at 13:38











  • You can use var navParams = new NavigationParameters(); instead.

    – samis
    Nov 28 '18 at 14:59













  • The TodoItem member is a property, so public TodoItem TodotItem { get; set; }, and EventItem a field, so private EventItem _selectedEvent;

    – samis
    Nov 28 '18 at 15:03













  • @samis Still null after changing this: var navParams = new NavigationParameters();

    – KalleP
    Nov 28 '18 at 15:31

















If you had ReSharper installed it would tell you that your naming convention for _todoItem isn't correct.

– samis
Nov 28 '18 at 13:33





If you had ReSharper installed it would tell you that your naming convention for _todoItem isn't correct.

– samis
Nov 28 '18 at 13:33













@samis You mean to uppercase? I changed that but still null.

– KalleP
Nov 28 '18 at 13:38





@samis You mean to uppercase? I changed that but still null.

– KalleP
Nov 28 '18 at 13:38













You can use var navParams = new NavigationParameters(); instead.

– samis
Nov 28 '18 at 14:59







You can use var navParams = new NavigationParameters(); instead.

– samis
Nov 28 '18 at 14:59















The TodoItem member is a property, so public TodoItem TodotItem { get; set; }, and EventItem a field, so private EventItem _selectedEvent;

– samis
Nov 28 '18 at 15:03







The TodoItem member is a property, so public TodoItem TodotItem { get; set; }, and EventItem a field, so private EventItem _selectedEvent;

– samis
Nov 28 '18 at 15:03















@samis Still null after changing this: var navParams = new NavigationParameters();

– KalleP
Nov 28 '18 at 15:31





@samis Still null after changing this: var navParams = new NavigationParameters();

– KalleP
Nov 28 '18 at 15:31












1 Answer
1






active

oldest

votes


















0














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

// ...
}
}





share|improve this answer
























  • I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

    – KalleP
    Nov 28 '18 at 15:42













  • How do I catch the parameter with a string from the xaml in the new page? @Jader

    – KalleP
    Nov 28 '18 at 15:45











  • PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

    – Jader Oliveira
    Nov 28 '18 at 16:04











  • NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

    – Jader Oliveira
    Nov 28 '18 at 16:12











  • Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

    – KalleP
    Nov 28 '18 at 16:13












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520601%2fpass-data-parameter-from-model-to-next-page-prism-xamarin-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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

// ...
}
}





share|improve this answer
























  • I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

    – KalleP
    Nov 28 '18 at 15:42













  • How do I catch the parameter with a string from the xaml in the new page? @Jader

    – KalleP
    Nov 28 '18 at 15:45











  • PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

    – Jader Oliveira
    Nov 28 '18 at 16:04











  • NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

    – Jader Oliveira
    Nov 28 '18 at 16:12











  • Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

    – KalleP
    Nov 28 '18 at 16:13
















0














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

// ...
}
}





share|improve this answer
























  • I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

    – KalleP
    Nov 28 '18 at 15:42













  • How do I catch the parameter with a string from the xaml in the new page? @Jader

    – KalleP
    Nov 28 '18 at 15:45











  • PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

    – Jader Oliveira
    Nov 28 '18 at 16:04











  • NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

    – Jader Oliveira
    Nov 28 '18 at 16:12











  • Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

    – KalleP
    Nov 28 '18 at 16:13














0












0








0







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

// ...
}
}





share|improve this answer













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

// ...
}
}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 15:19









Jader OliveiraJader Oliveira

1717




1717













  • I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

    – KalleP
    Nov 28 '18 at 15:42













  • How do I catch the parameter with a string from the xaml in the new page? @Jader

    – KalleP
    Nov 28 '18 at 15:45











  • PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

    – Jader Oliveira
    Nov 28 '18 at 16:04











  • NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

    – Jader Oliveira
    Nov 28 '18 at 16:12











  • Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

    – KalleP
    Nov 28 '18 at 16:13



















  • I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

    – KalleP
    Nov 28 '18 at 15:42













  • How do I catch the parameter with a string from the xaml in the new page? @Jader

    – KalleP
    Nov 28 '18 at 15:45











  • PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

    – Jader Oliveira
    Nov 28 '18 at 16:04











  • NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

    – Jader Oliveira
    Nov 28 '18 at 16:12











  • Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

    – KalleP
    Nov 28 '18 at 16:13

















I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

– KalleP
Nov 28 '18 at 15:42







I need PropertyChanged in the ViewModelBase too but it says that im hiding something. Should I worry? Maybe I dont need PropertChanged. Thanks for the help. @Jader Oliveira

– KalleP
Nov 28 '18 at 15:42















How do I catch the parameter with a string from the xaml in the new page? @Jader

– KalleP
Nov 28 '18 at 15:45





How do I catch the parameter with a string from the xaml in the new page? @Jader

– KalleP
Nov 28 '18 at 15:45













PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

– Jader Oliveira
Nov 28 '18 at 16:04





PropertyChanged is already implemented via Prism.Mvvm.BindableBase, and then you can use in your properties. E.g.: set => SetProperty(ref _isBusy, value); @KalleP

– Jader Oliveira
Nov 28 '18 at 16:04













NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

– Jader Oliveira
Nov 28 '18 at 16:12





NavigationParameters is a KeyValuePair, so you just need use the key 'ItemSelected' in order to pick the value object 'Item' > var itemSelected = (Item)parameters["ItemSelected"];

– Jader Oliveira
Nov 28 '18 at 16:12













Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

– KalleP
Nov 28 '18 at 16:13





Thank you alot. Just hope I can change the itemssource to go to viewmodel instead of code behind. I had problems showing data when using viewmodel. @Jader Oliveria

– KalleP
Nov 28 '18 at 16:13




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53520601%2fpass-data-parameter-from-model-to-next-page-prism-xamarin-forms%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)