Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Templated Control doesn't appear #6475

Closed
geometrikal opened this issue Aug 27, 2021 · 5 comments
Closed

Templated Control doesn't appear #6475

geometrikal opened this issue Aug 27, 2021 · 5 comments

Comments

@geometrikal
Copy link

I'm trying to create a TemplatedControl for the first time, but it just doesn't appear. Any ideas what I am doing wrong?

axaml:

<Styles xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="using:my_project.Controls">
    <Design.PreviewWith>
        <controls:Alert />
    </Design.PreviewWith>

    <Style Selector="controls|Alert">
        <!-- Set Defaults -->
        <Setter Property="Template">
            <ControlTemplate>
                <Grid IsVisible="True" Background="#40000000">
                    <Border Height="120" Width="200" Background="#EEEEEE" CornerRadius="{TemplateBinding CornerRadius}">
                        <StackPanel Spacing="16" Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" >
                            <TextBlock Text="{TemplateBinding Title}" />
                        </StackPanel>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter>
    </Style>
</Styles>

axaml.cs:

using Avalonia;
using Avalonia.Controls.Primitives;
using Avalonia.Data;

namespace my_project.Controls
{
    public class Alert : TemplatedControl
    {
        public static readonly DirectProperty<Alert, string> TitleProperty =
            AvaloniaProperty.RegisterDirect<Alert, string>(
                nameof(Thickness), 
                o => o.Title,
                (o, v) => o.Title = v, 
                defaultBindingMode: BindingMode.OneWay);

        private string title;
        public string Title
        {
            get => title;
            set => SetAndRaise(TitleProperty, ref title, value);
        }
    }
}

Main window:

...
<control:Alert Title="this is a test"/>
...
@maxkatz6
Copy link
Member

Have you included style from "axaml" in your app? With StyleInclude.

@geometrikal
Copy link
Author

@maxkatz6 Thanks that was it.

For anyone else:
Under Application.Styles in App.axaml you need to add something like this (avares = avalonia resource):

<StyleInclude Source = "avares://my_project/Controls/Alert.axaml" />

@Kruno313
Copy link

Today, 2024 with Avalonia 11.1.0 the template for Templated control does not use <Style> anymore. See Problem using TemplatedControl #16447.
So, the using in app.axaml is also different:

<Application.Resources>
	<ResourceDictionary>
		<ResourceDictionary.MergedDictionaries>
			<ResourceInclude Source="/Controls/TemplatedControl1.axaml"/>
		</ResourceDictionary.MergedDictionaries>
		. . .
	</ResourceDictionary>
</Application.Resources>

@NickeManarin
Copy link

NickeManarin commented Sep 15, 2024

Not working here with 11.1.0:

<Application.Resources>
      <ResourceDictionary>
          <ResourceDictionary.MergedDictionaries>
              <ResourceInclude Source='MyApp.Controls/Themes/ExtendedButton.axaml' />
          </ResourceDictionary.MergedDictionaries>
     </ResourceDictionary>
 </Application.Resources>

I get this error:

App.axaml(15,34): Error AVLN2000 Avalonia: Unable to resolve XAML resource "avares://MyApp/MyApp.Controls/Themes/ExtendedButton.axaml" in the "MyApp" assembly. Make sure this file exists and is public.

I followed the custom templated control example from here, just the difference being the control existing in another assembly.

Btw, in Rider, the Right-click > New > Templated Control still creates a control using the 'Styles' xaml.

@maxkatz6
Copy link
Member

maxkatz6 commented Sep 15, 2024

I followed the custom templated control example from here, just the difference being the control existing in another assembly.

You need to specify assembly then.

If "MyApp.Controls" is the name of your assembly, then:

<ResourceInclude Source='avares://MyApp.Controls/Themes/ExtendedButton.axaml' />

This doc is about assets, but we use the same URI syntax for resources as well:
https://docs.avaloniaui.net/docs/basics/user-interface/assets#library-assets
Also https://docs.avaloniaui.net/docs/guides/styles-and-resources/resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants