将资源字符串设置为XAML

Ind*_*ore 10 c# xaml microsoft-metro windows-8

我知道如何从资源
<TextBlock x:Uid="Text1"/> 中设置字符串Text1.Text"Hello"

但我想这样做

<TextBlock Text = {something here to get GreetingText}/>
Run Code Online (Sandbox Code Playgroud)

在哪里GreetingText"你好"

这样我也可以从代码中获得相同的字符串

var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
var string = loader.GetString("GreetingText");
Run Code Online (Sandbox Code Playgroud)

Nik*_*wal 11

包括这个

xmlns:system="clr-namespace:System;assembly=mscorlib"
Run Code Online (Sandbox Code Playgroud)

拥有这样的资源system:string.

<Window.Resources>
    <system:String x:Key="GreetingText">Hello</system:String>        
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

并在xaml中使用它

<TextBlock Text="{StaticResource GreetingText}" />
Run Code Online (Sandbox Code Playgroud)

并在后面的代码中使用它

string s = (string)objectofMainWindow.Resources["GreetingText"];
Run Code Online (Sandbox Code Playgroud)

编辑:回答您的评论

就是这样.资源字典在里面Window.Resources

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

      Your Rest namespaces

     />

<Window.Resources>
    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                    xmlns:local="using:ATTFamilyMap.strings">
        <system:String x:Key="GreetingText">Hello</system:String>
    </ResourceDictionary>
</Window.Resources>

Your Code

</Window>
Run Code Online (Sandbox Code Playgroud)


Sha*_*ish 11

Nikhil的答案是正确的,但适合其他平台.

对于Windows 8,您需要在资源目录中执行以下操作:

<x:String x:Key="MyString">This is a resource</x:String>
Run Code Online (Sandbox Code Playgroud)

在你的xaml中:

<TextBlock Text="{StaticResource MyString}"/>
Run Code Online (Sandbox Code Playgroud)

在代码中:

string myString = (string)(App.Current.Resources["MyString"]);
Run Code Online (Sandbox Code Playgroud)