Xamarin.form页面导航在mvvm中

Wal*_*had 10 c# xaml cross-platform mvvm xamarin.forms

我正在开发xamarin.form跨平台应用程序,我希望在按钮点击时从一个页面导航到另一个页面.正如我Navigation.PushAsync(new Page2());在ViewModel中所做的那样,因为它只能在Code-Behid文件中使用.请建议任何方式这样做?

这是我的观点:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Calculator.Views.SignIn"
             xmlns:ViewModels="clr-namespace:Calculator.ViewModels;assembly=Calculator">

    <ContentPage.BindingContext>
        <ViewModels:LocalAccountViewModel/>
    </ContentPage.BindingContext>

    <ContentPage.Content>

            <StackLayout>
                <Button Command="{Binding ContinueBtnClicked}"></Button>

            </StackLayout>


    </ContentPage.Content>
</ContentPage>
Run Code Online (Sandbox Code Playgroud)

这是我的ViewModel:

 public class LocalAccountViewModel : INotifyPropertyChanged
        {




            public LocalAccountViewModel()
            {
                this.ContinueBtnClicked = new Command(GotoPage2);
            }


            public void GotoPage2()
            {
                 /////

            }


            public ICommand ContinueBtnClicked
            {

                protected set;
                get;
            }

           public event PropertyChangedEventHandler PropertyChanged;


        protected virtual void OnPropertyChanges([CallerMemberName] string PropertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(PropertyName));
        }

        }
Run Code Online (Sandbox Code Playgroud)

Tai*_*T's 20

一种方法是您可以通过VM构造函数传递导航.由于页面继承自VisualElement,因此它们直接继承该Navigation属性.

代码隐藏文件:

public class SignIn : ContentPage
{
    public SignIn(){
       InitializeComponent();
       // Note the VM constructor takes now a INavigation parameter
       BindingContext = new LocalAccountViewModel(Navigation);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在您的VM中,添加一个INavigation属性并更改构造函数以接受a INavigation.然后,您可以使用此属性进行导航:

public class LocalAccountViewModel : INotifyPropertyChanged
{

       public INavigation Navigation { get; set;}


        public LocalAccountViewModel(INavigation navigation)
        {
            this.Navigation = navigation;
            this.ContinueBtnClicked = new Command(async () => await GotoPage2());
        }


        public async Task GotoPage2()
        {
             /////
             await Navigation.PushAsync(new Page2());
        }


        ...
Run Code Online (Sandbox Code Playgroud)

请注意您应修复的代码问题:GoToPage2()必须设置该方法async并返回Task类型.此外,该命令将执行异步操作调用.这是因为你必须异步进行页面导航!

希望能帮助到你!

  • 我很抱歉 - 但这绝对打破了所有MVVM/MVC/MV无论什么规则....你的ViewModel永远不应该创建一个视图 - 反过来说.所有这些概念的目标是将视图与模型分开,因此视图是可交换的,在您的示例中并非如此.想象一下为View,ViewModel和Model做单独的项目(真正的*.csproj文件),你会发现问题.来自WPF的背景我来到这里看看Xamarin家伙是如何做到这一点的,直到现在我对我发现的概念并不高兴...会继续寻找! (7认同)

Ale*_*aro 14

一个简单的方法是

this.ContinueBtnClicked = new Command(async()=>{

    await Application.Current.MainPage.Navigation.PushAsync(new Page2());
});
Run Code Online (Sandbox Code Playgroud)