'How do I access Window Resources in WinUI 3
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>
Solution 1:[1]
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>
Solution 2:[2]
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>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Rick Stanley |
Solution 2 | Plastic Sturgeon |