WP7 换肤效能实现

WP7 换肤功能实现

目录结构:

WP7 换肤效能实现

==========================

设计页面代码:

<phone:PhoneApplicationPage
    x:Class="换肤.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">
            <ListBox Name="lbSkin" Width="50" Height="80" FontSize="24" SelectionChanged="lbSkin_SelectionChanged">
                <ListBoxItem Content="红色" Name="red" />
                <ListBoxItem Content="绿色" Name="green" />
            </ListBox>
        </Grid>
    </Grid>

</phone:PhoneApplicationPage>

WP7 换肤效能实现

============================================

程序代码:

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 换肤
{
    public partial class MainPage : PhoneApplicationPage
    {
        // 构造函数
        public MainPage()
        {
            InitializeComponent();
        }

        private void lbSkin_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (lbSkin.SelectedItem==red)
            {
               
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri("/换肤;component/skin/red.xaml", UriKind.Relative);
                App.Current.Resources.MergedDictionaries.Clear();
                App.Current.Resources.MergedDictionaries.Add(rd);

            }
             if (lbSkin.SelectedItem == green)
            {
                ResourceDictionary rd = new ResourceDictionary();
                rd.Source = new Uri("/换肤;component/skin/green.xaml", UriKind.Relative);
                App.Current.Resources.MergedDictionaries.Clear();
                App.Current.Resources.MergedDictionaries.Add(rd);
            }
         
        }
    }
}

 

=========================

红色皮肤:

<ResourceDictionary
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Grid" >
        <Setter Property="Background" Value="Red" >
        </Setter>
    </Style>
</ResourceDictionary>

========================

绿色皮肤:

<ResourceDictionary
    xmlns="
http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="
http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Grid" >
        <Setter Property="Background" Value="Green" >
        </Setter>
    </Style>
</ResourceDictionary>

=======================