Xamarin.Forms:如何将 NavigationPage XAML 类添加到另一个 XAML 类?

标签 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">
  <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?

Example for NavigationPage in XAML

关于Xamarin.Forms:如何将 NavigationPage XAML 类添加到另一个 XAML 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36705856/

相关文章:

ios - Xamarin ios 如何在编辑时制作一个自适应大小的 UITableViewCell

android - 如何复制并粘贴到 Xamarin Android Player?

c# - 隐藏标题选项卡式导航页 Xamarin/FreshMvvm

android - Xamarin.Forms 获取 Mono.Linker.MarkException : Error processing method when compiling in release mode

xaml - Xamarin.Forms Xaml 背景图像

visual-studio-2012 - 如何使用 xamarin 在 Visual Studio 中构建 Mac 主机

xaml - 在 XAML 命名空间中找不到类型

xamarin.forms - Azure Active Directory 中的应用程序注册问题,应用程序不在租户中?

Xamarin Scrollview 滚动条不可见

ios - iOS 上的 Xamarin.Forms AbsoluteLayout 问题