我有一些字符串希望可以从我的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>
我想以编程方式获取这些值,因此我编写了以下实用函数:
internal static String GetResourceString(String keyStr)
{
var resldr = new Windows.ApplicationModel.Resources.ResourceLoader();
return resldr.GetString(keyStr);
}
但是它失败了,显示“Platypus.exe WinRT 信息:ResourceMap Not Found.”
我曾经在这里找到解决问题的方法,据说那种方法已经过时了,我应该改用这种方法:
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 Not Found.” 无论我使用哪种代码。
我传递的参数是有效的:
String bmn = GetResourceString("BingMapsNamespace");
那么问题是什么?
更新
也许使用App.xaml.cs比使用它的角弯表亲更好(实际上更好),因为它应该能正常工作:
public static string BingMapsNamespace { get; set; }
. . .
BingMapsNamespace = "http://schemas.microsoft.com/search/local/ws/rest/v1";
接下来可以通过以下方式从其他位置访问它:
String bmn = App.BingMapsNamespace;
稍后:是的,它确实有效。我将另一个标记为正确,因为对于某些人来说显然有效。