/// <summary> /// 使用此方法以切换背景类型 /// </summary> /// <param name="Type">背景类型</param> /// <param name="uri">图片背景的Uri</param> public void SwitchTo(BackgroundBrushType Type, BitmapImage Background = null, Uri ImageUri = null, Color?Color = null) { CurrentType = Type; switch (Type) { case BackgroundBrushType.Picture: { PictureBackgroundBrush.ImageSource = Background ?? throw new ArgumentNullException(nameof(Background), "if parameter: 'Type' is BackgroundBrushType.Picture, parameter: 'Background' could not be null"); ApplicationData.Current.LocalSettings.Values["PictureBackgroundUri"] = ImageUri?.ToString(); break; } case BackgroundBrushType.BingPicture: { BingPictureBursh.ImageSource = Background ?? throw new ArgumentNullException(nameof(Background), "if parameter: 'Type' is BackgroundBrushType.BingPicture, parameter: 'Background' could not be null"); break; } case BackgroundBrushType.SolidColor: { if (Color == null) { ApplicationData.Current.LocalSettings.Values.Remove("SolidColorType"); if (UIS.GetColorValue(UIColorType.Background) == Colors.White) { SolidColorBackgroundBrush.Color = Colors.White; AppThemeController.Current.Theme = ElementTheme.Light; } else { SolidColorBackgroundBrush.Color = "#1E1E1E".ToColor(); AppThemeController.Current.Theme = ElementTheme.Dark; } } else { AppThemeController.Current.Theme = Color == Colors.White ? ElementTheme.Light : ElementTheme.Dark; SolidColorBackgroundBrush.Color = Color.GetValueOrDefault(); ApplicationData.Current.LocalSettings.Values["SolidColorType"] = Color.GetValueOrDefault().ToString(); } break; } case BackgroundBrushType.Acrylic: { break; } } PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BackgroundBrush))); }
/// <summary> /// 使用此方法以切换背景类型 /// </summary> /// <param name="Type">背景类型</param> /// <param name="uri">图片背景的Uri</param> public void SwitchTo(BackgroundBrushType Type, Uri uri = null, Color?Color = null) { CurrentType = Type; switch (Type) { case BackgroundBrushType.Picture: { if (uri == null) { throw new ArgumentNullException(nameof(uri), "if parameter: 'Type' is BackgroundBrushType.Picture, parameter: 'uri' could not be null or empty"); } if (ApplicationData.Current.LocalSettings.Values["PictureBackgroundUri"] is string Ur) { if (Ur != uri.ToString()) { BitmapImage Bitmap = new BitmapImage(); PictureBackgroundBrush.ImageSource = Bitmap; Bitmap.UriSource = uri; ApplicationData.Current.LocalSettings.Values["PictureBackgroundUri"] = uri.ToString(); } } else { BitmapImage Bitmap = new BitmapImage(); PictureBackgroundBrush.ImageSource = Bitmap; Bitmap.UriSource = uri; ApplicationData.Current.LocalSettings.Values["PictureBackgroundUri"] = uri.ToString(); } break; } case BackgroundBrushType.SolidColor: { if (Color == null) { throw new ArgumentNullException(nameof(Color), "if parameter: 'Type' is BackgroundBrushType.SolidColor, parameter: 'Color' could not be null"); } SolidColorBackgroundBrush.Color = Color.GetValueOrDefault(); ApplicationData.Current.LocalSettings.Values["SolidColorType"] = Color.GetValueOrDefault().ToString(); break; } default: { AppThemeController.Current.ChangeThemeTo(ElementTheme.Dark); break; } } PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(BackgroundBrush))); }
/// <summary> /// 初始化BackgroundController对象 /// </summary> private BackgroundController() { if (ApplicationData.Current.LocalSettings.Values["SolidColorType"] is string ColorType) { SolidColorBackgroundBrush = new SolidColorBrush(GetColorFromHexString(ColorType)); } else { SolidColorBackgroundBrush = new SolidColorBrush(Colors.White); } if (ApplicationData.Current.LocalSettings.Values["UIDisplayMode"] is int ModeIndex) { switch (ModeIndex) { case 0: { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; CurrentType = BackgroundBrushType.Acrylic; break; } case 1: { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; CurrentType = BackgroundBrushType.SolidColor; if (SolidColorBackgroundBrush.Color == Colors.White && AppThemeController.Current.Theme == ElementTheme.Dark) { AppThemeController.Current.ChangeThemeTo(ElementTheme.Light); } else if (SolidColorBackgroundBrush.Color == Colors.Black && AppThemeController.Current.Theme == ElementTheme.Light) { AppThemeController.Current.ChangeThemeTo(ElementTheme.Dark); } break; } default: { if (Win10VersionChecker.Windows10_1903) { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = ApplicationData.Current.LocalSettings.Values["AcrylicThemeColor"] is string Color?GetColorFromHexString(Color) : Colors.LightSlateGray, TintOpacity = 1 - Convert.ToSingle(ApplicationData.Current.LocalSettings.Values["BackgroundTintOpacity"]), TintLuminosityOpacity = 1 - Convert.ToSingle(ApplicationData.Current.LocalSettings.Values["BackgroundTintLuminosity"]), FallbackColor = Colors.DimGray }; } else { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = ApplicationData.Current.LocalSettings.Values["AcrylicThemeColor"] is string Color?GetColorFromHexString(Color) : Colors.LightSlateGray, TintOpacity = 1 - Convert.ToSingle(ApplicationData.Current.LocalSettings.Values["BackgroundTintOpacity"]), FallbackColor = Colors.DimGray }; } if (ApplicationData.Current.LocalSettings.Values["CustomUISubMode"] is string SubMode) { CurrentType = (BackgroundBrushType)Enum.Parse(typeof(BackgroundBrushType), SubMode); } break; } } } else { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; CurrentType = BackgroundBrushType.Acrylic; } if (ApplicationData.Current.LocalSettings.Values["PictureBackgroundUri"] is string uri) { PictureBackgroundBrush = new ImageBrush { ImageSource = new BitmapImage(new Uri(uri)), Stretch = Stretch.UniformToFill }; } else { PictureBackgroundBrush = new ImageBrush { Stretch = Stretch.UniformToFill }; } }
/// <summary> /// 初始化BackgroundController对象 /// </summary> private BackgroundController() { UIS = new UISettings(); UIS.ColorValuesChanged += UIS_ColorValuesChanged; if (ApplicationData.Current.LocalSettings.Values["SolidColorType"] is string ColorType) { SolidColorBackgroundBrush = new SolidColorBrush(ColorType.GetColorFromHexString()); } else { if (UIS.GetColorValue(UIColorType.Background) == Colors.White) { SolidColorBackgroundBrush = new SolidColorBrush(Colors.White); } else { SolidColorBackgroundBrush = new SolidColorBrush("#1E1E1E".GetColorFromHexString()); } } if (ApplicationData.Current.LocalSettings.Values["UIDisplayMode"] is int ModeIndex) { switch (ModeIndex) { case 0: { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; CurrentType = BackgroundBrushType.Acrylic; break; } case 1: { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; CurrentType = BackgroundBrushType.SolidColor; if (SolidColorBackgroundBrush.Color == Colors.White && AppThemeController.Current.Theme == ElementTheme.Dark) { AppThemeController.Current.Theme = ElementTheme.Light; } else if (SolidColorBackgroundBrush.Color == Colors.Black && AppThemeController.Current.Theme == ElementTheme.Light) { AppThemeController.Current.Theme = ElementTheme.Dark; } break; } default: { if (WindowsVersionChecker.IsNewerOrEqual(WindowsVersionChecker.Version.Windows10_1903)) { if (double.TryParse(Convert.ToString(ApplicationData.Current.LocalSettings.Values["BackgroundTintOpacity"]), out double TintOpacity)) { if (double.TryParse(Convert.ToString(ApplicationData.Current.LocalSettings.Values["BackgroundTintLuminosity"]), out double TintLuminosity)) { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = ApplicationData.Current.LocalSettings.Values["AcrylicThemeColor"] is string Color?Color.GetColorFromHexString() : Colors.LightSlateGray, TintOpacity = 1 - TintOpacity, TintLuminosityOpacity = 1 - TintLuminosity, FallbackColor = Colors.DimGray }; } else { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = ApplicationData.Current.LocalSettings.Values["AcrylicThemeColor"] is string Color?Color.GetColorFromHexString() : Colors.LightSlateGray, TintOpacity = 1 - TintOpacity, FallbackColor = Colors.DimGray }; } } else { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; } } else { if (double.TryParse(Convert.ToString(ApplicationData.Current.LocalSettings.Values["BackgroundTintOpacity"]), out double TintOpacity)) { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = ApplicationData.Current.LocalSettings.Values["AcrylicThemeColor"] is string Color?Color.GetColorFromHexString() : Colors.LightSlateGray, TintOpacity = 1 - TintOpacity, FallbackColor = Colors.DimGray }; } else { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; } } if (ApplicationData.Current.LocalSettings.Values["CustomUISubMode"] is string SubMode) { CurrentType = (BackgroundBrushType)Enum.Parse(typeof(BackgroundBrushType), SubMode); } break; } } } else { AcrylicBackgroundBrush = new AcrylicBrush { BackgroundSource = AcrylicBackgroundSource.HostBackdrop, TintColor = Colors.LightSlateGray, TintOpacity = 0.4, FallbackColor = Colors.DimGray }; CurrentType = BackgroundBrushType.Acrylic; } }