【问题标题】:Xamarin Forms include xaml fileXamarin 表单包含 xaml 文件
【发布时间】:2020-03-19 22:25:33
【问题描述】:

我有一个写标题的 xaml 文件。

<?xml version="1.0" encoding="UTF-8"?>                                                    
<ContentView xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="TeenageClubWallet.Templates.Header">
<StackLayout>
    <Label Text="Header!" />
</StackLayout>

我想将此 xaml 文件用作其他 xaml 文件中的标头。 这是一个例子。我正在尝试将其包含到另一个 xaml 文件中:

我添加了一个命名空间:xmlns:template="clr-namespace:TeenageClubWallet.Templates"

“模板”标签现在用于包含文件:

<?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:d="http://xamarin.com/schemas/2014/forms/design"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         mc:Ignorable="d"
         x:Class="TeenageClubWallet.Statistics"
         xmlns:template="clr-namespace:TeenageClubWallet.Templates">

<ContentPage.Content>
    <template:Header/>

    <StackLayout>
        <Label Text="Content!" />
    </StackLayout>
</ContentPage.Content>

Visual Studio 显示错误:“错误 XLS0501 属性 'Content' 设置了多次。”

如何将一个 xaml 文件中的内容包含到另一个 xaml 文件中?

【问题讨论】:

    标签: xaml xamarin xamarin.forms


    【解决方案1】:

    在此示例中,ContentPage 有两个元素 - template:HeaderStackLayout,并且只允许使用一个。

    这就是这个问题的原因。我没有更深入地评估是否还有其他问题,但是如果它们存在并且您无法解决它们,您应该提出一个新问题。

    【讨论】:

      【解决方案2】:

      我修好了

      要解决此问题,您需要在&lt;StackLayout&gt; 标记内插入&lt;template:Header/&gt; 标记。

      以下是完整代码:

      <?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:d="http://xamarin.com/schemas/2014/forms/design"
               xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
               mc:Ignorable="d"
               x:Class="TeenageClubWallet.Statistics"
               xmlns:template="clr-namespace:TeenageClubWallet.Templates">
      
      <ContentPage.Content>
          
      
          <StackLayout>
              <template:Header/>
              <Label Text="Content!" />
          </StackLayout>
      </ContentPage.Content>

      【讨论】:

      • 虽然此代码可以解决问题,including an explanation 说明如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提出问题的人。请edit您的答案添加解释并说明适用的限制和假设。
      猜你喜欢
      • 1970-01-01
      • 2023-03-04
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多