福州网站seo公司,信誉好的做pc端网站,51link友链,广州专业做网站建设1.概要用过WPF的小伙伴一般都用过Prism#xff0c;Prism里面的导航概念在MAUI中也有类似的概念#xff0c;在MAUI中是直接集成在框架中我们不需要安装任何其他的nuget包。直接使用Navigation对象即可,通常在移动平台中使用的更多#xff0c;桌面程序中。我们先来看看微软官方…1.概要用过WPF的小伙伴一般都用过PrismPrism里面的导航概念在MAUI中也有类似的概念在MAUI中是直接集成在框架中我们不需要安装任何其他的nuget包。直接使用Navigation对象即可,通常在移动平台中使用的更多桌面程序中。我们先来看看微软官方是如何定义的如下面代码所示。public interface INavigation{IReadOnlyListPage ModalStack { get; }IReadOnlyListPage NavigationStack { get; }void InsertPageBefore(Page page, Page before);TaskPage PopAsync();TaskPage PopAsync(bool animated);TaskPage PopModalAsync();TaskPage PopModalAsync(bool animated);Task PopToRootAsync();Task PopToRootAsync(bool animated);Task PushAsync(Page page);Task PushAsync(Page page, bool animated);Task PushModalAsync(Page page);Task PushModalAsync(Page page, bool animated);void RemovePage(Page page);}我这里是直接找到了Navigation的上层接口的定义。那我们来看看几个基本的介绍。名称类型说明PopToRootAsync方法导航到根目录。PopAsync方法导航到上一个页面。PushAsync方法导航到指定页面。2.详细内容接下来演示一下基本用法。防止大家被绕晕这里整理了一下导航图如下MainPage.xaml代码?xml version1.0 encodingutf-8 ?
ContentPage xmlnshttp://schemas.microsoft.com/dotnet/2021/mauixmlns:xhttp://schemas.microsoft.com/winfx/2009/xamlx:ClassMauiApp1.MainPageScrollViewVerticalStackLayout Spacing25 Padding30,0 VerticalOptionsCenterButton x:NameBtnPage1TextPage1ClickedBtnPage1_ClickedHorizontalOptionsCenter /Button x:NameBtnPage2TextPage2ClickedBtnPage2_ClickedHorizontalOptionsCenter //VerticalStackLayout/ScrollView
/ContentPagenamespace MauiApp1;public partial class MainPage : ContentPage
{public MainPage()
{InitializeComponent();}private void BtnPage2_Clicked(object sender, EventArgs e)
{Navigation.PushAsync(new NewPage2());}private void BtnPage1_Clicked(object sender, EventArgs e)
{Navigation.PushAsync(new NewPage1());}
}Page1.xaml代码?xml version1.0 encodingutf-8 ?
ContentPage xmlnshttp://schemas.microsoft.com/dotnet/2021/mauixmlns:xhttp://schemas.microsoft.com/winfx/2009/xamlx:ClassMauiApp1.NewPage1TitleNewPage1VerticalStackLayoutLabel TextWelcome to .NET MAUI!VerticalOptionsCenter HorizontalOptionsCenter /Button x:NameBtnNextTextNetxt PageClickedBtnNext_ClickedHorizontalOptionsCenter /Button x:NameBtnGobackTextGo backClickedBtnGoback_ClickedHorizontalOptionsCenter //VerticalStackLayout
/ContentPagenamespace MauiApp1;public partial class NewPage1 : ContentPage
{public NewPage1()
{InitializeComponent();
}private void BtnNext_Clicked(object sender, EventArgs e)
{Navigation.PushAsync(new NewPage2());}private void BtnGoback_Clicked(object sender, EventArgs e)
{Navigation.PopAsync();}
}Page2.xaml代码?xml version1.0 encodingutf-8 ?
ContentPage xmlnshttp://schemas.microsoft.com/dotnet/2021/mauixmlns:xhttp://schemas.microsoft.com/winfx/2009/xamlx:ClassMauiApp1.NewPage2TitleNewPage2VerticalStackLayoutLabel TextWelcome to .NET MAUI!VerticalOptionsCenter HorizontalOptionsCenter /Button x:NameBtnGobackTextGo to rootClickedBtnGoback_ClickedHorizontalOptionsCenter //VerticalStackLayout
/ContentPagenamespace MauiApp1;public partial class NewPage2 : ContentPage
{public NewPage2()
{InitializeComponent();
}private void BtnGoback_Clicked(object sender, EventArgs e)
{Navigation.PopToRootAsync();}
}