uno: Control Library Styles Not Working

Current behavior

Using this documentation to create a control library for my existing Uno project doesn’t work. The control never have it’s default style used.

Expected behavior

The control should have it’s default style used.

How to reproduce it (as minimally and precisely as possible)

  1. Download BringTemplate.zip
  2. Start the solution with Visual Studio
  3. Start the application on Windows
  4. Notice that the page is empty

Workaround

Using Uno.XamlMerge.Task and directly referencing project together instead of using a NuGet worked but that’s not an option for the project.

Works on UWP/WinUI

No

Environment

Uno.WinUI / Uno.WinUI.WebAssembly / Uno.WinUI.Skia

NuGet package version(s)

4.7.37

Affected platforms

Android, iOS, Windows

IDE

Visual Studio 2022

IDE version

17.4.5

Relevant plugins

No response

Anything else we need to know?

I did not try the mobile platforms on the sample application because I was having weird issues with the new Uno application template (I did not compile and was complaining about targets). But I know from my other project that it also doesn’t work on mobile.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 25 (8 by maintainers)

Most upvoted comments

What do you mean? You can take a look at this document if you have questions on the app structure.

Thanks. Another technique to debug this to enable “native debugging” and run the app again.

Try breaking on all exceptions, the error message generally appears at that time.

Thanks for the report.

There are multiple issues in your sample coming from misdirections in the documentation:

  • “Custom Control” does not exist in the template. It’s Templated Control that needs to be used.

  • The DefaultStyleKey is missing, which is present in the Templated Control item template.

  • The Generic.xaml paths are incorrect and should be along those lines:

    
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
    
    <ResourceDictionary.MergedDictionaries>
    
    	<ResourceDictionary Source="ms-appx:///ControlLibrary.Uno.WinUI/CustomControl.xaml" />
    
    	<ResourceDictionary Source="ms-appx:///ControlLibrary.Uno.WinUI/Themes/CustomControlTest.xaml" />
    
    </ResourceDictionary.MergedDictionaries>
    
    </ResourceDictionary>
    
    
  • The styles in CustomControlTest.xaml are containing an invalid TargetType at line 9.

We’ll change the docs to make this clearer. You should also not need a shared project anymore, why did you create one?

Thanks I’ll take a look, and also the Shared project is used because it’s a library that target both WinUI(net6) and Xamarin so we have a .Uno and .Uno.WinUI head.