简体   繁体   English

如何在Xamarin.Forms中将参数从一个页面传递到另一个页面?

[英]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.

相关问题 在Xamarin.Forms中将字符串从一页传递到另一页 - Passing a string from one page to another in Xamarin.Forms 在 xamarin.forms 中将标签从一页添加到另一页的列表视图 - Adding labels to a listview from one page to another in xamarin.forms 如何从Xamarin.forms中的按钮单击从一个页面移动到另一个页面 - how to move from one page to another from button click in Xamarin.forms 我有一个关于 Xamarin.Forms 导航的问题。 如何将变量值从一页传递到另一页? - I have a question regarding Xamarin.Forms Navigation. How to pass variable values from one page to other? hybridWebView 页面从 Xamarin.Forms 中的 Javascript 打开另一个 ContentPage - hybridWebView page open another ContentPage from Javascript in Xamarin.Forms 在使用 Xamarin.Essentials 抖动检测事件时,未在 Xamarin.Forms 中从一页导航到另一页 - Not navigating from one page to another page in Xamarin.Forms while using using Xamarin.Essentials shake detect event Xamarin.Forms Prism-使用导航服务从一个详细信息页面导航到另一个 - Xamarin.Forms Prism - Using navigation service to navigate from one detail page to another 如何在 Xamarin.Forms 页面中将 Button 作为 CommandParameter 从 XAML 传递? - How do I pass the Button as CommandParameter from XAML in a Xamarin.Forms Page? 如何在 APP.xaml.cs 代码中进行更改以将参数从一个页面传递到 Xamarin 中的另一个页面? - How to make change in APP.xaml.cs code to pass a parameter from one page to another in Xamarin? 如何在 Xamarin Forms 中将参数从一个页面传递到另一个页面? 以及如何绑定视图(页面)及其ViewModel并初始化ViewModel object? - How to pass parameters from one Page to another in Xamarin Forms? and How to Bind View (Page) and its ViewModel and initialize ViewModel object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM