0

我正在尝试新发布的 xamarin shell。基本上,我正在尝试如何将路由映射到动态生成的页面。我注意到RegisterRoute有一个需要类型对象的重载RouteFactory。我为此创建了一个演示类:

class NavMan : RouteFactory
{
    private readonly string title;

    public NavMan(string title) : base()
    {
        this.title = title;
    }

    public override Element GetOrCreate()
    {
        return new ContentPage
        {
            Title = "tit: " + title,
            Content = new Label { Text = title }
        };
    }
}

现在,在App.cs我创建一个注册演示路线:

Routing.RegisterRoute("batman", new NavMan("I am batman"));

我尝试了几种设置Shell对象的变体。我大多得到钝的空指针,需要猜测要更改的内容。

截至目前,我App的类的构造函数具有以下代码:

var sea = new Shell();
var theItem = new FlyoutItem
{
    IsEnabled = true,
    Route = "batman"
};
sea.Items.Add(theItem);
sea.CurrentItem = theItem;
MainPage = sea;

这也给了我一个钝的空指针。我现在正在尝试的只是显示路线“蝙蝠侠”的页面。即使是弹出或选项卡也不是强制性的。

更新

虽然不是目的,但我至少让应用程序打开了以下内容:

var sea = new Shell();
Routing.RegisterRoute("batman", new NavMan("I am batman"));
var theItem = new ShellContent {
    Title = "hello 20",
    Route = "batman2",
    Content = new ContentPage {
        Content = new Button {
            Text = "Something shown",
            Command = new Command(async () => await Shell.Current.GoToAsync("//batman"))
        }
    }
};
sea.Items.Add(theItem);
sea.CurrentItem = theItem;
MainPage = sea;

单击按钮时,它现在向我显示以下异常并且从不调用该GetOrCreate函数。

System.Exception: <获取异常详细信息超时>

更新 3

基本上,我正在寻找一种将路由绑定到 ShellContent 的方法,它只是显示我提到的路由属性并在路由中显示一个页面。我需要提及路线并提及页面的内容模板是没有意义的。路由已映射到页面。

4

0 回答 0