.NET MAUI 页面导航与数据存储全解析
1. 导航基础:BindingContext 和路由
在使用 .NET MAUI 进行页面导航时,首先要确保将 XAML 与 ViewModel 绑定。这可以通过在代码隐藏文件中设置 BindingContext
来实现。例如,在使用 GoToAsync
静态方法进行页面导航时,需要传递页面名称作为参数。但问题是,程序如何知道像 buddydetailspage
这样的字符串对应的实际页面呢?答案就在于路由。
在 .NET MAUI 中,路由的注册是在 AppShell.xaml.cs
文件中完成的。以下是一个路由注册的示例:
public partial class AppShell : Shell
{
public AppShell()
{
InitializeComponent();
Routing.RegisterRoute("buddiespage", typeof(BuddiesPage));
Routing.RegisterRoute("buddydetailspage", typeof(BuddyDetailsPage));
Routing.RegisterRoute("aboutpage", typeof(AboutPage));
Routing.RegisterRoute("preferencespage", typeof(Preferenc