你可以援引以下法典:
• 创建MAUI项目,然后将LoginPage和NewPage1增到根名录,并在LoginViewModel内部添加观点模块:
伐木 页: 1
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp5.LoginPage"
Title="LoginPage">
<VerticalStackLayout>
<Label
Text="Welcome to .NET MAUI!"
VerticalOptions="Center"
HorizontalOptions="Center" />
<Button Text="Login"
Command="{Binding LoginCommand}"/>
</VerticalStackLayout>
</ContentPage>
public partial class LoginPage : ContentPage
{
public LoginPage()
{
InitializeComponent();
this.BindingContext = new LoginViewModel();
}
}
LoginViewModel:
public class LoginViewModel
{
public Command LoginCommand { get; }
public LoginViewModel () { LoginCommand = new Command (OnLoginClicked); }
private async void OnLoginClicked (object obj)
{
await Shell.Current.GoToAsync ($"//{nameof(MainPage)}");
}
}
为返回LoginPage而增加一个县:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MauiApp5.MainPage">
<ScrollView>
<VerticalStackLayout
Spacing="25"
Padding="30,0"
VerticalOptions="Center">
.....
<Button Text="LoginPage" Clicked="Button_Clicked"/>
</VerticalStackLayout>
</ScrollView>
</ContentPage>
public partial class MainPage: ContentPage
{
int count = 0;
public MainPage()
{
InitializeComponent();
}
....
private void Button_Clicked(object sender, EventArgs e)
{
Shell.Current.GoToAsync($"//{nameof(LoginPage)}");
}
}
AppShell.xaml:
<Shell
x:Class="MauiApp5.AppShell"
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MauiApp5"
Shell.FlyoutBehavior="Disabled">
<ShellContent Title="LoginPage" Route="LoginPage" ContentTemplate="{DataTemplate local:LoginPage}" Shell.TabBarIsVisible="False"/>
<FlyoutItem >
<ShellContent Title="Home" Route="MainPage" ContentTemplate="{DataTemplate local:MainPage}" />
<ShellContent Title="NewPage1" Route="NewPage1" ContentTemplate="{DataTemplate local:NewPage1}" />
</FlyoutItem>
</Shell>
这里是effect。