如何获取资源并在另一个xaml文件(WPF)中的代码上使用?

如何获取资源并在另一个xaml文件(WPF)中的代码上使用?,wpf,xaml,resources,styles,Wpf,Xaml,Resources,Styles,如何在另一个xaml文件中获取资源 我在Data.cs文件中,我想访问Example1.xaml文件中的样式资源 Style style1 = Application.Current.Resources["LabelTitleStyle"] as Style; Style style8 = Resources["NumberTitleText"] as Style; 实际上我是这样使用的,但它只能用于主xaml文件 我想在另一个xaml文件中获取资源 Style style1 = Appli

如何在另一个xaml文件中获取资源

我在Data.cs文件中,我想访问Example1.xaml文件中的样式资源

Style style1 = Application.Current.Resources["LabelTitleStyle"] as Style;
Style style8 = Resources["NumberTitleText"] as Style;
实际上我是这样使用的,但它只能用于主xaml文件 我想在另一个xaml文件中获取资源

Style style1 = Application.Current.Resources["LabelTitleStyle"] as Style;
Style style8 = Resources["NumberTitleText"] as Style;
然后,“资源”会得到红色下划线,因为“NumberTitleText”在当前上下文中不存在

如何从另一个xaml文件中获取资源并用于代码隐藏

请帮帮我!!:(

+++++

ResourceDictionary res=Application.LoadComponent(新Uri(“/…example.xaml”,UriKind.RelativeOrAbsolute))作为ResourceDictionary;
Style style8=res[“NumbertitleText”]作为样式;

我试过了,但是res是空的。为什么我不能带那本字典呢?

这对我来说很有效:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("..\\myResourcesFile.xaml", UriKind.Relative);
if (Application.Current.Resources.MergedDictionaries.Count > 0)
{
    Application.Current.Resources.MergedDictionaries.RemoveAt(Application.Current.Resources.MergedDictionaries.Count - 1);
}
Application.Current.Resources.MergedDictionaries.Add(dict);
这对我很有用:

ResourceDictionary dict = new ResourceDictionary();
dict.Source = new Uri("..\\myResourcesFile.xaml", UriKind.Relative);
if (Application.Current.Resources.MergedDictionaries.Count > 0)
{
    Application.Current.Resources.MergedDictionaries.RemoveAt(Application.Current.Resources.MergedDictionaries.Count - 1);
}
Application.Current.Resources.MergedDictionaries.Add(dict);