简体   繁体   English

Shell 导航

[英]Shell Navigation

I have a login/register page, and I want to be able to navigate between them routing navigation (//LoginPage) like in a web app.我有一个登录/注册页面,我希望能够在它们之间导航路由导航 (//LoginPage),就像在 web 应用程序中一样。 I cant really make it work.我真的不能让它工作。 I have built a AppShell.xaml page and i have registered my LoginPage and RegisterPage in App.xaml.cs (I have tried registering them in AppShell.xaml.cs too) and I have a viewmodel that has 2 functions for navigating between login and register pages but when I click on the button to navigate I get this我已经构建了一个 AppShell.xaml 页面,并且我已经在 App.xaml.cs 中注册了我的 LoginPage 和 RegisterPage (我已经尝试在 AppShell.xaml2 中注册它们)注册页面,但是当我单击按钮导航时,我得到了这个

System.NullReferenceException: 'Object reference not set to an instance of an object.' System.NullReferenceException:“对象引用未设置为 object 的实例。”

Here is my view-model这是我的视图模型

namespace Appointments.ViewModels
{
    public class RegLogViewModel : BaseViewModel
    {
        public AsyncCommand GoToRegisterCommand { get; }
        public AsyncCommand GoToLoginCommand { get; }
        public RegLogViewModel()
        {
            GoToLoginCommand = new AsyncCommand(GoToLogin);
            GoToRegisterCommand = new AsyncCommand(GoToRegister);
        }
async Task GoToRegister()
        {
            await Shell.Current.GoToAsync($"//{ nameof(RegisterPage)}");
        }

        async Task GoToLogin()
        {
            await Shell.Current.GoToAsync($"//{ nameof(LoginPage)}");
        }   
}

My AppShell.xaml我的 AppShell.xaml

<Shell xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Appointments.AppShell"
             xmlns:views="clr-namespace:Appointments.Views">

     //// I HAVE TRIED WITH AND WITHOUT THIS  SHELLCONTENT\\\\\

    <ShellContent Route="LoginPage" ContentTemplate="{DataTemplate views:LoginPage}"/>
    <ShellContent Route="RegisterPage" ContentTemplate="{DataTemplate views:RegisterPage}"/>
</Shell>

My AppShell.xaml.cs我的 AppShell.xaml.cs

namespace Appointments
{

    public partial class AppShell : Shell
    {
        public AppShell()
        {
            InitializeComponent();
        }
    }
}

My App.xaml.cs我的应用程序.xaml.cs

namespace Appointments
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
 
            MainPage = new LoginPage();
        }
    }
}

And Login.xaml并登录.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:viewmodels="clr-namespace:Appointments.ViewModels"
             x:DataType="viewmodels:RegLogViewModel"
             x:Class="Appointments.Views.LoginPage">

    <ContentPage.Content>
        <StackLayout>
            <Label 
                Text="Login Page!"
                FontSize="Title"
                VerticalOptions="CenterAndExpand" 
                HorizontalOptions="CenterAndExpand" />
            
            <Label
                Text="{Binding Name}"/>

            <Entry
                Text="{Binding Name}"/>

            <Button
                Text="Go To Registration"
                Command="{Binding GoToRegisterCommand}"/>
            
            <ActivityIndicator IsRunning="{Binding IsBusy, Mode=OneWay}"/>
        </StackLayout>

    </ContentPage.Content>
</ContentPage>

The problem was that in App.xaml.cs I have问题是在 App.xaml.cs 我有

MainPage = new LoginPage();

but I had to use但我不得不使用

MainPage = new AppShell();

it will work but now I don't understand how the app knows to open my LoginPage first and not RegisterPage or any other page它会工作,但现在我不明白应用程序如何知道首先打开我的 LoginPage 而不是 RegisterPage 或任何其他页面

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

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