示例#1
0
        public Boolean SetCurrentTheme(String name, Boolean applySetting)
        {
            try
            {
                // Find theme for name
                IMesTheme theme = Themes.FirstOrDefault(x => x.Name == name);
                if (theme == null)
                {
                    return(false);
                }
                CurrentTheme = theme;

                // Setup asnc info
                Mvx.IoCProvider.Resolve <IMvxMainThreadAsyncDispatcher>()
                .ExecuteOnMainThreadAsync(() =>
                {
                    // Setup app style
                    ResourceDictionary appTheme =
                        Application.Current.Resources.MergedDictionaries.Count > 0
                        ? Application.Current.Resources.MergedDictionaries[0] : null;

                    if (appTheme == null)
                    {
                        appTheme = new ResourceDictionary();
                        Application.Current.Resources.MergedDictionaries.Add(appTheme);
                    }

                    appTheme.BeginInit();

                    appTheme.MergedDictionaries.Clear();
                    foreach (Uri uri in theme.ApplicationResources)
                    {
                        ResourceDictionary newDict = new ResourceDictionary {
                            Source = uri
                        };
                        appTheme.MergedDictionaries.Add(newDict);
                    }
                    appTheme.EndInit();
                });

                _log?.LogInformation($"Theme set to {name}");

                // publish event
                _messenger.Publish(new MesThemeChangeMessage(this, CurrentTheme.Name));

                if (applySetting)
                {
                    Properties.Settings.Default.ThemeName = CurrentTheme.GetType().Name;
                    Properties.Settings.Default.Save();
                }

                return(true);
            }
            catch (Exception e)
            {
                _log?.LogInformation(e, "Log Theme Setting");
                return(false);
            }
        }