简体   繁体   English

Xamarin 表单包含 xaml 文件

[英]Xamarin Forms include xaml file

I have a xaml file where I wrote the title.我有一个写标题的 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>

I want to use this xaml file as a header in other xaml files.我想将此 xaml 文件用作其他 xaml 文件中的标头。 Here's an example.这是一个例子。 I'm trying to include it to another xaml file:我正在尝试将它包含到另一个 xaml 文件中:

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

The "template" tag is now used to include file: “模板”标签现在用于包含文件:

<?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>

The visual studio shows the error: "Error XLS0501 The property 'Content' is set more than once." Visual Studio 显示错误:“错误 XLS0501 多次设置属性‘内容’。”

How can I include content from one xaml file to another xaml file ?如何将一个 xaml 文件中的内容包含到另一个 xaml 文件中?

In this example ContentPage has two elements - template:Header and StackLayout and only one is allowed.在此示例中ContentPage有两个元素 - template:HeaderStackLayout并且只允许使用一个。

That is the cause of this problem.这就是这个问题的原因。 I haven't gone deeper in evaluating if there are further problems, but in the case they exist and you can't resolve them you should ask a new question.我没有更深入地评估是否还有其他问题,但是如果它们存在并且您无法解决它们,您应该提出一个新问题。

I fixed it我修好了它

To fix this, you need to insert the <template:Header/> tag inside the <StackLayout> tag.要解决此问题,您需要在<StackLayout>标签内插入<template:Header/> <StackLayout>标签。

Below is the full code:下面是完整的代码:

 <?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM