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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With