Jakub Kuszneruk Jakub Kuszneruk - 2 months ago 16
C# Question

What is the difference between Application.Current.Resources and Resources

I have global

ResourceDictionary
defined in my
App.xaml
file.

<Application.Resources>
<ResourceDictionary>
<Style x:Key="yolo" TargetType="Grid" />
</ResourceDictionary>
</Application.Resources>


I've had set breakpoint in random
Page
and checked difference in debugger:

Application.Current.Resources = 1
Resources = 0


What's the
this.Resources
?

Answer

Application.Current.Resources

Contains the resources that are declared in the App.xaml file, which can be seen the entire application.

this.Resources

Contains the resources that are defined locally for any control, such as Window, UserControl and are only available within this control.

Example with Window:

XAML

<Window ...
        xmlns:sys="clr-namespace:System;assembly=mscorlib"

<Window.Resources>
    <sys:String x:Key="MyString">TestString</sys:String>
</Window.Resources>

Code-behind

public MainWindow()
{
    InitializeComponent();
    string test = this.Resources["MyString"] as string;
}
Comments