Tho*_*que 16 string wpf resources xaml
我有一个ResourceDictionary
包含字符串:
<ResourceDictionary xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="Foo">Hello world</sys:String>
...
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
这个字典是主题的一部分,在某些主题中,一些字符串是空的:
<sys:String x:Key="Foo"></sys:String>
Run Code Online (Sandbox Code Playgroud)
麻烦的是,在这种情况下,我得到一个XamlParseException
:
无法创建"System.String"类型的对象.CreateInstance失败,这可能是因为没有'System.String'的公共默认构造函数
我知道可以在数组资源中声明一个空字符串<x:Static Member="sys:String.Empty" />
,但是我不想要一个数组...... x:Static
直接使用作为资源返回标记扩展名,而不是字符串.把x:Static
在sys:String
元素给出了同样的错误了.
甚至可以将空字符串声明为XAML资源吗?怎么样?
H.B*_*.B. 22
声明使用x:Static
似乎对我来说很好......
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Test"
xmlns:sys="clr-namespace:System;assembly=mscorlib">
<x:Static x:Key="empty" Member="sys:String.Empty" />
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
cc.Content = (string)FindResource("empty"); //Casts to string without exception
Run Code Online (Sandbox Code Playgroud)