private static ResourceDictionary GetAccentResource(Accent accent) { return(accent.Specified != null ? new ResourceDictionary { Source = CreateAccentResourceUri(accent.Specified.Value), } : GetAccentResource(accent.Color ?? WindowsTheme.GetAccentColor())); }
static ResourceDictionary GetUwpAccentResource(Accent accent) { return(accent.Specified != null ? new ResourceDictionary { Source = CreateUwpAccentResourceUri(accent.Specified.Value), } : GetUwpAccentResource(accent.Color ?? WindowsTheme.Accent.Current)); }
public void ChangeAccent(Accent accent) { if (this.Accent == accent) { return; } this.InvokeOnUIDispatcher(() => this.ChangeAccentCore(accent)); this.Accent = accent; }
/// <summary> /// テーマ機能を有効化します。テーマまたはアクセントが変更されたとき、<paramref name="app"/> /// で指定した WPF アプリケーション内のテーマ関連リソースは自動的に書き換えられます。 /// </summary> /// <param name="app">テーマ関連リソースを含む WPF アプリケーション。</param> /// <param name="theme">初期値として使用するテーマ。</param> /// <param name="accent">初期値として使用するアクセント。</param> /// <returns><paramref name="app"/> をリソースの書き換え対象から外すときに使用する <see cref="IDisposable"/> オブジェクト。</returns> public IDisposable Register(Application app, Theme theme, Accent accent) { this.dispatcher = app.Dispatcher; var disposable = this.Register(app.Resources, theme, accent); this.Theme = theme; this.Accent = accent; return(disposable); }
private void UpdateListener(Accent accent) { if (accent == Accent.Windows) { if (this.windowsColorListener == null) { // アクセントが Windows 依存で、リスナーが未登録だったら購読する this.windowsColorListener = WindowsTheme.RegisterAccentColorListener(x => this.ChangeAccentCore(x)); } } else if (this.windowsColorListener != null) { // アクセントが Windows 依存でないのにリスナーが登録されてたら解除する this.windowsColorListener.Dispose(); this.windowsColorListener = null; } }
internal IDisposable Register(ResourceDictionary rd, Theme theme, Accent accent) { var allDictionaries = EnumerateDictionaries(rd).ToArray(); var themeDic = GetThemeResource(theme); var targetThemeDic = allDictionaries.FirstOrDefault(x => CheckThemeResourceUri(x.Source)); if (targetThemeDic == null) { targetThemeDic = themeDic; rd.MergedDictionaries.Add(targetThemeDic); } else { foreach (var key in themeDic.Keys.OfType <string>().Where(x => targetThemeDic.Contains(x))) { targetThemeDic[key] = themeDic[key]; } } this.themeResources.Add(targetThemeDic); var accentDic = GetAccentResource(accent); var targetAccentDic = allDictionaries.FirstOrDefault(x => CheckAccentResourceUri(x.Source)); if (targetAccentDic == null) { targetAccentDic = accentDic; rd.MergedDictionaries.Add(targetAccentDic); } else { foreach (var key in accentDic.Keys.OfType <string>().Where(x => targetAccentDic.Contains(x))) { targetAccentDic[key] = accentDic[key]; } } this.accentResources.Add(targetAccentDic); // Unregister したいときは戻り値の IDisposable を Dispose() してほしい return(Disposable.Create(() => { this.themeResources.Remove(targetThemeDic); this.accentResources.Remove(targetAccentDic); })); }
public void Initialize(Application app, Theme theme, Accent accent) { this.Register(app, theme, accent); }
private void ChangeAccentCore(Accent accent) { this.ChangeAccentCore(GetAccentResource(accent)); }
void ChangeAccentCore(Accent accent) { this.ChangeAccentCore(GetAccentResource(accent), GetUwpAccentResource(accent)); }
internal IDisposable Register(ResourceDictionary rd, Theme theme, Accent accent) { this.SetAppMode(theme); var allDictionaries = EnumerateDictionaries(rd).ToArray(); // MetroRadiance Theme var themeDic = GetThemeResource(theme); var targetThemeDic = allDictionaries.LastOrDefault(x => CheckThemeResourceUri(x.Source)); targetThemeDic = UpdateDic(rd, targetThemeDic, themeDic); this._themeResources.Add(targetThemeDic); // UWP Theme ResourceDictionary targetUwpThemeDic = null; var upwThemeDic = GetUwpThemeResource(theme); targetUwpThemeDic = allDictionaries.LastOrDefault(x => CheckUwpThemeResourceUri(x.Source)); if (targetUwpThemeDic != null || this._enableUWPCompatibleResources) { targetUwpThemeDic = UpdateDic(rd, targetUwpThemeDic, upwThemeDic); this._uwpThemeResources.Add(targetUwpThemeDic); } // MetroRadiance Accent var accentDic = GetAccentResource(accent); var targetAccentDic = allDictionaries.LastOrDefault(x => CheckAccentResourceUri(x.Source)); targetAccentDic = UpdateDic(rd, targetAccentDic, accentDic); this._accentResources.Add(targetAccentDic); // UWP Accent var uwpAccentDic = GetUwpAccentResource(accent); if (targetUwpThemeDic != null) { // UWP Themeにより、追加されている可能性があるので、再取得する allDictionaries = EnumerateDictionaries(rd).ToArray(); } var targetUwpAccentDic = allDictionaries.LastOrDefault(x => CheckUwpAccentResourceUri(x.Source)); if (targetUwpAccentDic != null || this._enableUWPCompatibleResources) { targetUwpAccentDic = UpdateDic(rd, targetUwpAccentDic, uwpAccentDic); this._uwpAccentResources.Add(targetUwpAccentDic); } // Find resources that include SolidColorBrushes in style and UWP theme var targetStyleDics = allDictionaries.Where(x => CheckStyleResourceUri(x.Source)); if (targetUwpThemeDic != null) { targetStyleDics = targetStyleDics.Concat(Enumerable.Repeat(targetUwpThemeDic, 1)); } targetStyleDics = targetStyleDics.Where(y => { foreach (var item in y.OfType <System.Collections.DictionaryEntry>()) { if (item.Value is SolidColorBrush) { return(true); } } return(false); }); this._styleResources.AddRange(targetStyleDics); // Unregister したいときは戻り値の IDisposable を Dispose() してほしい return(Disposable.Create(() => { this._themeResources.Remove(targetThemeDic); if (targetUwpThemeDic != null) { this._uwpThemeResources.Remove(targetUwpThemeDic); } this._accentResources.Remove(targetAccentDic); if (targetUwpAccentDic != null) { this._uwpAccentResources.Remove(targetUwpAccentDic); } targetStyleDics.ToList().ForEach(i => this._styleResources.Remove(i)); })); }
/// <summary> /// 現在の <see cref="Color"/> 構造体から、MetroRadiance の <see cref="ThemeService"/> で使用する <see cref="Accent"/> を作成します。 /// </summary> public static Accent ToAccent(this Color color) { return(Accent.FromColor(color)); }