简体   繁体   English

如何以编程方式从App.xaml访问资源字符串?

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

I have some Strings I want to access from my code in App.xaml like: 我有一些我想从App.xaml中的代码访问的字符串,如:

<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. " 但如果失败,“ 在Platypus.exe WinRT信息中发生了'System.Exception'类型的第一次机会异常:找不到ResourceMap。

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." 我得到了相同的旧“WinRT信息:未找到ResourceMap”。 with this code, too. 也有这个代码。

The arg I'm passing is valid: 我通过的arg是有效的:

String bmn = GetResourceString("BingMapsNamespace");

So what's the dealio? 那么什么是有效的?

UPDATE 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: 它可能同样(或更好,实际上,因为它可能会工作),使用App.xaml.cs而不是它的表弟:

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. 我认为对方是正确的,因为它显然适用于某些人。

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. 我不知道这是否是正确的方法,但这很方便。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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