簡體   English   中英

如何在Xamarin.Forms中將參數從一個頁面傳遞到另一個頁面?

[英]How to pass a parameter from one Page to another Page in 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>

這是我的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;                
        }

跳躍工作。 如何將“CommandParameter”發送到“Page2”?

謝謝,Dragos

最簡單的方法是將值作為參數傳遞給Page2的構造函數。 或者您可以在Page2上創建公共屬性,並在創建后為其分配值。

await _navigation.PushAsync(new Page2(argument_goes_here)); // HERE

使用SQLite存儲對象並在新的ViewModel中實例化它,或使用Xamarin.Forms中內置的消息中心間接在ViewModel之間傳遞數據。

Jason的方法可行但是親自將數據傳遞到視圖,到另一個視圖然后返回到視圖模型不是我想要做的事情。

SQLite文檔

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/databases/

消息中心文檔

http://developer.xamarin.com/guides/cross-platform/xamarin-forms/messaging-center/

將參數傳遞給ViewModel並獲取結果的好例子: 如何將條目參數傳遞給Xamarin MVVM viewmodel

從ViewModel導航到下一頁(View):

等待Xamarin.Forms.Application.Current.MainPage.Navigation.PushAsync(new MyView());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM