英文:
How can I get the x:Name attribute of a ResourceDictionary in code behind?
问题
Console.WriteLine(Application.Current.Resources.MergedDictionaries[1].x:Name);
英文:
As the title says, I just want to get the x:Name
attribute of a ResourceDictionary
in code behind.
My ResourceDictionary
:
<ResourceDictionary x:Name="DefaultLight"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
...
</ResourceDictionary>
My App.Xaml
<Application x:Class="WpfApp1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApp1"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MyLib;component/Resources/OutgoingResources.xaml"/>
<ResourceDictionary Source="/MyLib;component/Resources/Themes/DefaultDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
My code behind:
Console.WriteLine(Application.Current.Resources.MergedDictionaries[1].???);
答案1
得分: 1
我不认为在合并后能够再次获取名称。
但是,你可以找到资源字典的来源:
var mergedDict = Application.Current.Resources.MergedDictionaries;
foreach (var rd in mergedDict)
{
Debug.WriteLine(rd.Source.OriginalString);
}
对于以下XML代码:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Dictionary1.xaml"/>
<ResourceDictionary Source="/Resources/Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
你会得到:
/Resources/Dictionary1.xaml
/Resources/Dictionary2.xaml
而不是Dictionary1或2。当然,你可以将这些文件命名为DefaultLight.xaml、DefaultDark.xaml等。
如果你的目的只是查看合并了哪个主题的资源字典,你可以在每个资源字典中使用一个关键字来告诉你:
<clr:String x:Key="CurrentTheme">DefaultDark</clr:String>
当然,在亮主题的资源字典中使用相同的关键字DefaultLight。
如果你想要为一组资源字典定义某种友好的名称,你可以定义一个列表,允许你将名称、URL和IsLoaded布尔值分离。加载你选择的资源字典并标记它为已加载。
英文:
I don't think you're going to get the name back out once it's merged.
You can, however, find the source of a resource dictionary:
var mergedDict = Application.Current.Resources.MergedDictionaries;
foreach (var rd in mergedDict)
{
Debug.WriteLine(rd.Source.OriginalString);
}
For:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Resources/Dictionary1.xaml"/>
<ResourceDictionary Source="/Resources/Dictionary2.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I get
/Resources/Dictionary1.xaml
/Resources/Dictionary2.xaml
Rather than Dictionary1 or 2 you can of course name those files DefaultLight.xaml, DefaultDark.xaml etc.
If your purpose is just to see which theme resource dictionary is merged then you could "just" have a key in each of them which tells you.
<clr:String x:Key="CurrentTheme">DefaultDark</clr:String>
And of course DefaultLight in the light resource dictionary, with the same key.
If you wanted to have some sort of friendly name for a bunch of resource dictionaries. You could define a list allows you to decouple.
A class with name, url and IsLoaded bool in a list. Load whichever you choose in code and mark it as loaded.
答案2
得分: 1
不要翻译代码,以下是翻译的部分:
There is no Name
property of a ResourceDictionary
that is set by the XAML processor.
You could create a custom type though that adds a Name
property though:
public class NamedResourceDictionary : ResourceDictionary
{
public string Name { get; set; }
}
And then set the property in the XAML markup:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:NamedResourceDictionary Name="Dark" Source="/MyLib;component/Resources/Themes/DefaultDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
...and retrieve it by casting the merged resource dictionary to your custom type:
var name = (Application.Current.Resources.MergedDictionaries[0] as NamedResourceDictionary)?.Name;
英文:
There is no Name
property of a ResourceDictionary
that is set by the XAML processor.
You could create a custom type though that adds a Name
property though:
public class NamedResourceDictionary : ResourceDictionary
{
public string Name { get; set; }
}
And then set the property in the XAML markup:
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:NamedResourceDictionary Name="Dark" Source="/MyLib;component/Resources/Themes/DefaultDark.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
...and retrieve it by casting the merged resource dictionary to your custom type:
var name = (Application.Current.Resources.MergedDictionaries[0] as NamedResourceDictionary)?.Name;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论