下面的代码工作正常:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.MainTabbedPage">
<NavigationPage Title="asdfee">
<x:Arguments>
<ContentPage Title="asdf">
<Label Text="asdfawwer"/>
</ContentPage>
</x:Arguments>
</NavigationPage>
</TabbedPage>
但是如果我想将 NavigationPage 放在一个单独的 XAML 文件中,在不同的目录中,这是行不通的。例如:
<?xml version="1.0" encoding="utf-8" ?>
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.MainTabbedPage"
xmlns:layouts="clr-namespace:MyApp.Layouts">
<layouts:MyNavigationPage Title="asdfee"/>
</TabbedPage>
MyNavigationPage.xaml 位于 Layouts 目录中,但具有与之前代码完全相同的代码:
<?xml version="1.0" encoding="utf-8" ?>
<NavigationPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="MyApp.Layouts.MyNavigationPage">
<x:Arguments>
<ContentPage Title="asdf">
<Label Text="asdfawwer"/>
</ContentPage>
</x:Arguments>
</NavigationPage>
当我这样做时,它会抛出以下错误:
System.InvalidOperationException: NavigationPage must have a root Page before being used. Either call PushAsync with a valid Page, or pass a Page to the constructor before usage.
我之前遇到过同样的错误,当时所有代码都像第一个示例 中的代码,但我没有 <x:Arguments>
在 NavigationPage 的 child 之前。所以我猜是子创建顺序有问题。
另外,如果我使用 ContentPage 执行第二种方法,它也有效。
我是做错了什么还是应该填写错误报告?
有什么想法吗?
谢谢
最佳答案
@Dpedrinha
这是可能的。
Xamarin.Forms: How to add a NavigationPage XAML class to another XAML class?
关于Xamarin.Forms:如何将 NavigationPage XAML 类添加到另一个 XAML 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705856/