Xamarin Forms:如何将NavigationPage XAML类添加到另一个XAML类?
以下代码可以正常工作:Xamarin Forms:如何将NavigationPage XAML类添加到另一个XAML类?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,以下代码可以正常工作: <?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"> <Navigatio
<?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>
位于Layouts目录中的MyNavigationPage.xaml,但代码与前一代码完全相同:
<?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必须具有根页面
在使用之前。使用有效页面调用PushAsync,或传递
使用前请将页面转到构造函数
我以前也遇到过同样的错误,当时所有的代码都与第一个示例中的类似,而导航页面的子项之前没有
。所以我猜孩子的创建顺序有问题
另外,如果我使用ContentPage执行第二种方法,它也会起作用
我是做错了什么,还是应该填写错误报告
有什么想法吗
谢谢我不相信你能用XAML创建导航页面。我总是从代码开始:
var nav = new NavigationPage(new MyContentXaml());
@Dpedrinha
这是可能的
Forms:如何将NavigationPage XAML类添加到另一个XAML类
通过添加
类似:(注意,我为CurrentPageChanged添加了一个事件)
您要导航到的页面应该是ContentPage。将标题添加到页面中
<?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="MyApp.Layouts.MyNavigationPage"
Title="asdf">
<Label Text="asdfawwer"/>
</ContentPage>
我不喜欢这种解决方法。要么所有内容都用C#编写,要么所有布局都用xaml编写。它在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="MyApp.Layouts.MyNavigationPage"
Title="asdf">
<Label Text="asdfawwer"/>
</ContentPage>