开发者

How to declare an empty string in XAML 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>

The trouble is that in that case, I get a XamlParseException:

Cannot create object of type 'System.String'. CreateInstance failed, which can be caused by not having a public default constructor for 'System.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 extensi开发者_JAVA百科on, not the string. Putting x:Static in the sys:String element gives the same error as before.

Is it even possible to declare an empty string as an XAML resource? How?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜