这个例子里面用Button作为菜单,写了很多例子,我们先把菜单和跳转写好,之后一个例子一个例子添加。

在AppShell.cs中添加

this.Navigate("MauiViews.MauiDemos.Book._03.LayoutPanels");

LayoutPanels.xaml代码

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="MauiViews.MauiDemos.Book._03.LayoutPanels"
             Title="LayoutPanels"
             HeightRequest="400" WidthRequest="252">
    <ScrollView>
        <StackLayout Margin="5" x:Name="mainStack" Spacing="5">
            <Button Text="SimpleStack"/>
            <Button Text="SimpleWrap"/>
            <Button Text="SimpleDock"/>
            <Button Text="BasicDialogBox"/>
            <Button Text="SimpleGrid"/>
            <Button Text="SplitWindow"/>
            <Button Text="DoubleSplitWindow"/>
            <Button Text="SimpleCanvas"/>
            <Button Text="SimpleInkCanvas"/>
            <Button Text="TextBoxColumn"/>
            <Button Text="LocalizableText"/>
            <Button Text="TextBox"/>
            <Button Text="ModularContent"/>
        </StackLayout>
    </ScrollView>
</ContentPage>

对应的cs文件代码

using Shares;

namespace MauiViews.MauiDemos.Book._03;

public partial class LayoutPanels : ContentPage
{
	public LayoutPanels()
	{
		InitializeComponent();
		foreach (var child in mainStack.Children)
		{
            if (child is Button btn)
            {
				btn.Clicked += Button_Clicked;
            }
        }
	}
    private void Button_Clicked(object? sender, EventArgs e)
	{
		Shell.Current.GoTo("MauiViews.MauiDemos.Book._03." + (sender as Button)?.Text, true);
    }
}

运行效果,目前还没有写对应的ContentPage代码,所以点击会提示无法跳转。

 

posted on 2025-06-14 14:45  dalgleish  阅读(5)  评论(0)    收藏  举报