You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI C#中Navigation.PushAsync无法跳转页面问题求助

MAUI页面导航崩溃问题解决

在MAUI C#项目中,多次尝试点击按钮跳转至Fluid页面均失败,耗时3小时排查。调试确认能进入Button_Clicked方法,但执行Navigation.PushAsync时Pixel安卓模拟器崩溃,尝试Navigation.PopAsync同样无效。

MainPage的XAML代码

<ScrollView>
    <Grid x:Name="Container" BackgroundColor="{StaticResource Primary}" RowDefinitions="100,70,300,90,90,90,250" ColumnDefinitions="*,*,*,*,*,*" Margin="0,0,0,10">
        <Frame  VerticalOptions="Start" HorizontalOptions="FillAndExpand" BackgroundColor="{StaticResource Secondary}" Grid.Row="0" Grid.ColumnSpan="6" HeightRequest="90">
            <Label Text="مركز إصلاح واجن لصيانة السيارات " FontSize="Large" FontAttributes="Bold" HorizontalTextAlignment="Center" TextColor="Black" />
        </Frame>
        <Frame Grid.Row="1" Grid.ColumnSpan="6" Margin="0,0,0,0" BackgroundColor="{StaticResource TextColor}">
        <Label Text="menu" FontSize="Large" LineBreakMode="TailTruncation" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" HeightRequest="70" Margin="0,5,0,5" />
         </Frame>
        <Frame Grid.Row="2" Grid.ColumnSpan="6" BackgroundColor="{StaticResource Secondary}" Margin="0,5,0,5" >
            <Label Text="pic slider" FontSize="Large" FontAttributes="Bold" HorizontalTextAlignment="Center" TextColor="Black" VerticalTextAlignment="Center"/>
        </Frame>
           <Button Text="تغيير الزيوت "   BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Medium"  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" HeightRequest="80" WidthRequest="170" Clicked="Button_Clicked"/>
           <Button Text="تغيير الإطارات "  BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Medium"  Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="3" HeightRequest="80" WidthRequest="170" />
           <Button Text="تغيير البطارية " BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Medium" VerticalOptions="Center"  HorizontalOptions="Center"  Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="3" HeightRequest="80" WidthRequest="170"/>
           <Button Text="الصيانة الدورية" BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Medium" VerticalOptions="Center"  HorizontalOptions="Center"  Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="3" HeightRequest="80" WidthRequest="170"/>
           <Button Text="الصيانات الاخرى " BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Medium" VerticalOptions="Center"  HorizontalOptions="Center"  Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="3" HeightRequest="80" WidthRequest="170"/>
           <Button Text="test"            BackgroundColor="Red" TextColor="White" FontAttributes="Bold" FontSize="Medium" VerticalOptions="Center"  HorizontalOptions="Center"  Grid.Row="5" Grid.Column="3" Grid.ColumnSpan="3" HeightRequest="80" WidthRequest="170"/>
    <Frame Grid.Row="6" Grid.Column="0" Grid.ColumnSpan="6" BackgroundColor="{StaticResource Secondary}">
        <Label Text="footer" FontSize="Large" FontAttributes="Bold" HorizontalTextAlignment="Center" TextColor="Black" VerticalTextAlignment="Center"/>
    </Frame>
     </Grid>
</ScrollView>

MainPage的C#代码

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }
    private void Button_Clicked(object sender, EventArgs e)
    {
        Navigation.PushAsync(new Fluid());
    }
}

解决方案

修改App.xaml.cs,将MainPage包装为NavigationPage即可正常导航:

打开App.xaml.cs文件,在构造函数中创建MainPage实例并包裹在NavigationPage中,将App类的MainPage属性设置为该NavigationPage。修改后的代码如下:

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        // Create a new NavigationPage with MainPage as the root page
        MainPage = new NavigationPage(new MainPage());
    }
}

修改后,MainPage作为NavigationPage的根页面,即可正常使用Navigation.PushAsync进行页面导航。

内容的提问来源于stack exchange,提问作者Mohammed Algheathi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 21:47:49