Embed a System.String in XAML

I don't know why, but in my .Net Core 3 WPF app I should use this xmlns definition instead of "mscorlib":

xmlns:system="clr-namespace:System;assembly=System.Runtime"

then I can define:

<system:Double x:Key="FontSizeLarge">24</system:Double>

or

<system:String x:Key="StringTest">Test</system:String>

You should use Window.Resources

Here's an example for Page, in your case it will be Window.Resources tag:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:System="clr-namespace:System;assembly=mscorlib">
  <Page.Resources>
    <System:String x:Key="MyString">Hello</System:String>
  </Page.Resources>
  <Grid>  
    <TextBlock Text="{StaticResource MyString}"></TextBlock>
  </Grid>
</Page>

In the Application tag you need to include the following:

xmlns:system="clr-namespace:System;assembly=mscorlib">

without the above code, Visual Studio will complain about a missing assembly reference.


Having a reference to the string will not allow you to change it later, since strings are immutable, so as Yacoder suggests, just put it in the <Window.Resources> section. Something like:

<Window.Resources>
        <System:String x:Key="TestString">Test</System:String>
</Window.Resources>

If you need to be able to change the value of the string that appears in your grid, you'll want to use a TextBlock or other control whose Content property can be set.