简体   繁体   中英

How to save Application theme name in WPF

I am using Devexpress and WPF. There are different themes user can apply provided by devexpress.

 ThemeManager.ApplicationThemeName = Theme.MetropolisDarkName; //MetropolisDarkName is name of a Theme.

In my application user can select any theme to apply. But if he closes application and opens it again, Themes changes doesn't same. I want that these changes should be saved so if user after applying theme will open it again, changes should be saved and apply.

Should i have to save name of Theme in database or is there any other way to solve this. I need your suggestions. Thank you.

The super easy way to do this in Visual Studio is to add a new .settings file to your project, and define a setting of ThemeName . You can find the settings template under General in the C# project Templates. The settings file itself is just a designer with an underlying class of type System.Configuration.ApplicationSettingsBase .

The class created saves setting values to the app.config . The neat thing is you can define settings as application or user, so different users using the application on the same machine can have their own custom settings.

The following assumes that you created the file Settings.settings with an entry called ThemeName of type string .

Get the theme from Settings

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ThemeManager.ApplicationThemeName = Properties.Settings.Default.ThemeName;
}

Save the Theme

public void SetTheme(string themeName) {
    ThemeManager.ApplicationThemeName = themeName;
    Properties.Settings.Default.ThemeName = themeName;
    Properties.Settings.Default.Save();
}

Settings on MSDN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM