none
Windows Mobile 10中如何实现动态换肤的功能呢? RRS feed

  • 问题

  • 在Windows Mobile 10 中App有3套控件颜色的xaml文件, 如何实现当点击红色按钮整个App中的所有Page的颜色都变成红色的呢?

    同理当点击蓝色按钮,App中的所有Page都更改为蓝色呢??

    并且在整个换肤的过程中不需要重新启动软件.

    2016年1月7日 5:53

答案

  • 您好,

    你需要在Page的resource中定义三种样式,然后当你点击某个按钮时,获取对应的样式,并指定给page,具体请参考我的代码:

    <Page
        x:Class="App4.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App4"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
    
        <Page.Resources>
            <Style x:Key="color1" TargetType="Page">
                <Setter Property="Background" Value="Red"></Setter>
            </Style>
    
            <Style x:Key="color2" TargetType="Page">
                <Setter Property="Background" Value="Blue"></Setter>
            </Style>
    
            <Style x:Key="color3" TargetType="Page">
                <Setter Property="Background" Value="Yellow"></Setter>
            </Style>
        </Page.Resources>
        
        <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="50">
            <Button Content="red" Click="Button_Click"></Button>
            <Button Content="blue" Click="Button_Click"></Button>
            <Button Content="yellow" Click="Button_Click"></Button>
        </StackPanel>
    </Page>
    private void Button_Click(object sender, RoutedEventArgs e)
            {
                Button btn = (Button)sender;
                switch (btn.Content.ToString())
                {
                    case "red":
                        this.Style = Resources["color1"] as Style;break;
                    case "blue":
                        this.Style = Resources["color2"] as Style;break;
                    case "yellow":
                        this.Style = Resources["color3"] as Style;break;
                    default:break;
                }
            }
    这里有个地方需要注意一下,一般page里面会有一个Grid或者StackPanel,默认的这个容器是和Page一样大小的,所以如果你没有给这个容器指定大小的话,当你给page指定样式时,你会看不到任何变化,其实是被这个容器挡住的缘故。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 花開灬 2016年1月23日 6:31
    2016年1月20日 7:23

全部回复

  • 您好,

    你需要在Page的resource中定义三种样式,然后当你点击某个按钮时,获取对应的样式,并指定给page,具体请参考我的代码:

    <Page
        x:Class="App4.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:App4"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d">
    
    
        <Page.Resources>
            <Style x:Key="color1" TargetType="Page">
                <Setter Property="Background" Value="Red"></Setter>
            </Style>
    
            <Style x:Key="color2" TargetType="Page">
                <Setter Property="Background" Value="Blue"></Setter>
            </Style>
    
            <Style x:Key="color3" TargetType="Page">
                <Setter Property="Background" Value="Yellow"></Setter>
            </Style>
        </Page.Resources>
        
        <StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="50">
            <Button Content="red" Click="Button_Click"></Button>
            <Button Content="blue" Click="Button_Click"></Button>
            <Button Content="yellow" Click="Button_Click"></Button>
        </StackPanel>
    </Page>
    private void Button_Click(object sender, RoutedEventArgs e)
            {
                Button btn = (Button)sender;
                switch (btn.Content.ToString())
                {
                    case "red":
                        this.Style = Resources["color1"] as Style;break;
                    case "blue":
                        this.Style = Resources["color2"] as Style;break;
                    case "yellow":
                        this.Style = Resources["color3"] as Style;break;
                    default:break;
                }
            }
    这里有个地方需要注意一下,一般page里面会有一个Grid或者StackPanel,默认的这个容器是和Page一样大小的,所以如果你没有给这个容器指定大小的话,当你给page指定样式时,你会看不到任何变化,其实是被这个容器挡住的缘故。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • 已标记为答案 花開灬 2016年1月23日 6:31
    2016年1月20日 7:23
  • 非常感谢  谢谢你的帮助 
    2016年1月23日 6:32