Error adding a custom view in xaml

IuliaJecanIuliaJecan USMember ✭✭
edited February 2017 in Xamarin.Forms

Hey.
I'm trying to add a simple CustomView in my xaml page. But I get this error while compiling: "Position 4:4. Property Content is null or is not IEnumerable".

The steps for adding the view are:
1. Declare the use of the namespace in the ContentPage header like this:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PCSA_Infirmiers.MyClass" xmlns:local="clr-namespace:MyNameSpace">
2. Use my CustomView in xaml like this:
<local:CustomView />

When I use xmlns:local="clr-namespace:MyNameSpace;assembly=MyNameSpace" I have the error: CustomView not found in the namespace MyNameSpace.

Can someone help me, please?

Best Answer

  • IuliaJecanIuliaJecan US ✭✭
    Accepted Answer

    Found my pb!!
    In Custom View xaml code I did not put the content of the page in a main layout, there for the error:

    The solution: add all the views in a Layout.

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PCSA_Infirmiers.HeaderCustomView">
        <ContentView.Content>
            <StackLayout>
            <Label x:Name="header" Text="OptionsTrest" TextColor="White" FontSize="50" VerticalOptions="Start" BackgroundColor="#7caad5" FontAttributes="Bold" />
                <BoxView Color="White" HeightRequest="3" HorizontalOptions="FillAndExpand" />
        </StackLayout>
        </ContentView.Content>
    </ContentView>v
    

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Post the entire Xaml

  • IuliaJecanIuliaJecan USMember ✭✭

    Here you are:

    Custom View Code:

    namespace PCSA_Infirmiers
            {
                public partial class HeaderCustomView : ContentView
                {
                    public HeaderCustomView()
                    {
                        InitializeComponent();
                    }
                }
            }
    

    Custom View xaml code:

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PCSA_Infirmiers.HeaderCustomView">
        <ContentView.Content>
            <Label x:Name="header" Text="Options" TextColor="White" FontSize="50" VerticalOptions="Start" BackgroundColor="#7caad5" FontAttributes="Bold" />
            <BoxView Color="White" HeightRequest="3" HorizontalOptions="FillAndExpand" />
        </ContentView.Content>
    </ContentView>
    

    The xaml in which I use the Custom View

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PCSA_Infirmiers.OptionsPage" xmlns:local="clr-namespace:PCSA_Infirmiers">
        <ContentPage.Resources>
            <ResourceDictionary>
                <Color x:Key="textStyleColor">White</Color>
                <Color x:Key="LightBlueColor">#7caad5</Color>
                <x:Double x:Key="labelWidth">65</x:Double>
                <!-- Buttons style -->
                <Style x:Key="GoToButton" TargetType="Button">
                    <Setter Property="FontAttributes" Value="Bold" />
                    <Setter Property="TextColor" Value="White" />
                    <Setter Property="HorizontalOptions" Value="Fill" />
                    <Setter Property="VerticalOptions" Value="Center" />
                </Style>
                <!-- Deconnection and logout buttons style -->
                <Style x:Key="ActionsButtons" TargetType="Button">
                    <Setter Property="BackgroundColor" Value="{StaticResource LightBlueColor}" />
                    <Setter Property="Font" Value="Medium" />
                    <Setter Property="FontAttributes" Value="Bold" />
                    <Setter Property="BorderWidth" Value="2" />
                    <Setter Property="BorderColor" Value="White" />
                    <Setter Property="TextColor" Value="White" />
                    <Setter Property="VerticalOptions" Value="Center" />
                    <Setter Property="HorizontalOptions" Value="FillAndExpand" />
                </Style>
                <!-- Labels style -->
                <Style x:Key="TextStyle" TargetType="Label">
                    <Setter Property="Font" Value="Medium" />
                    <Setter Property="WidthRequest" Value="{StaticResource labelWidth}" />
                    <Setter Property="MinimumWidthRequest" Value="{StaticResource labelWidth}" />
                    <Setter Property="TextColor" Value="{StaticResource textStyleColor}" />
                    <Setter Property="HorizontalOptions" Value="StartAndExpand" />
                    <Setter Property="VerticalOptions" Value="Center" />
                </Style>
                <Style x:Key="EditorTextStyle" TargetType="Editor">
                    <Setter Property="Keyboard" Value="Text" />
                    <Setter Property="TextColor" Value="{StaticResource textStyleColor}" />
                    <Setter Property="HorizontalOptions" Value="CenterAndExpand" />
                    <Setter Property="VerticalOptions" Value="Start" />
                    <Setter Property="BackgroundColor" Value="Transparent" />
                </Style>
                <Style x:Key="RowsContainersStyle" TargetType="StackLayout">
                    <Setter Property="Orientation" Value="Horizontal" />
                    <Setter Property="HorizontalOptions" Value="StartAndExpand" />
                    <Setter Property="VerticalOptions" Value="Center" />
                    <Setter Property="Padding" Value="3" />
                </Style>
            </ResourceDictionary>
        </ContentPage.Resources>
        <ContentPage.Content>
            <StackLayout Spacing="0" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" BackgroundColor="#6596da" Orientation="Vertical">
                <!-- 
    
                Header 
    
                -->
                <local:HeaderCustomView />
                <!-- 
    
                Content Row 
    
                -->
                <StackLayout Spacing="0" Padding="2" Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="Fill">
                    <!-- Server -->
                    <StackLayout Style="{StaticResource RowsContainersStyle}">
                        <Label Text="Serveur:" Style="{StaticResource TextStyle}" />
                        <Entry x:Name="servEnt" Style="{StaticResource EditorTextStyle}" />
                    </StackLayout>
                    <!-- IP -->
                    <StackLayout Style="{StaticResource RowsContainersStyle}">
                        <Label Text="IP:" Style="{StaticResource TextStyle}" />
                        <Entry x:Name="ipEnt" Style="{StaticResource EditorTextStyle}" />
                    </StackLayout>
                    <!-- Port -->
                    <StackLayout Style="{StaticResource RowsContainersStyle}">
                        <Label Text="Port:" Style="{StaticResource TextStyle}" />
                        <Entry x:Name="portEnt" Style="{StaticResource EditorTextStyle}" />
                    </StackLayout>
                    <!-- Token -->
                    <StackLayout Style="{StaticResource RowsContainersStyle}">
                        <Label x:Name="tokenLabel" Text="Token:" Style="{StaticResource TextStyle}" />
                        <Label x:Name="token" TextColor="{StaticResource textStyleColor}" Font="Medium" HorizontalOptions="Start" VerticalOptions="Center" />
                    </StackLayout>
                    <BoxView Color="Transparent" HeightRequest="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />
                    <Button x:Name="legalNoticesButton" Clicked="openLegalNotices" Text="Mentions Legales" Style="{StaticResource GoToButton}" />
                    <Button x:Name="charteqQualiteButton" Clicked="openCharteQualite" Text="Charte Qualité" Style="{StaticResource GoToButton}" />
                    <Button x:Name="charteConfidentialitButton" Clicked="openCharteConfidentialite" Text="Charte Confidentialité" Style="{StaticResource GoToButton}" />
                </StackLayout>
                <!--    
    
                Buttons Rows 
    
                -->
                <StackLayout Padding="10,5" Orientation="Vertical" HorizontalOptions="FillAndExpand" VerticalOptions="EndAndExpand">
                    <Button x:Name="logoutButton" Clicked="logoutButtonClicked" Text="Déconnecter" Style="{StaticResource ActionsButtons}" Image="ico_logout.png" />
                    <Button x:Name="returnButton" Clicked="returnButtonClicked" Text="Retour" Style="{StaticResource ActionsButtons}" Image="ico_retour.png" />
                </StackLayout>
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    Thank you!

  • IuliaJecanIuliaJecan USMember ✭✭
    Accepted Answer

    Found my pb!!
    In Custom View xaml code I did not put the content of the page in a main layout, there for the error:

    The solution: add all the views in a Layout.

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="PCSA_Infirmiers.HeaderCustomView">
        <ContentView.Content>
            <StackLayout>
            <Label x:Name="header" Text="OptionsTrest" TextColor="White" FontSize="50" VerticalOptions="Start" BackgroundColor="#7caad5" FontAttributes="Bold" />
                <BoxView Color="White" HeightRequest="3" HorizontalOptions="FillAndExpand" />
        </StackLayout>
        </ContentView.Content>
    </ContentView>v
    
Sign In or Register to comment.