简体   繁体   中英

How to get a resource String in the c# code in WP7?

I followed this tutorial to localize my app: http://msdn.microsoft.com/en-us/library/ff637520%28v=vs.92%29.aspx#Y1210

So I created the class:

namespace Foo
{
    public class LocalizedStrings
    {
        public LocalizedStrings()
        {
        }

        private static Foo.AppResources localizedresources = new Foo.AppResources();

        public Foo.AppResources Localizedresources { get { return localizedresources; } }

    }
}

When I use {Binding Path=Localizedresources.String1, Source={StaticResource LocalizedStrings}} in the XAML files it works great, but

how can I access to the String1 in the source code eg to set a textBlock.Text

In C# you can just do:

textBlock.Text = AppResources.MyLocalizedString;

or in XAML:

<TextBlock Text="{Binding Path=LocalizedResources.MyLocalizedString, Source={StaticResource LocalizedStrings}}" >

When you are in the code behind (ie c#), use the following to access the APPResources to display your localizedString like this:

busStopAppBtn.Content = AppResources.aBusStopAppBtn;

Obviously, the name of the object (ie app button (eg BusStopAppBtn)) can be any object of choice while the access modifier is AppResources. followed by the name of the object in your .resx file (ie aBusStopAppBtn).

I hope this helps.

I used the article "How to build a localized app for Windows Phone 8" which can be discovered here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff637520(v=vs.105).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM