windows phone 换肤(2)

//这里有篇参考文章

http://www.cnblogs.com/tianhonghui/p/3373276.html#commentform

 

以下思路是来自徐老师,昨晚看了一个晚上球赛,睡了不到6个小时,又被人拉去游泳,现在各种困。。。

文字我就不多描述,直接上代码吧。

样式style

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
                    xmlns:my="clr-namespace:System;assembly=mscorlib"
                    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
                    xmlns:System="clr-namespace:System;assembly=mscorlib">
    <!--自定义一个画刷,也可以用系统,感觉不好控制,还是自己定义的好,实际项目没实战过,到时拿1、2项目练练手在汇报-->
    <SolidColorBrush x:Key="S_SolidColorBrush">
        <Color>#0478fb</Color>
    </SolidColorBrush>

    <!--标题定义样式-->
    <Style TargetType="TextBlock" x:Key="tbTitle">
        <Setter Property="Foreground" Value="{StaticResource S_SolidColorBrush}" />
        <Setter Property="FontSize" Value="56" />
    </Style>

    <!--ListBox里面文字定义样式-->
    <Style TargetType="TextBlock" x:Key="listboxTitle">
        <Setter Property="Foreground" Value="{StaticResource S_SolidColorBrush}" />
        <Setter Property="FontSize" Value="32" />
    </Style>
</ResourceDictionary>

 

切换皮肤帮助类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace 换皮肤WP
{

    public class ThemeHelper
    {
        /// <summary>
        /// 设置默认为绿色
        /// </summary>
        /// <param name="type"></param>
        public static void ChangeTheme(ThemeType type = ThemeType.Green)
        {
            //默认颜色值
            var color = Color.FromArgb(255, 0x04, 0x78, 0xfb);
            //var color = (Color)Application.Current.Resources["S_FontColor"];
            switch (type)
            {
                case ThemeType.Green:
                    //color=Colors.Green;
                    //color=Color.FromArgb(255,4,120,255);
                    //上面方法都不推荐这么写,因为颜色值往往是从ps取出来。16进制0478fb
                    color = Color.FromArgb(255, 0x04, 0x78, 0xfb);
                    break;
                case ThemeType.Blue:
                    color = Color.FromArgb(255, 0x04, 0xfb, 0x72);
                    break;
                default:
                    //找不到也是默认值
                    color = Color.FromArgb(255, 0x04, 0x78, 0xfb);
                    break;
            }

            SetColor("S_FontColor", color);
        }

        /// <summary>
        /// 设置颜色
        /// </summary>
        /// <param name="key"></param>
        /// <param name="color"></param>
        private static void SetColor(string key, Color color)
        {
            if (Application.Current.Resources.Contains("S_SolidColorBrush"))
            {
                var brush = (SolidColorBrush)Application.Current.Resources["S_SolidColorBrush"];
                brush.Color = color;
            }
        }
    }

    public enum ThemeType
    {
        Green = 1,
        Blue = 2,
        //…自定义定义更多颜色
    }
}

 

MainPage.xaml

<phone:PhoneApplicationPage 
    x:Class="换皮肤WP.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="换肤Demo" Margin="9,-7,0,0" Style="{StaticResource tbTitle}"/>
        </StackPanel>

        <!--ContentPanel - 在此处放置其他内容-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <ListBox ItemsSource="{Binding Words}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" Style="{StaticResource listboxTitle}" />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <Button Content="绿色" HorizontalAlignment="Left" Margin="82,512,0,0" VerticalAlignment="Top" Height="85" Click="Button_Click_1"/>
            <Button Content="黄色" HorizontalAlignment="Left" Margin="303,512,0,0" VerticalAlignment="Top" Height="85" Click="Button_Click_2"/>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

 

 

MainPage.xaml.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;

namespace 换皮肤WP
{
    public partial class MainPage : PhoneApplicationPage
    {
        //mvvm如果属性放到这里,绑定看构造函数
        public List<string> Words { get; set; }

        // 构造函数
        public MainPage()
        {
            InitializeComponent();

            ThemeHelper.ChangeTheme();

            //测试列表里面文字换肤
            Words = new List<string> 
            {
               "Hello word",
               "Kennie",
               "no zuo no die"
            };

            this.DataContext = this;
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            ThemeHelper.ChangeTheme();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            ThemeHelper.ChangeTheme(ThemeType.Blue);
        }
    }
}

 

App引用样式

WP7<ResourceDictionary Source="Style/common.xaml" />

WP8写法不一样,自行去百度,实在不太多人不懂话,代码稍后再贴

 

下载Demo

 

 

posted @ 2014-06-15 02:35  walleyekneel  阅读(794)  评论(1编辑  收藏  举报