Application.Current.Resources no keys available
See original GitHub issueDescription
Hello, everyone,
I tried to load a style from code, unfortunately the ResourceDictionary shows me that it has 35 objects but no single key or value.
I don’t know if I’m doing something wrong, but I can’t load a style with a key
Steps to Reproduce
- Create Maui App
- Add Button in MainPage
- Add code
private void Button_Clicked(object sender, EventArgs e)
{
var res = Application.Current.Resources.MergedDictionaries.FirstOrDefault() as ResourceDictionary;
if(res.ContainsKey("Primary"))
{
}
}
Version with bug
6.0 (current)
Last version that worked well
Unknown/Other
Affected platforms
Android, I was not able test on other platforms
Affected platform versions
Android API 31
Did you find any workaround?
No response
Relevant log output
No response
Issue Analytics
- State:
- Created a year ago
- Reactions:7
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Can't retrieve WPF application resource if there is only one
I've found few references to bug when Application.Resources don't load when StartupURI is not defined. WPF: Cannot find resource defined in ...
Read more >Application.Resources Property (System.Windows)
Gets or sets a collection of application-scope resources, such as styles and brushes. ... Resources is thread safe and is available from any...
Read more >How can I use an application resource ?
No resource with the key of 'GelButton' is found in the current page, so the resource lookup scope for the requested resource continues...
Read more >Managing Application Resources when WPF is Hosted
The primary issue is that the runtime host is not WPF in these cases which means that Application.Current.Resources is not available.
Read more >WPF Resource Key Collision - Software Development and more
Although a resource key must be unique within any individual dictionary, a key can exist multiple times in a set of merged dictionaries....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
you can’t enumerate the resources (design choice at the time, for reasons I can’t recall at the moment), but you can query it using
TryGetValue()
the main purpose of ResourceDictionary is to be the source for {StaticResource}
From inspecting the resource dictionary a bit more closely, I could see that under the Private properties there is a property called MergedResources which looks to be populated appropriately:
So as a workaround, I’ve written an extension method to find a resource from this. It’s not ideal, and not going to be very efficient as it uses reflection on every call to it, so I will be removing this workaround from my own code as soon as this issue is fixed in Maui, but at least it works.
The reason for Casting to an IEnumerable<KeyValuePair<string, object>> is that casting to a Maui.Controls.ResourceDictionary throws an exception for some reason. The actual type of the underlying implementation property was an IEnumerable<KeyValuePair<string, object>>:
Usage:
myView.Style = Application.Current.Resources.FindResource("StyleKey") as Style;
Control now getting Style object:
Disclaimer - I’ve only tried this on Windows, but Op has said it also affects Android. The Microsoft.Maui.Controls.ResourceDictionary class implements a bunch of other collection type interfaces, so would be cautious about whether other platforms store the data in different collection types.