Reputation: 77
I have an UWP App in which I want to allow the user to change the AccentColors while using the App. For this, I would like to override the SystemAccentColor. I noticed that I can override it in the App.xaml, but I don't know how I would do this while the programm is running.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<Color x:Key="SystemAccentColor">#107C10</Color>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Most answers to this question I've found used the Theme (Default, Dark, Light) to do this. I would like if the user could change the Theme from Light to Dark Mode AND have a own AccentColor. The Theme change works. Changing the Accent Color as above also works.
Upvotes: 0
Views: 996
Reputation: 32785
Please check ResourceDictionary and XAML resource references official document, you could edit SystemAccentColor
ResourceDictionary content in the code behind. But we need place the code in the Application.OnLaunched
method
protected override void OnLaunched(LaunchActivatedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
SolidColorBrush brush = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 0, 255, 0)); // green
this.Resources["brush"] = brush;
// … Other code that VS generates for you …
}
}
If you want to make it change at real time, we suggest use setting class to achieve
for example
public class Setting : INotifyPropertyChanged
{
private SolidColorBrush accentColor = new SolidColorBrush( Colors.Red);
public SolidColorBrush AccentColor
{
get { return accentColor; }
set { accentColor = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary>
<local:Setting x:Key="Setting"/>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
<Button
VerticalAlignment="Bottom"
Background="{Binding AccentColor, Mode=TwoWay, Source={StaticResource Setting}}"
Click="Button_Click"
Content="Click" />
Update the value at runtime
((Setting)Application.Current.Resources["Setting"]).AccentColor = new SolidColorBrush(Colors.Beige) ;
Upvotes: 1