C# Application.Current.Resources和Resources之间的区别是什么
我在我的C# Application.Current.Resources和Resources之间的区别是什么,c#,wpf,resources,C#,Wpf,Resources,我在我的App.xaml文件中定义了全局ResourceDictionary <Application.Resources> <ResourceDictionary> <Style x:Key="yolo" TargetType="Grid" /> </ResourceDictionary> </Application.Resources> 这个资源是什么 这个资源是什么 它是控件的资源字典,我们正在编
App.xaml
文件中定义了全局ResourceDictionary
<Application.Resources>
<ResourceDictionary>
<Style x:Key="yolo" TargetType="Grid" />
</ResourceDictionary>
</Application.Resources>
这个资源是什么
这个资源是什么
它是控件的资源字典,我们正在编写其代码背后的代码
另一方面,Application.Current.Resources
是应用程序对象本身的资源字典
如果您在
App.xaml.cs
中编写代码,则Resources
和Application.Current.Resources
将引用同一对象。Application.Current.Resources
包含在App.xaml
文件中声明的资源,可以在整个应用程序中看到这些资源
this.Resources
包含本地为任何控件定义的资源,例如窗口
,用户控件
,并且仅在此控件中可用
带有窗口的示例
:
XAML
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Window.Resources>
<sys:String x:Key="MyString">TestString</sys:String>
</Window.Resources>
<Window ...
xmlns:sys="clr-namespace:System;assembly=mscorlib"
<Window.Resources>
<sys:String x:Key="MyString">TestString</sys:String>
</Window.Resources>
public MainWindow()
{
InitializeComponent();
string test = this.Resources["MyString"] as string;
}