Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 extension, 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?

like image 709
Thomas Levesque Avatar asked Aug 26 '11 11:08

Thomas Levesque


1 Answers

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
like image 171
H.B. Avatar answered Nov 12 '22 09:11

H.B.