繁体   English   中英

Xamarin 导航页面不工作 (MVVM)

[英]Xamarin navigating pages not working ( MVVM )

主页.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelRecordApp.MainPage"
             xmlns:View="clr-namespace:TravelRecordApp.View">

    <View:MainView/>

</ContentPage>

主视图.xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelRecordApp.View.MainView"
             xmlns:ViewModel="clr-namespace:TravelRecordApp.ViewModel">

    <ContentView.BindingContext>
        <ViewModel:MainViewModel/>
    </ContentView.BindingContext>  
    
        <StackLayout ..>
            <Entry ../>
            <Entry ../>
            <Button Command="{Binding LoginCommand}"/>
        </StackLayout>
</ContentView>

app.xaml.cs

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage());
    //Also Tried MainPage = new MainPage();
}

MainViewModel.cs

public void Login()
{
    bool isEmailEmpty = string.IsNullOrEmpty(EmailEntry);
    bool isPasswordEmpty = string.IsNullOrEmpty(PasswordEntry);

    if (isEmailEmpty || isPasswordEmpty)
    {
        
    }
    else
    {
        Navigation.PushAsync(new HomePage());
    }
}

首页.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="TravelRecordApp.View.HomePage">
    <StackLayout>
            <Label Text="Hello, this is main page" />
        </StackLayout>
</ContentPage>

问题是:

当我调试代码时,我可以看到它到达Navigation.PushAsync(new HomePage()); 单击按钮后,但此后没有任何反应。 它没有打开主页。

  1. 正如杰森所说,您可以尝试使用App.Current.MainPage.Navigation.PushAsync(new HomePage());

  2. 使用“Xamarin.Forms Shell”导航是在页面之间导航的好方法。 详情请参考: https://learn.microsoft.com/en-us/xamarin/xamarin.forms/app-fundamentals/shell/navigation

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM