Page view counter

Silverlight Tips of the Day - Blog by Mike Snow

Game Programming with Silverlight

Silverlight Tip of the Day #89 – How to use String Resources

In this Tip I will be showing you how to place hard coded strings into a resource area. This is a common best practice for software development that makes it much easier to manage the strings in your application.

To start, open up your App.xaml file and add a reference to the MSCORLIB library as seen highlighted here.

<Application xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns
:x=http://schemas.microsoft.com/winfx/2006/xaml 
xmlns:clr
="clr-namespace:System;assembly=mscorlib"
x:Class="TestApp.App">

One place you can add all your strings is under the Application.Resources section in your App.xaml. To illustrate this, I have added one string resource that can be referenced through the key “MyName”.

<Application.Resources>
    <clr:String x:Key="MyName">Mike Snow</clr:String>
</Application.Resources>

Finally, open up Page.xaml and add a TextBlock. Set the text of this TextBlock to point to the string resource we just added. This can be done by using the syntax Text=”{StaticResource MyName}” as seen here:

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

Thank you,
--Mike Snow

 Subscribe in a reader

Comments

Microsoft Weblogs said:

In this Tip I will be showing you how to place hard coded strings into a resource area. This is a common

# January 27, 2009 7:29 PM

vitor_canova said:

ASP.NET has a feature that you put the string in Resource and depending on language page it takes Resources.pt-BR, Resources.en-US, Resources.es-ES (I think that the sintax is correct).

This tip remember me this features then my question is: How can I put string for diferents languages and handle this. Like a Globalization of site.

# January 27, 2009 8:12 PM

Community Blogs said:

In this issue: Thiago Felix, Thomas Kirchmair, Ken Cox, Jeff Paries, Daniel Crenna, Bryant Likes, Mike

# January 28, 2009 12:30 AM

Dew Drop - January 28, 2009 | Alvin Ashcraft's Morning Dew said:

Pingback from  Dew Drop - January 28, 2009 | Alvin Ashcraft's Morning Dew

# January 28, 2009 8:48 AM

suntsu said:

hi

Why not just use the standard way for handling resources in .net(resx)?

# January 28, 2009 9:52 AM

Programming with Silverlight, WPF & .NET » Tip of The Day #84 - #89 How to …. said:

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Tip of The Day #84 - #89 How to &#8230;.

# January 29, 2009 2:27 AM

Programming with Silverlight, WPF & .NET » Tip of The Day #84 - #89 How to …. said:

Pingback from  Programming with Silverlight, WPF &amp; .NET &raquo; Tip of The Day #84 - #89 How to &#8230;.

# January 29, 2009 2:27 AM

Секреты Visual Studio said:

В этом совете будет показано, как поместить строковые константы в ресурсы проекта. Эта практика является

# January 30, 2009 1:25 AM

Секреты Visual Studio said:

В этом совете будет показано, как поместить строковые константы в ресурсы проекта. Эта практика является

# January 30, 2009 1:27 AM

Silverlight Tips of the Day - Blog by Mike Snow said:

The purpose of this post is to create an outline summary all the blogs from my Silverlight Tips of the

# February 10, 2009 6:22 PM

Visual Web Developer Team Blog said:

Most Recent Posts: Tip #93 - Reading XML with Silverlight Tip #92 - How to Load Images from a Stream

# February 10, 2009 6:25 PM

vivek74 said:

How do i use page Title in a text box? I tried

<TextBox Title={Binding Path=Title}/> and that didnt work. I am using clr defined string as a workaround but it would be nice to reuse page properties in the controls it hosts.

# August 7, 2009 2:10 PM

NewsPeeps said:

Thank you for submitting this cool story - Trackback from NewsPeeps

# August 8, 2009 6:35 PM