如何以编程方式从 App.xaml 访问资源字符串? [英] How can I programmatically access a resource string from App.xaml?

查看:49
本文介绍了如何以编程方式从 App.xaml 访问资源字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 App.xaml 中的代码访问一些字符串,例如:

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);
}

但如果失败,Platypus.exe WinRT 信息中发生类型为‘System.Exception’的第一次机会异常:ResourceMap Not Found."

我以为我找到了问题的解决方案 here,据说这种方式已被弃用,我应该这样做:

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;
}

但这也行不通;我得到了同样的旧WinRT 信息:未找到 ResourceMap".也用这个代码.

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

那么交易是什么?

使用 App.xaml.cs 而不是它的亲戚:

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.

推荐答案

这对我来说很好:

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

然后在代码中:

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. :)

这篇关于如何以编程方式从 App.xaml 访问资源字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆