修炼九阴真经Windows Phone开发 (7):本地化应用程序栏Localizing an Application Bar 下...
生活随笔
收集整理的這篇文章主要介紹了
修炼九阴真经Windows Phone开发 (7):本地化应用程序栏Localizing an Application Bar 下...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本節介紹另一個本地化的方法:
在項目中添加資源文件:(這個文件將包含應用程序的默認語言的資源)
將要名稱和值添加進去。(作為應用程序中向用戶顯示字符串值).
重復上面的方法,向項目中添加更多的其它語言資源文件。(參見后面的截圖)
?
定義默認的區域
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/// <summary>/// Constructor for the PhoneApplicationPage/// The ApplicationBar is initialized. Icon buttons and menu items are added/// to the ApplicationBar and event handlers are set./// </summary>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/// <summary>/// Click handler for accent color menu item./// Changes the colored UI elements to the built-in PhoneAccentColor/// </summary>/// <param name="sender">The control that raised the click event.</param>/// <param name="e">An EventArgs object containing event data.</param>void accentItem_Click(object sender, EventArgs e){UpdateColor((Color)Resources["PhoneAccentColor"]);}/// <summary>/// Click handler for accent color menu item./// Changes the colored UI elements to the built-in PhoneForegroundColor/// </summary>/// <param name="sender">The control that raised the click event.</param>/// <param name="e">An EventArgs object containing event data.</param>void foregroundItem_Click(object sender, EventArgs e){UpdateColor((Color)Resources["PhoneForegroundColor"]);}/// <summary>/// Click handler for opacity icon button./// Sets the opacity value of the ApplicationBar to 0, 1, or .5/// </summary>/// <param name="sender">The control that raised the click event.</param>/// <param name="e">An EventArgs object containing event data.</param>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();}/// <summary>/// Click handler for hide icon button./// Changes the Visible property of the ApplicationBar to false/// And makes the "Show Application Bar" button visible/// </summary>/// <param name="sender">The control that raised the click event.</param>/// <param name="e">An EventArgs object containing event data.</param>void hide_Click(object sender, EventArgs e){ApplicationBar.IsVisible = false;showButton.Visibility = Visibility.Visible;UpdateText();}/// <summary>/// Click handler for menu enable icon button./// Changes the IsMenuEnabled property of the ApplicationBar/// When IsMenuEnabled is false, the menu will not pop up/// </summary>/// <param name="sender">The control that raised the click event.</param>/// <param name="e">An EventArgs object containing event data.</param>void enabled_Click(object sender, EventArgs e){ApplicationBar.IsMenuEnabled = !ApplicationBar.IsMenuEnabled;UpdateText();}/// <summary>/// Click handler for show button./// Sets the Visible property of the Application Bar to true/// </summary>/// <param name="sender">The control that raised the click event.</param>/// <param name="e">An EventArgs object containing event data.</param>private void showButton_Click(object sender, RoutedEventArgs e){ApplicationBar.IsVisible = true;showButton.Visibility = Visibility.Collapsed;UpdateText();}/// <summary>/// Updates the TextBlock objects to reflect the current state/// of the ApplicationBar/// </summary>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";}/// <summary>/// Helper method for changing the color of the UI/// </summary>/// <param name="c">The new color for the UI elements</param>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 localeCultureInfo 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 textif (this.ApplicationBar.MenuItems != null){foreach (ApplicationBarMenuItem itm in this.ApplicationBar.MenuItems){itm.Text = cc.NativeName;}}}#endregion} }?
?
?
XAML代碼:
<phone:PhoneApplicationPage x:Class="PhoneApp6.MainPage"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"FontFamily="{StaticResource PhoneFontFamilyNormal}"FontSize="{StaticResource PhoneFontSizeNormal}"Foreground="{StaticResource PhoneForegroundBrush}"SupportedOrientations="Portrait" Orientation="Portrait"shell:SystemTray.IsVisible="True"><!--LayoutRoot 是包含所有頁面內容的根網格--><Grid x:Name="LayoutRoot" Background="Transparent"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions><!--TitlePanel 包含應用程序的名稱和頁標題--><StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"><TextBlock x:Name="ApplicationTitle" Text="我的應用程序" Style="{StaticResource PhoneTextNormalStyle}"/><TextBlock x:Name="PageTitle" Text="頁面名稱" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/></StackPanel><!--ContentPanel - 在此處放置其他內容--><Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid></Grid><!--演示 ApplicationBar 用法的示例代碼--><phone:PhoneApplicationPage.ApplicationBar><shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"><shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="按鈕 1"/><shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="按鈕 2"/><shell:ApplicationBar.MenuItems><shell:ApplicationBarMenuItem Text="菜單項 1"/><shell:ApplicationBarMenuItem Text="菜單項 2"/></shell:ApplicationBar.MenuItems></shell:ApplicationBar></phone:PhoneApplicationPage.ApplicationBar></phone:PhoneApplicationPage>?
?
轉載于:https://www.cnblogs.com/eagle1986/archive/2012/04/26/2472751.html
總結
以上是生活随笔為你收集整理的修炼九阴真经Windows Phone开发 (7):本地化应用程序栏Localizing an Application Bar 下...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置润乾报表鼠标移到格子上就显示提示内容
- 下一篇: java例程练习(一维数组)