设为首页 收藏本站
查看: 1892|回复: 0

修炼九阴真经Windows Phone开发 (7):本地化应用程序栏Localizing an Application Bar 下

[复制链接]

尚未签到

发表于 2015-5-14 10:19:45 | 显示全部楼层 |阅读模式
  本节介绍另一个本地化的方法:
  在项目中添加资源文件:(这个文件将包含应用程序的默认语言的资源)
DSC0000.jpg
  将要名称和值添加进去。(作为应用程序中向用户显示字符串值).
  重复上面的方法,向项目中添加更多的其它语言资源文件。(参见后面的截图)
  
  定义默认的区域
  1.在解决方案资源管理器中,右键单击项目名称,选择属性,在application选项卡中,点 程序集信息。在非特定语言列表中,选择默认区域性。此标识语言的默认资源文件
  中的字符串。例如,如果默认资源文件被命名为AppResources.resx,并在该文件中的字符串支持英语,则可以选择english作为项目的中立国语言。
  
  添加LocalizedStrings类处理资源文件:



    public class LocalizedStrings
{
public LocalizedStrings()
{
}
private static ApplicationBarSample.AppResources localizedresources = new ApplicationBarSample.AppResources();
public ApplicationBarSample.AppResources Localizedresources { get { return localizedresources; } }
}
  
  主工程CS代码:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Globalization;
using System.Threading;
namespace ApplicationBarSample
{
public partial class MainPage : PhoneApplicationPage
{
#region Initialization
///
/// Constructor for the PhoneApplicationPage
/// The ApplicationBar is initialized. Icon buttons and menu items are added
/// to the ApplicationBar and event handlers are set.
///
public MainPage()
{
InitializeComponent();
this.SupportedOrientations = SupportedPageOrientation.PortraitOrLandscape;
ApplicationBar = new ApplicationBar();
ApplicationBar.IsMenuEnabled = true;
ApplicationBar.IsVisible = true;
ApplicationBar.Opacity = 1.0;
ApplicationBarIconButton hide = new ApplicationBarIconButton(new Uri("/Images/expand.png", UriKind.Relative));
//hide.Text = "hide";
hide.Text = AppResources.ButtonText;
hide.Click += new EventHandler(hide_Click);
ApplicationBarIconButton opacity = new ApplicationBarIconButton(new Uri("/Images/opacity.png", UriKind.Relative));
//opacity.Text = "opacity";
opacity.Text = AppResources.ButtonText;
opacity.Click += new EventHandler(opacity_Click);
ApplicationBarIconButton enabled = new ApplicationBarIconButton(new Uri("/Images/menuenabled.png", UriKind.Relative));
//enabled.Text = "enabled";
enabled.Text = AppResources.ButtonText;
enabled.Click += new EventHandler(enabled_Click);
ApplicationBar.Buttons.Add(hide);
ApplicationBar.Buttons.Add(opacity);
ApplicationBar.Buttons.Add(enabled);
//ApplicationBarMenuItem foregroundItem = new ApplicationBarMenuItem("use foreground color");
ApplicationBarMenuItem foregroundItem = new ApplicationBarMenuItem(AppResources.MenuItemText);
foregroundItem.Click += new EventHandler(foregroundItem_Click);
//ApplicationBarMenuItem accentItem = new ApplicationBarMenuItem("use accent color");
ApplicationBarMenuItem accentItem = new ApplicationBarMenuItem(AppResources.MenuItemText);
accentItem.Click += new EventHandler(accentItem_Click);
ApplicationBar.MenuItems.Add(foregroundItem);
ApplicationBar.MenuItems.Add(accentItem);
UpdateText();
}
#endregion
#region User Iterface
///
/// Click handler for accent color menu item.
/// Changes the colored UI elements to the built-in PhoneAccentColor
///
/// The control that raised the click event.
/// An EventArgs object containing event data.
void accentItem_Click(object sender, EventArgs e)
{
UpdateColor((Color)Resources["PhoneAccentColor"]);
}
///
/// Click handler for accent color menu item.
/// Changes the colored UI elements to the built-in PhoneForegroundColor
///
/// The control that raised the click event.
/// An EventArgs object containing event data.
void foregroundItem_Click(object sender, EventArgs e)
{
UpdateColor((Color)Resources["PhoneForegroundColor"]);
}
///
/// Click handler for opacity icon button.
/// Sets the opacity value of the ApplicationBar to 0, 1, or .5
///
/// The control that raised the click event.
/// An EventArgs object containing event data.
void opacity_Click(object sender, EventArgs e)
{
if (ApplicationBar.Opacity < .01)
{
ApplicationBar.Opacity = 1;
}
else if (ApplicationBar.Opacity > .49 && ApplicationBar.Opacity < .51)
{
ApplicationBar.Opacity = 0;
}
else
{
ApplicationBar.Opacity = .5;
}
UpdateText();
}
///
/// Click handler for hide icon button.
/// Changes the Visible property of the ApplicationBar to false
/// And makes the "Show Application Bar" button visible
///
/// The control that raised the click event.
/// An EventArgs object containing event data.
void hide_Click(object sender, EventArgs e)
{
ApplicationBar.IsVisible = false;
showButton.Visibility = Visibility.Visible;
UpdateText();
}
///
/// Click handler for menu enable icon button.
/// Changes the IsMenuEnabled property of the ApplicationBar
/// When IsMenuEnabled is false, the menu will not pop up
///
/// The control that raised the click event.
/// An EventArgs object containing event data.
void enabled_Click(object sender, EventArgs e)
{
ApplicationBar.IsMenuEnabled = !ApplicationBar.IsMenuEnabled;
UpdateText();
}
///
/// Click handler for show button.
/// Sets the Visible property of the Application Bar to true
///
/// The control that raised the click event.
/// An EventArgs object containing event data.
private void showButton_Click(object sender, RoutedEventArgs e)
{
ApplicationBar.IsVisible = true;
showButton.Visibility = Visibility.Collapsed;
UpdateText();
}
///
/// Updates the TextBlock objects to reflect the current state
/// of the ApplicationBar
///
void UpdateText()
{
VisibleLabel.Text = "Application Bar Visible ";
VisibleTextBlock.Text = ApplicationBar.IsVisible ? "Yes" : "No";
OpacityLabel.Text = "Application Bar Opacity ";
OpacityTextBlock.Text = ApplicationBar.Opacity.ToString("0.0");
MenuEnabledLabel.Text = "MenuEnabled ";
MenuEnabledTextBlock.Text = ApplicationBar.IsMenuEnabled ? "Yes" : "No";
}
///
/// Helper method for changing the color of the UI
///
/// The new color for the UI elements
void UpdateColor(Color c)
{
SolidColorBrush brush = new SolidColorBrush(c);
VisibleTextBlock.Foreground = brush;
OpacityTextBlock.Foreground = brush;
MenuEnabledTextBlock.Foreground = brush;
((LinearGradientBrush)Resources["Gradient"]).GradientStops[1].Color = c;
}
private void LocList_SelectedIndexChanged(object sender, SelectionChangedEventArgs e)
{
// Set the current culture according to the selected locale and display information such as
// date, time, currency, etc in the appropriate format.
string nl;
string cul;
nl = locList.SelectedIndex.ToString();
switch (nl)
{
case "0":
cul = "es-ES";
break;
case "1":
cul = "de-DE";
break;
case "2":
cul = "en-US";
break;
default:
cul = "en-US";
break;
}
// set this thread's current culture to the culture associated with the selected locale
CultureInfo newCulture = new CultureInfo(cul);
Thread.CurrentThread.CurrentCulture = newCulture;
CultureInfo cc, cuic;
cc = Thread.CurrentThread.CurrentCulture;
cuic = Thread.CurrentThread.CurrentUICulture;
VisibleLabel.Text = cc.NativeName;
VisibleTextBlock.Text = "";
//OpacityLabel.Text = cuic.DisplayName;
OpacityLabel.Text = "";
OpacityTextBlock.Text = "";
MenuEnabledLabel.Text = "";
MenuEnabledTextBlock.Text = "";
//localize icon button text      
if (this.ApplicationBar.Buttons != null)
{
foreach (ApplicationBarIconButton btn in this.ApplicationBar.Buttons)
{
btn.Text = cc.NativeName.Substring(0, cc.NativeName.ToString().Length/2);
}
}
//localize menu buttons text
if (this.ApplicationBar.MenuItems != null)
{
foreach (ApplicationBarMenuItem itm in this.ApplicationBar.MenuItems)
{
itm.Text = cc.NativeName;
}
}
}
#endregion
}
}
  
  
  
  XAML代码:






























  
  
DSC0001.jpg

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-66847-1-1.html 上篇帖子: Windows Phone 7 真机调试 下篇帖子: Windows Phone 7 ApplicationBar 轴视图切换 出现黑条或白条问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表