WPF实战学习笔记07-设置备忘录以及设置界面

设置备忘录以及设置界面

设置备忘录界面

同待办事项,相关名字改掉,类tododto改成memodto即可

添加文件

./Views/SkinView.xaml

./Views/SysSetView.xaml

./Views/AboutView.xaml

./ViewModels/SkinViewModel.cs

./ViewModels/SysSetViewModel.cs

./ViewModels/AboutViewModel.cs

添加子区域

添加变量

  • PrismManager.cs

    public static readonly string SettingsViewRgionName = "SettingsViewRgion";
    

注册区域

  • SettingsView.xaml

    
    

SettingsView.xaml

从MainView.xaml处复制过来


    
        
            
            
        
        
        
            
                
                    
                        
                    
                
                
                    
                        
                            
                            
                        
                    
                
            
            
        
    


SettingsViewModel.cs

using Mytodo.Common.Models;
using Mytodo.Extensions;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mytodo.ViewModels
{
    public class SettingsViewModel:BindableBase
    {
        public SettingsViewModel(IRegionManager rgm)
        {
            regionManager = rgm;

            CreatMenuBar();

            //关联命令与操作
            NavigateCmd = new DelegateCommand(Navigate);
        }

        #region 菜单栏绑定变量定义以及初始化

        private ObservableCollection menuBars;

        public ObservableCollection MenuBars
        {
            get { return menuBars; }
            set { menuBars = value; RaisePropertyChanged(); }
        }

        void CreatMenuBar()
        {
            MenuBars = new ObservableCollection();

            MenuBars.Add(new MenuBar { Icon = "Home", NameSpace = "SkinView", Title = "个性化" });
            MenuBars.Add(new MenuBar { Icon = "FormatListChecks", NameSpace = "SysSetView", Title = "系统设置" });
            MenuBars.Add(new MenuBar { Icon = "Notebook", NameSpace = "AboutView", Title = "关于更多" });
        }

        #endregion



        #region 定义打开命令变量以及初始化命令操作函数
        /// 
        /// 定义导航变量
        /// 
        private readonly IRegionManager regionManager;
        /// 
        /// 定义命令
        /// 
        public DelegateCommand NavigateCmd { get; private set; }

        /// 
        /// 定义导航操作
        /// 
        /// 
        private void Navigate(MenuBar obj)
        {
            if (obj == null || string.IsNullOrWhiteSpace(obj.NameSpace))
                return;
            regionManager.Regions[PrismManager.SettingsViewRgionName].RequestNavigate(obj.NameSpace);
        }

        #endregion
    }
}

添加App.xaml,关联view和viewmodel

//建立view与viewmodel的关系
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
    containerRegistry.RegisterForNavigation();
    containerRegistry.RegisterForNavigation();
    containerRegistry.RegisterForNavigation();
    containerRegistry.RegisterForNavigation();
    containerRegistry.RegisterForNavigation();
    containerRegistry.RegisterForNavigation();
    containerRegistry.RegisterForNavigation();
}

编辑skinview

skinview.xaml

主要从materialDesignDemo项目复制,需要更改项目名称,引用空间,另需要添加转换器以及对应的转换器资源key


    
        
        
        
            
        
    
    
        
            
            
        
        
            

            

            
        
        
            
                
                    
                        
                        
                            
                                
                                    
                                
                            
                        
                    
                
            
        
    


skinviewmodel.cs

都是从materialDesignDemo的colortool对应的.cs中复制过来,除了Swatches

using MaterialDesignColors;
using MaterialDesignColors.ColorManipulation;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;

namespace Mytodo.ViewModels
{
    class SkinViewModel : BindableBase
    {
        public IEnumerable Swatches { get; } = SwatchHelper.Swatches;
        private readonly PaletteHelper paletteHelper = new PaletteHelper();
        private bool _isDarkTheme;
        public bool IsDarkTheme
        {
            get => _isDarkTheme;
            set
            {
                if (SetProperty(ref _isDarkTheme, value))
                {
                    ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
                }
            }
        }

        public IEnumerable ContrastValues => Enum.GetValues(typeof(Contrast)).Cast();
        private static void ModifyTheme(Action modificationAction)
        {
            var paletteHelper = new PaletteHelper();

            ITheme theme = paletteHelper.GetTheme();

            modificationAction?.Invoke(theme);

            paletteHelper.SetTheme(theme);
        }

        public DelegateCommand ChangeHueCommand { get; private set; }

        public SkinViewModel()
        {
            ChangeHueCommand = new DelegateCommand(ChangeHue);
        }

        private void ChangeHue(object obj)
        {
            var hue = (Color)obj;
            ITheme theme = paletteHelper.GetTheme();
            theme.PrimaryLight = new ColorPair(hue.Lighten());
            theme.PrimaryMid = new ColorPair(hue);
            theme.PrimaryDark = new ColorPair(hue.Darken());
            paletteHelper.SetTheme(theme);
        }
    }
}
 
  

ColorToBrushConverter.cs

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Media;

namespace Mytodo.Common.Converters
{
    [ValueConversion(typeof(Color), typeof(Brush))]
    public class ColorToBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is Color color)
            {
                return new SolidColorBrush(color);
            }
            return Binding.DoNothing;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is SolidColorBrush brush)
            {
                return brush.Color;
            }
            return default(Color);
        }
    }
}

MultiValueEqualityConverter.CS

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;

namespace Mytodo.Common.Converters
{
    public class MultiValueEqualityConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            return values?.All(o => o?.Equals(values[0]) == true) == true || values?.All(o => o == null) == true;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

其他

其他的view自己定义就可以,这里不再展开

你可能感兴趣的:(WPF实战学习笔记,wpf,学习,笔记)