如何在 XAML ResourceDictionary 中声明空字符串 [英] How to declare an empty string in XAML ResourceDictionary
问题描述
我有一个包含字符串的 ResourceDictionary
:
I have a ResourceDictionary
that contains strings:
<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>
这个字典是一个主题的一部分,在一些主题中,有些字符串是空的:
This dictionary is part of a theme, and in some themes, some of the strings are empty:
<sys:String x:Key="Foo"></sys:String>
问题是在那种情况下,我得到一个 XamlParseException
:
The trouble is that in that case, I get a XamlParseException
:
无法创建类型为System.String"的对象.创建实例失败,这可能是由于没有公共默认构造函数'System.String'
Cannot create object of type 'System.String'. CreateInstance failed, which can be caused by not having a public default constructor for 'System.String'
我知道可以使用 <x:Static Member="sys:String.Empty"/>
在数组资源中声明一个空字符串,但我不想要一个数组...直接使用 x:Static
作为资源返回标记扩展名,而不是字符串.将 x:Static
放在 sys:String
元素中会产生与之前相同的错误.
I know it is possible to declare an empty string in an array resource, using <x:Static Member="sys:String.Empty" />
, but I don't want an array... Using x:Static
directly as the resource returns the markup extension, not the string. Putting x:Static
in the sys:String
element gives the same error as before.
是否可以将空字符串声明为 XAML 资源?怎么样?
Is it even possible to declare an empty string as an XAML resource? How?
推荐答案
使用 x:Static
声明它似乎对我来说很好用...
Declaring it using x:Static
seems to work just fine for me...
<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>
cc.Content = (string)FindResource("empty"); //Casts to string without exception
这篇关于如何在 XAML ResourceDictionary 中声明空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!