Set resource string to XAML

后端 未结 2 1344
逝去的感伤
逝去的感伤 2020-12-31 10:08

I know how to set string from resource
where Text1.Text is \"Hello\"

But I want to do like this

相关标签:
2条回答
  • 2020-12-31 10:59

    Include this

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

    Have a resource of system:string like this.

    <Window.Resources>
        <system:String x:Key="GreetingText">Hello</system:String>        
    </Window.Resources>
    

    and use it in xaml as

    <TextBlock Text="{StaticResource GreetingText}" />
    

    and use it in code behind as

    string s = (string)objectofMainWindow.Resources["GreetingText"];
    

    Edit: Answer to your comment

    Its this way. Resource Dictionary is inside 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>
    
    0 讨论(0)
  • 2020-12-31 11:14

    Nikhil's answer is on the right track, but is right for other platforms.

    For windows 8, you need to do the following in your resource directory:

    <x:String x:Key="MyString">This is a resource</x:String>
    

    In your xaml:

    <TextBlock Text="{StaticResource MyString}"/>
    

    In code:

    string myString = (string)(App.Current.Resources["MyString"]);
    
    0 讨论(0)
提交回复
热议问题