How to pass a parameter from one Page to another Page in Xamarin.Forms?
Solution 1
The easiest approach would be to pass the value as a parameter to the constructor of Page2. Or you could create a public property on Page2 and assign the value to it after you create it.
await _navigation.PushAsync(new Page2(argument_goes_here)); // HERE
Solution 2
Use SQLite to store the object and instantiate it in the new ViewModel or use the messaging center built into Xamarin.Forms to pass data between ViewModels indirectly.
Jason's approach will work but personally passing data up to the view, to another view then back down to the view model is not something I want to do.
SQLite Documentation
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/
Messaging Center Documentation
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/messaging-center/
Dragos
Updated on October 08, 2020Comments
-
Dragos over 3 years
I want to send a value when I press a button in a form to another form through the MVVM pattern.
This is the XAML file
<Button x:Name="myButton" Text="RandomButton" TextColor="#000000" BackgroundColor="{x:Static local:FrontEndConstants.ButtonBackground}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3" Command="{Binding NewPage}" CommandParameter="100"> </Button>
And this is my
ModelView class
where I get redirected to another form.class JumpMVVM :INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private INavigation _navigation; public ICommand NewPage { get { return new Command(async () => { await _navigation.PushAsync(new Page2()); // HERE }); } } public JumpMVVM() { } public JumpMVVM(INavigation navigation) { _navigation = navigation; }
The jump works. How can I send that "CommandParameter" to "Page2" ?
Thanks, Dragos
-
Dragos about 9 yearsOk, but how do I access the value from the CommandParameter of the button?
-
Dragos about 9 yearswell actually the data is already in the view. imagine if a user enter's a number in a field and then presses a button and a new view pops up and that value needs to be sent.
-
ClintL about 9 yearsYou can pass data from view to view in the views constructor as suggested. It really depends on if your app is complex enough to need the additional infrastructure of MVVM. If you want MVVM I would warn against coupling between views, and suggest storing the data of one view in a model that is persisted by SQLite or some other local DB structure. Then each view bound to a view model in which the view model can access the DB for data will be independent of the previous view.
-
Rohit Vipin Mathews about 9 yearsHave a look at this developer.xamarin.com/guides/cross-platform/xamarin-forms/… it should help you pass values to Command
-
27k1 about 7 yearsmessagiingcenter is the best solution for passing page to page when tabbed pages are involved. up one vote.
-
ClintL over 5 yearsI disagree. Messaging center creates a breach of view model encapsulation and should only be used as a last resort.