【问题标题】:Xamarin.Forms: Can I embed one ContentPage or ContentView into another ContentPageXamarin.Forms:我可以将一个 ContentPage 或 ContentView 嵌入到另一个 ContentPage
【发布时间】:2019-06-26 04:00:06
【问题描述】:

我的 Xamarin 项目中有多个 ContentPage xaml 文件。我想在每个 ContentPage 中嵌入一段共享的 xaml。 xaml 的共享部分没有什么特别之处(它不需要做任何特定于平台的事情)。难道不应该像在 ContentPage 的 xaml 中嵌入标签以包含共享的 xaml 文件一样简单吗?有人能指出我正确的方向吗?

【问题讨论】:

    标签: xamarin.forms content-pages


    【解决方案1】:

    非常感谢 IdoT,它确实对我有用,但是在添加了一些行之后。 因为这将有助于制作模板/自定义控件/子表单,从而在 Xamarin.Forms 中轻松添加/共享。

    这是我根据您的建议完成的全部工作,因此可以与其他人一起使用:

    HeaderNavigationBar.xaml

    <?xml version="1.0" encoding="utf-8" ?>
    <StackLayout xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="App9.MVC.Views.HeaderNavigationBar"
                 Orientation="Horizontal"
                 HorizontalOptions="FillAndExpand"
                 Padding="10"
                 ackgroundColor="White">
    
        <Button Text="Internal 1" />
        <Button Text="Internal 2" />
    </StackLayout>
    

    如您所见,添加:

    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    

    HeaderNavigationBar.cs中,它被声明为StackLayout

    HeaderNavigationBar.cs

    using Xamarin.Forms;
    
    namespace App9.MVC.Views
    {
        public partial class HeaderNavigationBar : StackLayout
        {
            public HeaderNavigationBar()
            {
                InitializeComponent();
            }
        }
    }
    

    然后对于将保存它/显示它的页面:

    MainView.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"
                 xmlns:common="clr-namespace:App9.MVC.Views;assembly=App9"
                 x:Class="App9.MVC.Views.MainView">
    
        <StackLayout Padding="0,0,0,20">
            <common:HeaderNavigationBar>
                <Button Text="External 1" />
            </common:HeaderNavigationBar>
    
            <Button Text="Test Button 1
                    x:Name="btnPage1"
                    Clicked="btnPage1_clicked" />
        </StackLayout>
    </ContentPage>
    

    如您所见,命名空间有完整路径,在MainView:

    xmlns:common="clr-namespace:App9.MVC.Views;assembly=App9"
    

    另外,您会注意到有一个名为External 1 的按钮,它也会显示在 内部按钮,因为控件是 StackLayout,所以可以处理添加更多控件。

    截图:

    也适用于另一个页面内的页面:

    再次感谢 IdoT。

    【讨论】:

      【解决方案2】:

      您可以将ContentPage 的父子代(例如,包装所有子代的StackLayout),将其放入外部xaml 文件中, 然后将该组件包含在您的每个 ContentPages 中。

      外部 xaml 文件将是 StackLayout 类型,而不是 ContentPage。

      编辑 - 添加代码示例:

      让我们添加一个标题 StackLayout:我们在类后面添加一个代码:

      public partial class HeaderNavigationBar 
      {
          public HeaderNavigationBar()
          {
              InitializeComponent();
          }
      }
      

      然后添加 XAML 代码:

      <StackLayout x:Class="HeaderNavigationBar"
                   Orientation="Horizontal"
                   HorizontalOptions="FillAndExpand"
                   Padding="10"
                   BackgroundColor="White">
      
          <Image Source="burger_icon"
                 HorizontalOptions="StartAndExpand"
                 Aspect="AspectFit">
              <Image.GestureRecognizers>
                  <TapGestureRecognizer Command="{Binding SlideNavigationDrawerCommand}" />
              </Image.GestureRecognizers>
          </Image>
      </StackLayout>
      

      最后,在您要重用组件的页面中 - 添加此引用:&lt;HeaderNavigationBar /&gt;

      【讨论】:

      • 谢谢。那会让我更亲近。您能告诉我将外部 xaml 文件包含到我的内容页面中的语法是什么吗?
      • 越来越近了。当我尝试运行它时,它在 xaml 文件中给了我这个错误:“x 是未声明的命名空间。”有什么想法吗?
      • 您需要在内容页面标题减速中添加如下内容:xmlns:common="clr-namespace:Consumer.Mobile.Views.Common;assembly=Consumer.Mobile" 然后声明您的参考像这样:
      • 还是没有运气。内容页面出现“Type common:HeaderNavigation not found in xmlns clr-namespace...”等错误。
      • 这里没有官方文档吗?
      猜你喜欢
      • 2022-01-20
      • 2017-03-04
      • 2020-11-28
      • 1970-01-01
      • 2018-08-20
      • 1970-01-01
      • 2023-02-14
      • 2020-04-19
      • 1970-01-01
      相关资源
      最近更新 更多