简体   繁体   中英

Xamarin Forms include xaml file

I have a xaml file where I wrote the title.

<?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. Here's an example. I'm trying to include it to another xaml file:

I added a namespace: 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."

How can I include content from one xaml file to another xaml file ?

In this example ContentPage has two elements - template:Header and StackLayout and only one is allowed.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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