在AppResources中使用字符串
我正在使用LocalizedResources
来获取我的WP 8.1应用程序的本地化。 但是,如果我使用这样的代码:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { string xyz = string.Empty; NavigationContext.QueryString.TryGetValue("znam", out xyz); tekstZnamenitosti.Text = AppResources.tekstTrsat; }
那么我必须有很多if语句来检查每个znamenitost
。 有没有办法使用
tekstZnamenitosti.Text = AppResources.xyz;
其中xyz是我之前和之前创建的字符串,是从我导航的页面传递的值。
您可以使用ResourceManager获取AppResources
值:
tekstZnamenitosti.Text = AppResources.ResourceManager.GetString("xyz");
只是为了扩展Chris Shao的答案 (+1) – 也许有人觉得它很有用:
为您的资源创建特殊类:
namespace YourNamespace { public class AppRes { private static ResourceLoader load = new ResourceLoader(); private static string GetProperty([CallerMemberName] string propertyName = null) { return propertyName; } public static string PropertyName { get { return load.GetString(GetProperty()); } } } }
在App.xaml中添加资源键 :
// ... rest of the code.
现在您可以轻松地使用代码中的资源 :
string fromResources = AppRes.PropertyName;
在XAML中:
您需要做的一件事是,一旦您将资源添加到Resources.resw ,您必须添加另一行:
public static string NewPropertyName { get { return load.GetString(GetProperty()); } }
是的,您可以在App.xaml.cs
获得字符串值,例如
public static string xyz;
然后,您可以将所需的值保存到您在App.xaml.cs中创建的变量中。
tekstZnamenitosti.Text = App.xyx;
您可以在应用程序的任何位置使用此变量。
上述就是C#学习教程:在AppResources中使用字符串分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/953021.html