Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 91519

How to create Usercontrol in xaml

$
0
0

Dear,
i'm try to create a ContentPage wich contains a ContentView this "ContentView" will be another xaml file like an usercontrol.

But this doesn't work! show me an exception

The ex is throwed becasuse the code behind in Page2.xaml.cs not derive from ContentView class, the ContentPage try to load the codebehind of the class "Page2" and not the xaml of it.
The ex disappear if put the : ContentView but the two label (MyLabel and MyEntry) aren't showed

For example:

        <?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="Cardioline.Pagetest"
                     xmlns:c="clr-namespace:Cardioline;assembly=Cardioline.Forms"
                     >
        <StackLayout>
                <c:Page2 Grid.Row="1" Grid.Column="0" BackgroundColor="Purple"/>
        </StackLayout>
        </ContentPage>

My idea is to LOAD and inject the Page2.xaml file this was:

        <?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="Page2">
            <ContentView.Content>
                <StackLayout BackgroundColor="Teal">
                    <Label x:Name="MyLabel" Text="Label">Ciao</Label>
                    <Entry x:Name="MyEntry" Text="Entry">Come va</Entry>
                </StackLayout>
            </ContentView.Content>
        </ContentView>

and behind code was :

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;

        using Xamarin.Forms;

        namespace Cardioline
        {
            public partial class Page2 //: ContentView ...but this not load the MyLabel and other defined into xaml
            {
                public Page2()
                {
                //InitializeComponent(); this func loadXaml but exist only in ContentPage?!? ho can call from another elemnt?
                }
            }
        }

Viewing all articles
Browse latest Browse all 91519

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>