28 March 2017

Windows Store và Language: Thay đổi Languages trên App Windows Store C#

MainPage.xaml
Windows Store 2017
<Page
    x:Class="LocalizationSample.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LocalizationSample"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
   
        <ComboBox x:Name="cbbLanguage" SelectedValuePath="Name" HorizontalAlignment="Left" Margin="392,98,0,0" VerticalAlignment="Top" Width="192">
            <ComboBoxItem x:Name="en" Content="English"/>
            <ComboBoxItem x:Name="vi" Content="VietNam"/>
            <ComboBoxItem x:Name="ja" Content="Japan"/>
        </ComboBox>
        <Button x:Name="btnChangeLanguage" Content="Change Language" HorizontalAlignment="Left" Margin="634,95,0,0" VerticalAlignment="Top" Click="btnChangeLanguage_Click"/>
        <Image x:Name="image1" HorizontalAlignment="Left" Height="164" Margin="38,88,0,0" VerticalAlignment="Top" Width="312"/>
        <TextBlock x:Name="tbString" HorizontalAlignment="Left" Margin="401,196,0,0" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" FontSize="20"/>

    </Grid>
</Page>
MainPage.xaml.cs
Windows Store 2017
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Resources.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace LocalizationSample
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        private void btnChangeLanguage_Click(object sender, RoutedEventArgs e)
        {
            var c = ResourceContext.GetForCurrentView();
            var list = new List<string>();
            //Get value from Combobox
            list.Add(cbbLanguage.SelectedValue.ToString());
            c.Languages = list;

            var r = ResourceManager.Current.MainResourceMap.GetSubtree("Resources");
            //Set Text vs Images
            tbString.Text = r.GetValue("AppString",c).ValueAsString;
            image1.Source = new BitmapImage(new Uri("ms-appx:///" + r.GetValue("PathFlag", c).ValueAsString));

        }
    }
}
Download Icon vs ToolKit (Cần cài đặt Toolkit )

0 nhận xét:

Post a Comment

 

BACK TO TOP

Xuống cuối trang