Search code examples
avalonia

How can I show my own Control in Avalonia


in my code, I create a class MyButon and it inherit button

`public class MyButon : Button`

but it can not show in UserContorl

<StackPanel> <control2:MyButon Background="Red" Width="100" Height="50"/> <Button Content="123" Background="Red" Width="100" Height="50" /> </StackPanel>

so what can i do to settle this Problem


Solution

  • Since you do not have an associated .xaml file the loader doesn't know which view to use. You can fix that by implementing StyleKey property from the IStyleable interface in your class. It will make your control use the Button style.

    Type IStyleable.StyleKey => typeof(Button);