7

I've just started working on a WinUi 3 (Desktop) project and have got blocked trying to add Window resources to a Window.

I would have thought the following would work. However there appears to be no Resources property.

<Window.Resources>
    
</Window.Resources>
Mark
  • 173
  • 2
  • 6

3 Answers3

2

Apparently it was a design choice, to quote Miguel, a member of Microsoft, from a Github issue:

[...] the Window object doesn't have Resource property like WPF, for instance. It was a design decision [...]

The alternative is to use the dictionary within the component's context, for instance:

<ListView>
  <ListView.Resources>
   <!-- My resources -->
  </ListView.Resources>
</ListView>
Rick Stanley
  • 732
  • 2
  • 9
  • 23
0

You can declare the resource in the root control of your window.

<Window ...>
    <Border ...>
        <Border.Resources>
            <Style TargetType="Button">
                <Setter Property="Margin" Value="10"/>
            </Style>
        </Border.Resources>
        <Button ... />
    </Border>
</Window>
-1

You're right - there are no Window resources. You can have globally shared resources by making App resources, or you can have <page> and <Control> level resources.

What your probably want is to define your resource dictionaries in App.xml

<Application
x:Class="BOMCheck.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="using:CommunityToolkit.WinUI.UI.Converters"
xmlns:helpers="using:BOMCheck.Helpers"
xmlns:local="using:BOMCheck"
xmlns:media="using:Microsoft.UI.Xaml.Media"
RequestedTheme="Light">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47