今天大清早起牀打開微信朋友圈看到大學同窗院一哥們發的結婚的照片,在此遙祝一對新人:新婚快樂,百年好合!這哥們大學時時班長,結婚也來了好多同窗,不禁得以爲吾等屌絲大學確實留下了不少遺憾~哦,對了,這哥們還跟我如今在一個公司的不一樣部門裏,據說他也混的如魚得水,祝福!前端
轉到正題,今天想記錄的知識點有三個:c#
1.wpf樣式實現安卓和蘋果移動終端上的滑塊式開關;微信
2.wpf轉換器的使用;ide
3.c#對xml文件的操做;動畫
說說需求,項目上要手動打開配置文件修改裏面的值,確切來講是在true和false中間切換裏面一個配置的值。想着給客戶上線使用,讓他們打開配置文件切換值確定是很差的,就想着作個圖形界面的開關。以爲手機終端的滑塊式開關不錯,就本身寫了一個。先看手機上的滑塊式開關是什麼樣的:spa
對,就要這種效果。調試
先看XAML界面的代碼吧:code
1 <Window x:Class="ModifyConfig.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:local="clr-namespace:ModifyConfig" 5 Title="MainWindow" 6 Width="525" 7 Height="350"> 8 <Window.Resources> 9 10 <local:GradeState x:Key="GradeState" /> 11 12 <Style x:Key="CheckRadioFocusVisual"> 13 <Setter Property="Control.Template"> 14 <Setter.Value> 15 <ControlTemplate> 16 <Rectangle Margin="14,0,0,0" 17 SnapsToDevicePixels="true" 18 Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" 19 StrokeDashArray="1 2" 20 StrokeThickness="1" /> 21 </ControlTemplate> 22 </Setter.Value> 23 </Setter> 24 </Style> 25 <Style x:Key="SliderCheckBox" TargetType="{x:Type CheckBox}"> 26 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" /> 27 <Setter Property="BorderThickness" Value="1" /> 28 <Setter Property="Cursor" Value="Hand" /> 29 <Setter Property="Template"> 30 <Setter.Value> 31 <ControlTemplate TargetType="{x:Type CheckBox}"> 32 <ControlTemplate.Resources> 33 <Storyboard x:Key="StoryboardIsChecked"> 34 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> 35 <EasingDoubleKeyFrame KeyTime="0" Value="0" /> 36 <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="14" /> 37 </DoubleAnimationUsingKeyFrames> 38 </Storyboard> 39 <Storyboard x:Key="StoryboardIsCheckedOff"> 40 <DoubleAnimationUsingKeyFrames Storyboard.TargetName="CheckFlag" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)"> 41 <EasingDoubleKeyFrame KeyTime="0" Value="14" /> 42 <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0" /> 43 </DoubleAnimationUsingKeyFrames> 44 </Storyboard> 45 </ControlTemplate.Resources> 46 <BulletDecorator Background="Transparent" SnapsToDevicePixels="true"> 47 <BulletDecorator.Bullet> 48 <Border x:Name="ForegroundPanel" 49 Width="35" 50 Height="20" 51 BorderThickness="1" 52 CornerRadius="10"> 53 <Canvas> 54 <Border x:Name="CheckFlag" 55 Width="19" 56 Height="18" 57 VerticalAlignment="Center" 58 Background="{Binding Converter={StaticResource GradeState} }" 59 BorderThickness="1" 60 CornerRadius="10" 61 RenderTransformOrigin="0.5,0.5"> 62 <Border.RenderTransform> 63 <TransformGroup> 64 <ScaleTransform /> 65 <SkewTransform /> 66 <RotateTransform /> 67 <TranslateTransform /> 68 </TransformGroup> 69 </Border.RenderTransform> 70 <Border.Effect> 71 <DropShadowEffect Direction="180" ShadowDepth="1" /> 72 </Border.Effect> 73 </Border> 74 </Canvas> 75 </Border> 76 </BulletDecorator.Bullet> 77 <ContentPresenter Margin="{TemplateBinding Padding}" 78 HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 79 VerticalAlignment="Center" 80 RecognizesAccessKey="True" 81 SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 82 </BulletDecorator> 83 <ControlTemplate.Triggers> 84 <Trigger Property="HasContent" Value="true"> 85 <Setter Property="FocusVisualStyle" Value="{StaticResource CheckRadioFocusVisual}" /> 86 <Setter Property="Padding" Value="4,0,0,0" /> 87 </Trigger> 88 <Trigger Property="IsEnabled" Value="false"> 89 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 90 </Trigger> 91 <Trigger Property="IsChecked" Value="True"> 92 <Setter TargetName="ForegroundPanel" Property="Background" Value="LightGreen" /> 93 <Trigger.EnterActions> 94 <BeginStoryboard x:Name="BeginStoryboardCheckedTrue" Storyboard="{StaticResource StoryboardIsChecked}" /> 95 <RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedFalse" /> 96 </Trigger.EnterActions> 97 </Trigger> 98 <Trigger Property="IsChecked" Value="False"> 99 <Setter TargetName="ForegroundPanel" Property="Background" Value="LightGray" /> 100 <Trigger.EnterActions> 101 <BeginStoryboard x:Name="BeginStoryboardCheckedFalse" Storyboard="{StaticResource StoryboardIsCheckedOff}" /> 102 <RemoveStoryboard BeginStoryboardName="BeginStoryboardCheckedTrue" /> 103 </Trigger.EnterActions> 104 </Trigger> 105 </ControlTemplate.Triggers> 106 </ControlTemplate> 107 </Setter.Value> 108 </Setter> 109 </Style> 110 </Window.Resources> 111 <Canvas> 112 <TextBlock Text="是否開啓考試模式:" Canvas.Left="27" Canvas.Top="12" /> 113 <CheckBox Width="200" 114 Height="23" 115 Style="{DynamicResource SliderCheckBox}" 116 x:Name="Switch_IsExamMode" 117 Click="IsExamMode_Click" Canvas.Left="132" Canvas.Top="12" /> 118 119 </Canvas> 120 </Window>
下面的是後臺cs文件的代碼:orm
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Windows; 6 using System.Windows.Controls; 7 using System.Windows.Data; 8 using System.Windows.Documents; 9 using System.Windows.Input; 10 using System.Windows.Media; 11 using System.Windows.Media.Imaging; 12 using System.Windows.Navigation; 13 using System.Windows.Shapes; 14 using System.Xml; 15 using System.Globalization; 16 17 namespace ModifyConfig 18 { 19 /// <summary> 20 /// MainWindow.xaml 的交互邏輯 21 /// </summary> 22 public partial class MainWindow : Window 23 { 24 /// <summary> 25 /// 是否考試模式 26 /// </summary> 27 private bool _isExamMode; 28 29 /// <summary> 30 /// 是否考試模式 31 /// </summary> 32 public bool IsExamMode 33 { 34 get { return _isExamMode; } 35 set { _isExamMode = value; } 36 } 37 38 /// <summary> 39 /// 主窗口初始化 40 /// </summary> 41 public MainWindow() 42 { 43 InitializeComponent(); 44 Switch_IsExamMode.IsChecked = GetIsExamMode(); 45 IsExamMode = (bool)Switch_IsExamMode.IsChecked; 46 Switch_IsExamMode.DataContext = IsExamMode; 47 } 48 /// <summary> 49 /// 點擊開關觸發的事件 50 /// </summary> 51 /// <param name="sender"></param> 52 /// <param name="e"></param> 53 private void IsExamMode_Click(object sender, RoutedEventArgs e) 54 { 55 IsExamMode = (bool)Switch_IsExamMode.IsChecked; 56 Switch_IsExamMode.DataContext = IsExamMode; 57 SetIsExamMode(); 58 } 59 /// <summary> 60 /// 獲得XML文件中的節點值 61 /// </summary> 62 /// <returns></returns> 63 private bool GetIsExamMode() 64 { 65 XmlDocument xmlDoc = new XmlDocument(); 66 xmlDoc.Load(@"..\..\..\config\Platform.System.Config"); 67 XmlNodeList accountList = xmlDoc.SelectSingleNode("configuration").ChildNodes; 68 foreach (XmlNode account in accountList) 69 { 70 if (account.Name == "add" && account.Attributes["key"].Value.ToString() == "InExamMode") 71 { 72 return account.Attributes["value"].Value == "True" ? true : false; 73 } 74 } 75 return false; 76 } 77 /// <summary> 78 /// 向xml文件中寫入值 79 /// </summary> 80 private void SetIsExamMode() 81 { 82 XmlDocument xmlDoc = new XmlDocument(); 83 xmlDoc.Load(@"..\..\..\config\Platform.System.Config"); 84 XmlNodeList accountList = xmlDoc.SelectSingleNode("configuration").ChildNodes; 85 foreach (XmlNode account in accountList) 86 { 87 if (account.Name == "add") 88 { 89 //if account exists 90 if ("InExamMode" == account.Attributes["key"].Value.ToString()) 91 { 92 if ((bool)Switch_IsExamMode.IsChecked) 93 { 94 account.Attributes["value"].Value = "True"; 95 } 96 else 97 { 98 account.Attributes["value"].Value = "False"; 99 } 100 xmlDoc.Save(@"..\..\..\config\Platform.System.Config"); 101 } 102 } 103 } 104 } 105 } 106 /// <summary> 107 /// 轉換器 108 /// </summary> 109 public class GradeState : IValueConverter 110 { 111 public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 112 { 113 if (value == null) 114 return null; 115 dynamic data = value; 116 if ((bool)data) 117 return new SolidColorBrush(Colors.Green); 118 else 119 return new SolidColorBrush(Colors.Gray); 120 } 121 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 122 { 123 throw new NotImplementedException(); 124 } 125 } 126 }
樣式實現滑塊開關沒什麼難度,說說轉換器,轉換器的使用,1.增長一個轉換器的類,它繼承於接口IValueConverter,2.實現該接口的兩個方法,Convert(object value, Type targetType, object parameter, CultureInfo culture)和ConvertBack(object value, Type targetType, object parameter, CultureInfo culture);第二個方法通常都是throw new NotImplementedException();3.XAML界面資源裏聲明該轉換器(第10行);4.XAML界面須要轉換的地方使用該轉換器(第58行)。xml
xml文件的操做我這邊比較簡單,有讀取節點屬性值和設置節點值兩個功能。
下面是咱們的配置文件Platform.System.Config
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <add key="TradeMainVersion" value="1" /> 4 <add key="IsDebugMode" value="False" /> 5 <add key="IsStartHardDiskNumber" value="False" /> 6 <add key="PlatformTitle" value="綜合業務前端系統" /> 7 <add key="PagesDirectory" value="TellerSysPages" /> 8 <add key="UpdateApplication" value="TellerSystem.Update.exe" /> 9 <add key="TempFilePath" value="tmp/" /> 10 <add key="ConfigFilePath" value="TellerSysconf/" /> 11 <!--是否啓動動畫--> 12 <add key="AnimationEnable" value="True" /> 13 <!--是否啓用權限控制--> 14 <add key="IsPowerCheckByUser" value="False" /> 15 <!--啓用項目層更新交易dll True:項目層更新,False:底層更新 --> 16 <add key="IsFromProjectDownLoadTradeDll" value="False" /> 17 <!--是否啓用客戶端更新--> 18 <add key="IsUpdateCheckForClient" value="True" /> 19 <!--是否越過支付密碼器檢查--> 20 <add key="IsSkipCheckPayPassword" value="True" /> 21 <!--是否越過驗印流水檢查--> 22 <add key="IsSkipCheckYanYinWater" value="True" /> 23 <!--是否啓用考試模式--> 24 <add key="InExamMode" value="False" /> 25 <!--是否外設調試模式--> 26 <add key="InDeviceDebugMode" value="True" /> 27 </configuration>
看看最後的運行結果:
關閉時:
開啓時:
下載源碼壓縮包連接:http://files.cnblogs.com/files/zhangyongheng/ModifyConfig.rar
2015-05-05 10:43:53