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

Dra*_*gos 5 c# xaml mvvm xamarin xamarin.forms

当我通过MVVM模式将表单中的按钮按到另一个表单时,我想发送一个值.

这是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>
Run Code Online (Sandbox Code Playgroud)

这是我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;                
        }
Run Code Online (Sandbox Code Playgroud)

跳跃工作.如何将"CommandParameter"发送到"Page2"?

谢谢,Dragos

Jas*_*son 9

最简单的方法是将值作为参数传递给Page2的构造函数.或者您可以在Page2上创建公共属性,并在创建后为其分配值.

await _navigation.PushAsync(new Page2(argument_goes_here)); // HERE
Run Code Online (Sandbox Code Playgroud)