Set resource string to XAML

28,483

Solution 1

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"]);

Solution 2

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>
Share:
28,483
Inder Kumar Rathore
Author by

Inder Kumar Rathore

Develops mobile applications (primarily iOS) Open source work https://github.com/InderKumarRathore LinkedIn profile https://in.linkedin.com/in/InderKumarRathore Personal Site: http://inderkumar.herokuapp.com 📧rathore619🌀gmail🔘com But before emailing please read Jon Skeet's blog post on Stack Overflow-related emails first. Blogs Property in Objective - C

Updated on December 01, 2020

Comments

  • Inder Kumar Rathore
    Inder Kumar Rathore over 3 years

    I know how to set string from resource
    <TextBlock x:Uid="Text1"/> where Text1.Text is "Hello"

    But I want to do like this

    <TextBlock Text = {something here to get GreetingText}/>
    

    where GreetingText is "Hello"

    So that I may get the same string from code also as

    var loader = new Windows.ApplicationModel.Resources.ResourceLoader();
    var string = loader.GetString("GreetingText");
    
  • Inder Kumar Rathore
    Inder Kumar Rathore over 11 years
    You mean to say I have to create a System.string file and put your xml tags in it ???
  • Inder Kumar Rathore
    Inder Kumar Rathore over 11 years
    This is my resource dictionary and it's giving me some errors. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta‌​tion" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:ATTFamilyMap.strings" xmlns:system="clr-namespace:System;assembly=mscorlib"> <Window.Resources> <system:String x:Key="GreetingText">Hello</system:String> </Window.Resources> </ResourceDictionary>
  • Inder Kumar Rathore
    Inder Kumar Rathore over 11 years
    Error1 Unknown attachable member 'Window.Resources' on element 'ResourceDictionary' Error2 Unknown type 'String' in XML namespace 'clr-namespace:System;assembly=mscorlib'
  • Abbas
    Abbas over 11 years
    Your elements are in the wrong order. Place the ResourceDictionary-element inside the Window.Resources element. Look at Nihkils code-example! :)
  • Lee Richardson
    Lee Richardson over 11 years
    I couldn't get "<system:String" to work with the way Nikhil described for Windows 8, but it did work with as "<x:String" for whatever reason.