Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何以编程方式从App.xaml访问资源字符串?_C#_Windows Store Apps_Winrt Xaml_Resourcedictionary_App.xaml - Fatal编程技术网

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

C# 如何以编程方式从App.xaml访问资源字符串?,c#,windows-store-apps,winrt-xaml,resourcedictionary,app.xaml,C#,Windows Store Apps,Winrt Xaml,Resourcedictionary,App.xaml,我想从App.xaml中的代码中访问一些字符串,如: <Application.Resources> <ai:TelemetryContext x:Key="ApplicationInsightsBootstrapper" xmlns:ai="using:Microsoft.ApplicationInsights"/> <ResourceDictionary x:Key="rd"> <ResourceDictionary.

我想从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>
但如果失败,则“Platypus.exe WinRT信息中发生了'System.exception'类型的第一次意外异常:未找到ResourceMap。”

我认为我找到了解决问题的方法,据说这种方法不可取,我应该这样做:

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”

我传递的参数有效:

String bmn = GetResourceString("BingMapsNamespace");
那交易是什么

更新 与其使用App.xaml.cs,还不如使用App.xaml.cs(或者更好,实际上,因为它可能会起作用):

public static string BingMapsNamespace { get; set; }
. . .
BingMapsNamespace = "http://schemas.microsoft.com/search/local/ws/rest/v1";
然后从其他地方访问它,如下所示:

String bmn = App.BingMapsNamespace;

后来:是的,它确实有效。我将另一个标记为正确,因为它显然适用于某些人。

这对我来说很好:

<Application.Resources>
    <x:String x:Key="AppName">My App Name</x:String>
</Application.Resources>
我不知道这样做是否正确,但这很方便

<Application.Resources>
    <x:String x:Key="AppName">My App Name</x:String>
</Application.Resources>
string text = (string)App.Current.Resources["AppName"];