Xamarin.Forms导航异常:点击按钮跳转至程序集浏览器而非目标页面
Xamarin.Forms导航故障修复方案
问题现象
- 在Mac版Visual Studio开发Xamarin.Forms应用时,点击导航按钮跳转至
FirstPlayer页面时,模拟器卡顿且自动打开程序集浏览器,未完成正常导航 - 同一按钮跳转至其他页面时功能正常
- 目标页面已在AppShell中注册,模拟器无错误提示
故障页面代码
XAML代码(存在语法错误)
<?xml version="1.0" encoding="UTF-8" ?> <<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="Rollthedice3._0.Views.FirstPlayer" Title="First Player"> <ContentPage.Content> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackLayout BackgroundColor="{StaticResource Accent}" VerticalOptions="FillAndExpand" HorizontalOptions="Fill"> </StackLayout> </Grid> </ContentPage.Content> </ContentPage>
C#代码
using System; using System.Collections.Generic; using Rollthedice3._0.ViewModels; using Xamarin.Forms.Xaml; using Xamarin.Forms; using System.IO; namespace Rollthedice3._0.Views { public partial class FirstPlayer : ContentPage { public FirstPlayer() { InitializeComponent(); } } }
修复步骤
修正XAML语法错误
核心问题是XAML文件开头的<<ContentPage多了一个左尖括号,导致XAML解析器无法正确处理页面结构,进而引发导航时的异常。将其修改为<ContentPage>即可。清理重建项目
- 执行
Build > Clean All清理项目缓存 - 执行
Build > Rebuild All重新编译解决方案 - 重启模拟器后测试导航功能
- 执行
验证AppShell路由配置
确保AppShell中已正确注册FirstPlayer页面的路由:Routing.RegisterRoute(nameof(FirstPlayer), typeof(FirstPlayer));导航代码需匹配路由名称,示例:
await Shell.Current.GoToAsync(nameof(FirstPlayer));检查静态资源定义
确认App.xaml中已正确定义Accent静态资源,避免页面加载时因资源缺失导致异常:<Color x:Key="Accent">#你的颜色值</Color>
内容的提问来源于stack exchange,提问作者Jace
相关产品推荐
相关产品推荐

