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

ControlTemplate for a custom control completely breaks the view

$
0
0

I have a custom control called ValidationSummary extended from Xamarin.Forms.Frame. Xaml of the control looks like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Frame xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          x:Class="My.DemoControl"
          BackgroundColor="#FFEEEE"
          Padding="5.0"
          x:Name="root"
          OutlineColor="Red">

        <StackLayout Padding="5" x:Name="container">
          <Label TextColor="Red" Text="Hello red"></Label>
        </StackLayout>

    </Frame>

The control itself is empty for the purposes of this demo:

        using Xamarin.Forms;

        namespace My
        {
            public partial class DemoControl : Frame
            {
                public DemoControl()
                {
                    InitializeComponent();
                }
            }
        }

As a test, I have a simple content page:

        <?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:cntrls="clr-namespace:My"
                     x:Class="Views.SandboxPage"
                     Title="Sandbox"
                     BackgroundColor="Teal">

          <StackLayout Padding="10.0">

            <cntrls:DemoControl>
            </cntrls:DemoControl>

            <Label Text="Something else"/>
          </StackLayout>
        </ContentPage>

Everything works as expected, I see Hello red and Something else texts on the page.

But then I want to set custom ControlTemplate like this in my App.xaml:

     <Application.Resources>
            <ResourceDictionary>
              <ControlTemplate x:Key="greenDemo">
                   <StackLayout Padding="5">
                       <Label TextColor="Green" Text="Hello green"></Label>
                    </StackLayout>
               </ControlTemplate>
            </ResourceDictionary>
       </Application.Resources>

I set it like this:

        <cntrls:DemoControl ControlTemplate="{StaticResource greenDemo}">
        </cntrls:DemoControl>

As the result, I would expect to see Hello green and Something else texts on the page. But I see nothing at all! The page gets completely empty as if the ContentPage had been completely broken.

Everything else - global styles etc. - works fine, so it's no App.xaml problem.

What's wrong? How to implement ControlTemplate for my custom control?


Viewing all articles
Browse latest Browse all 75885

Trending Articles



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