[英]How to pass a parameter from one Page to another Page in Xamarin.Forms?
I want to send a value when I press a button in a form to another form through the MVVM pattern . 当我通过MVVM模式将表单中的按钮按到另一个表单时,我想发送一个值。
This is the XAML file 这是XAML文件
<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. 这是我的
ModelView class
,我将其重定向到另一个表单。
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" ?
如何将“CommandParameter”发送到“Page2”?
Thanks, Dragos 谢谢,Dragos
The easiest approach would be to pass the value as a parameter to the constructor of Page2. 最简单的方法是将值作为参数传递给Page2的构造函数。 Or you could create a public property on Page2 and assign the value to it after you create it.
或者您可以在Page2上创建公共属性,并在创建后为其分配值。
await _navigation.PushAsync(new Page2(argument_goes_here)); // HERE
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. 使用SQLite存储对象并在新的ViewModel中实例化它,或使用Xamarin.Forms中内置的消息中心间接在ViewModel之间传递数据。
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. Jason的方法可行但是亲自将数据传递到视图,到另一个视图然后返回到视图模型不是我想要做的事情。
SQLite Documentation SQLite文档
http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/ 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/ http://developer.xamarin.com/guides/cross-platform/xamarin-forms/messaging-center/
Good example of passing parameters to ViewModel and getting result back: How do I pass entry parameters to Xamarin MVVM viewmodel 将参数传递给ViewModel并获取结果的好例子: 如何将条目参数传递给Xamarin MVVM viewmodel
To navigate from ViewModel to next page (View): 从ViewModel导航到下一页(View):
await Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new MyView()); 等待Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new MyView());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.