How can I programmatically access a resource string from App.xaml?

10,473

This works fine for me:

<Application.Resources>
    <x:String x:Key="AppName">My App Name</x:String>
</Application.Resources>

Then in code:

string text = (string)App.Current.Resources["AppName"];

I don't know if that's the right way to do it, but it's convenient enough. :)

Share:
10,473
B. Clay Shannon-B. Crow Raven
Author by

B. Clay Shannon-B. Crow Raven

My novel about climate change and social justice featuring talking animals traveling through time and space to prevent disasters is now available on amazon, in three formats: Taterskin &amp; The Eco Defenders Kindle eBook; Taterskin &amp; The Eco Defenders Paperback; Taterskin &amp; The Eco Defenders Hardcover Taterskin &amp; The Eco Defenders, told in “first canine” by the titular character, a Labrador Retriever, is the story of a few humans and several talking animals who travel through time and space to make the past—and thus the future—a better place. The improvements effected by the Eco Defenders benefit not just the earth itself, but also mistreated humans and animals. In Book 1 (“Wonders Never Cease”), The Eco Defenders travel 150 million years into the past, to meet a Pterodactyl and make plans to “nip Nazism in the bud.” After that, it's on to 1787 Australia to protect the indigenous people and the environment there. The Eco Defenders next go to India, where they assemble animals from all over that country to put an end to Thuggee and fights to the death between Cobras and Mongooses. Their final stop is 1885 Africa, where the Eco Defenders band together with the local animals to prevent King Leopold of Belgium from taking control of the Congo, following which they put an end to the poaching of animals throughout the continent. Book 2 (“Tell it to Future Generations”) takes up with the Eco Defenders following up on their earlier adventures by 1) Preventing the American Civil War in 1861, after which a slave they free joins them; 2) Saving the Indians from being massacred at Wounded Knee in 1890, following which Chapawee, a Sioux Indian, joins the Eco Defenders; 3) Putting an end to the practice of vivisection (experimentation on live animals) in 1903; 4) Coming to the aid of exploited workers in 1911 Manhattan, saving hundreds from the Triangle Shirtwaist Fire; and 5) Traveling to the Amazon Basin in 1978 to protect and preserve the Amazon rainforest. @@@@@@@@@@@@@@@@@@@@@@@ I have lived in eight states; besides my native California (where I was born and where I now again reside), in chronological order I have infested: New York (Brooklyn), Montana (Helena), Alaska (Anchorage), Oklahoma (Bethany), Wisconsin (New Berlin and Oconomowoc), Idaho (Coeur d'Alene), and Missouri (Piedmont). I am a writer of both fiction (for which I use the nom de guerre "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006 and can be downloaded gratis here.

Updated on June 16, 2022

Comments

  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven almost 2 years

    I have some Strings I want to access from my code in App.xaml like:

    <Application.Resources>
        <ai:TelemetryContext x:Key="ApplicationInsightsBootstrapper" xmlns:ai="using:Microsoft.ApplicationInsights"/>
        <ResourceDictionary x:Key="rd">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="GlobalStylesResourceDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
            <!-- Application-specific resources. -->
            <x:String x:Key="AppName">Platypus</x:String>
            <x:String x:Key="BingMapsNamespace">http://schemas.microsoft.com/search/local/ws/rest/v1</x:String>
        </ResourceDictionary>
    </Application.Resources>
    

    And I want to programmatically get those values, so I wrote this utility function:

    internal static String GetResourceString(String keyStr)
    {
        var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
        return resldr.GetString(keyStr);
    }
    

    But if fails with, "A first chance exception of type 'System.Exception' occurred in Platypus.exe WinRT information: ResourceMap Not Found."

    I thought I found the solution to the problem here, where it is said that way of doing it is deprecated, and I should do this instead:

    internal static String GetResourceString(String keyStr)
    {
        try
        {
            //var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
            //return resldr.GetString(keyStr);
            var resldr = ResourceLoader.GetForCurrentView();
            return resldr.GetString(keyStr);
        }
        catch (Exception ex)
        {
            Debug.WriteLine("Something wrong with the keyStr? Exception in GetResourceString(): {0}", ex.Message); 
        }
        return String.Empty;
    }
    

    But that doesn't work, either; I get the same old "WinRT information: ResourceMap Not Found." with this code, too.

    The arg I'm passing is valid:

    String bmn = GetResourceString("BingMapsNamespace");
    

    So what's the dealio?

    UPDATE

    It may be just as well (or better, actually, as it will presumably work), to use App.xaml.cs rather than its angly cousin:

    public static string BingMapsNamespace { get; set; }
    . . .
    BingMapsNamespace = "http://schemas.microsoft.com/search/local/ws/rest/v1";
    

    And then access it from elsewhere like so:

    String bmn = App.BingMapsNamespace;
    

    Later: And yes, it does work. I'm marking the other as correct, as it obviously works for some people.