modern ui for wpf 持久化风格设置

modern ui for wpf 自带的颜色设置下次打开不能保存,再次给出一个完整解决方案。 

再次申明:以下内容翻译至:http://gordonbeeming.azurewebsites.net/2013/05/12/persisting-modern-ui-for-wpf-styles/

第一步:在vs中添加一个settings文件(ApplicationSettings.settings),添加4个settings

1.)

Name : SelectedAccentColor, Type : System.Windows.Media.Color, Scope : User, Value: #FF1BA1E2

2.)

Name : SelectedThemeSource, Type : System.Uri, Scope : User, Value : /FirstFloor.ModernUI;component/Assets/ModernUI.Dark.xaml

3.)

Name : SelectedThemeDisplayName, Type : string, Scope : User, Value : dark

4.)

Name : SelectedFontSize, Type : string, Scope : User, Value : large

第二步:打开Content\SettingsAppearanceViewModel.cs,添加一个私有的bool变量_colorLoadedYet 

1 private bool _colorLoadedYet;

第三步:找到SyncThemeAndColor方法,在最下方添加下方代码:

1 if (this._colorLoadedYet)
2 {
3     ApplicationSettings.Default.SelectedThemeDisplayName = this.SelectedTheme.DisplayName;
4     ApplicationSettings.Default.SelectedThemeSource = this.SelectedTheme.Source;
5     ApplicationSettings.Default.SelectedAccentColor = this.SelectedAccentColor;
6     ApplicationSettings.Default.SelectedFontSize = this.SelectedFontSize;
7     ApplicationSettings.Default.Save();
8 }

第四步:添加一个方法如下:

1 public void SetThemeAndColor(string themeSourceDisplayName, Uri themeSourceUri, Color accentColor, string fontSize)
2 {
3     this.SelectedTheme = new Link { DisplayName = themeSourceDisplayName, Source = themeSourceUri };
4     this.SelectedAccentColor = accentColor;
5     this.SelectedFontSize = fontSize;
6     this._colorLoadedYet = true;
7 }

第五步:找到SelectedFontSize 属性,在this.OnPropertyChanged(“SelectedFontSize”); 添加如下代码:

1 if (_colorLoadedYet)
2 {
3     ApplicationSettings.Default.SelectedFontSize = this.selectedFontSize;
4     ApplicationSettings.Default.Save();
5 }

最后一步:打开MainWindow.xaml.cs文件,在InitializeComponent();后面添加如下代码:

1 SettingsAppearanceViewModel settings = new SettingsAppearanceViewModel();
2 settings.SetThemeAndColor(ApplicationSettings.Default.SelectedThemeDisplayName,
3       ApplicationSettings.Default.SelectedThemeSource, 
4       ApplicationSettings.Default.SelectedAccentColor, 
5       ApplicationSettings.Default.SelectedFontSize);

到此全部工作完成,重新编译运行,看看效果了。

第一次写博客,欢迎大家收藏,谢谢!

posted @ 2014-07-10 09:32  cloud602  阅读(495)  评论(0编辑  收藏  举报