wp7之换肤原理简单分析

纠结很久。。。感觉勉强过得去啦。还望各位大牛指点江山

百度找到这篇参考文章http://www.cnblogs.com/sonyye/archive/2012/03/12/2392679.html#2329255

还有这篇:http://www.cnblogs.com/xumingxiang/archive/2012/03/23/2413342.html

进入正文吧

1  UI代码很简单,一个listbox 和一个标题textblock,一个button按钮


 
<phone:PhoneApplicationPage
x:Class="ChangeSkin.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" >
<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="我的应用程序" />
<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="lb">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Name="lbText"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="换皮肤" HorizontalAlignment="Left" Margin="157,471,0,0" VerticalAlignment="Top" Click="Button_Click_1" /> </Grid>
</Grid>
</phone:PhoneApplicationPage>

截图如下:

2 新建一个皮肤资源文件,

(ps:这里比较繁琐:新建一个页面文件,删除cs文件,然后找一下这个路径资源文件来参考 C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.1\Design\DarkBlue\System.Windows.xaml ,为啥微软不直接提供一个新建皮肤文件?????)

设置Resource,非常重要啊,看下图

皮肤里面文件代码:


 
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib">
<Style TargetType="TextBlock" x:Key="tbox">
<Setter Property="Foreground" Value="White"></Setter>
</Style>
</ResourceDictionary>

3、后台代码


 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
namespace ChangeSkin
{
public class SkinsHelper
{
/// <summary>
/// 得到皮肤资源文件
/// </summary>
/// <param name="Light"></param>
/// <returns></returns>
public static ResourceDictionary GetSkinResource(SkinsType Light = SkinsType.Green)
{
ResourceDictionary resourceDictionary = new ResourceDictionary();
string uri = "/ChangeSkin;component/Skins/";
//这里一定要设置Black.xaml生成操作为Resource
switch (Light)
{
case SkinsType.Green: uri += "Green.xaml"; break;
case SkinsType.Black: uri += "Black.xaml"; break;
default: uri += "Green.xaml"; break;
}
Application.LoadComponent(resourceDictionary, new Uri(uri, UriKind.Relative));
Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
return resourceDictionary;
}
}
public enum SkinsType
{
Green = 0,
Black = 1
}
}

4 工具类


 
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
namespace JM.Common
{
public class VisualHelper
{
//获取子类型
public static T FindChildOfType<T>(DependencyObject root) where T : class
{
var queue = new Queue<DependencyObject>();
queue.Enqueue(root);
while (queue.Count > 0)
{
DependencyObject current = queue.Dequeue();
for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--)
{
var child = VisualTreeHelper.GetChild(current, i);
var typedChild = child as T;
if (typedChild != null)
{
return typedChild;
}
queue.Enqueue(child);
}
}
return null;
}
public static List<T> GetVisualChildCollection<T>(object parent) where T : UIElement
{
List<T> visualCollection = new List<T>();
GetVisualChildCollection(parent as DependencyObject, visualCollection);
return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : UIElement
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(parent, i);
if (child is T)
{
visualCollection.Add(child as T);
}
else if (child != null)
{
GetVisualChildCollection(child, visualCollection);
}
}
}
}
}

5 调用方法


 
private void SetSkins(SkinsType skin = SkinsType.Black)
{
SkinsHelper.GetSkinResource(skin);
this.ApplicationTitle.SetValue(TextBlock.StyleProperty, Application.Current.Resources["tbox"]);
//listbox文字颜色样式改变
List<TextBlock> tbList= VisualHelper.GetVisualChildCollection<TextBlock>(lb);
foreach (TextBlock textBlock in tbList)
{
textBlock.SetValue(TextBlock.StyleProperty, Application.Current.Resources["tbox"]);
}
}

测试结果:
测试结果:
测试结果:文字改变绿色

下载代码

后记,如果想切换背景,添加更多效果,请增加样式,再写上对应调用样式代码

 
 
分类: WP7

wp7之换肤原理简单分析的更多相关文章

  1. Android中插件开发篇之----应用换肤原理解析

    一.前言 今天又到周末了,感觉时间过的很快呀.又要写blog了.那么今天就来看看应用的换肤原理解析.在之前的一篇博客中我说道了Android中的插件开发篇的基础:类加载器的相关知识.没看过的同学可以转 ...

  2. AbstractRoutingDataSource 实现动态数据源切换原理简单分析

    AbstractRoutingDataSource 实现动态数据源切换原理简单分析 写在前面,项目中用到了动态数据源切换,记录一下其运行机制. 代码展示 下面列出一些关键代码,后续分析会用到 数据配置 ...

  3. mr原理简单分析

    背景 又是一个周末一天一天的过的好快,今天的任务干啥呢,索引总结一些mr吧,因为前两天有面试问过我?我当时也是简单说了一下,毕竟现在写mr程序的应该很少很少了,废话不说了,结合官网和自己理解写起. 官 ...

  4. DevExpress 12.1 换肤 超级简单的方法(2013-11-5版)

    本例子是按照DevExpress 12.1 版本 进行演示.请先准备好DevExpress.BonusSkins.v12.1.dll 和DevExpress.Utils.v12.1.dll 1.首先添 ...

  5. DWM1000 测距原理简单分析 之 SS-TWR

    蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: DWM1000 超宽带测距,使用的TOF(time of fly) 的方式,也就是计算无线电磁波传输时 ...

  6. DWM1000 测距原理简单分析 之 SS-TWR代码分析2 -- [蓝点无限]

    蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: 首先将SS 原理介绍中的图片拿过来,将图片印在脑海里. 对于DeviceA 和 DeviceB来说,初 ...

  7. DWM1000 测距原理简单分析 之 SS-TWR代码分析1 -- [蓝点无限]

    蓝点DWM1000 模块已经打样测试完毕,有兴趣的可以申请购买了,更多信息参见 蓝点论坛 正文: 这一篇内容主要是通过官方源码理解SS-TWR 细节 代码下载链接:https://download.c ...

  8. Spring整合Mybatis原理简单分析

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" ...

  9. Android APK方式换肤实现原理

    现在很多APP都有换肤的功能,例如微博,QQ等应用.这些应用的换肤原理是什么? 在用微博的时候,不难发现,当你要换肤时,先下载并安装一个皮肤apk,然后选择这个皮肤,就可以了. 这种方式就是把皮肤打包 ...

随机推荐

  1. 解决VS 于 致命错误 RC1015: 无法打开包含文件 &#39;afxres.h&#39; 问题

    在试验VS2010当一个问题困扰了我,它是开放的c++项目达产后,rc的dialog入口.您不能拖动控制,让我疯狂... 而最有发言权的是在线Directions问题. .题明显不是这个问题. 于是我 ...

  2. 第一个JavaWeb程序

    转载 第一个JavaWeb程序 JavaWeb学习总结第二篇—第一个JavaWeb程序 最近我在学院工作室学习并加入到研究生的项目中,在学长学姐的带领下,进入项目实践中,为该项目实现一个框架(用已有框 ...

  3. MongoDB在window下的安装

    1.下载mongodb的windows版本号,有32位和64位版本号,依据系统情况下载,下载地址:http://www.mongodb.org/downloads 2.解压缩至D:/mongodb就可 ...

  4. git合并远端分支到本地分支的两种方式

    作者:zhanhailiang 日期:2014-11-21 在使用版本号工具提交改动之前,都须要通过update先将本地代码更新到最新版本号.SVN通过svn update就能够实现,那么git怎样实 ...

  5. [LeetCode]Copy List with Random Pointer &amp;Clone Graph 复杂链表的复制&amp;图的复制

    /** * Definition for singly-linked list with a random pointer. * struct RandomListNode { * int label ...

  6. 最新HTML BroadcastChannel API引荐

    HTML BroadcastChannel API 当前浏览器中只有Firefox38唯一能支持BroadcastChannel API(在编写本文的时间点),而Firefox38官方宣称要到2015 ...

  7. sql点滴42—mysql中的数据结构

    原文:sql点滴42-mysql中的数据结构 MySQL 的数值数据类型可以大致划分为两个类别,一个是整数,另一个是浮点数或小数.许多不同的子类型对这些类别中的每一个都是可用的,每个子类型支持不同大小 ...

  8. SQL点滴20—T-SQL中的排名函数

    原文:SQL点滴20-T-SQL中的排名函数 提到排名函数我们首先可能想到的是order by,这个是排序,不是排名,排名需要在前面加个名次序号的,order by是没有这个功能的.还可能会想到ide ...

  9. VMware Linux 下 Nginx

    负载   VMware Linux 下 Nginx 安装配置 - nginx.conf 配置 [负载两个 Tomcat] (三) Weiseditor 2014-11-26 23:42 阅读:1 评论 ...

  10. CSS边框阴影效果

    ul { margin:5px 0 0 0; padding:0; list-style:none; width:300px; background:#f1f1f1; }li { border-lef ...