Oleksandr Myronchuk
Oleksandr Myronchuk

Reputation: 360

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 ?

Upvotes: 1

Views: 854

Answers (2)

Oleksandr Myronchuk
Oleksandr Myronchuk

Reputation: 360

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>

Upvotes: 1

Ivan I
Ivan I

Reputation: 9990

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.

Upvotes: 1

Related Questions