C# UWP Change global app background - c#

I need to change default white background that is showing on the right side when I am resizing the window of UWP application, and I need to do it dynamically.
I have tried:
var newBackground = Application.Current.Resources["ApplicationPageBackgroundThemeBrush"] as SolidColorBrush;
if (newBackground != null)
{
newBackground.Color = newColor;
}
The newBackground is changed, but not affecting the application.
Any help?

If you want to override ApplicationPageBackgroundThemeBrush
Application.Current.Resources["ApplicationPageBackgroundThemeBrush"] = Colors.Red;

For your issue we need to check something in background:
When you check the ApplicationPageBackgroundThemeBrush in the generaic.xaml(To konw what is generaic.xaml you can see here), you will find that ApplicationPageBackgroundThemeBrush is defined three times in "Default","HighContrast" and also "Light". So that when you call request theme all colors will be changed in different themes.
this.RequestedTheme = ElementTheme.Dark
So go back to your question, if you change the request theme to "Dark" you will find that the change color code:
newBackground.Color = newColor;
will not change since there is a default setting for "Dark".(It works for default/Light theme)
And it seems we cannot modify this theme brush at runtime from code behind.
I think the only way for this is to create theme color yourself then change the color by set the element explictly.
To set the theme color. New acrylic document provide a good point for us. Here I write a simple sample for you to show how default theme works:
Create a dictionary and insert the following code:
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="Blue"></SolidColorBrush>
</ResourceDictionary>
<ResourceDictionary x:Key="Dark">
<SolidColorBrush x:Key="ApplicationPageBackgroundThemeBrush" Color="Yellow"></SolidColorBrush>
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
Add it to app.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Dictionary1.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
To explictly set the element, do something like mygrid.background=xxxx. If you want to trigger it when resize, change the propery in adaptivetrigger.

Related

Am I including font using PACK URI correctly?

I want to include my fonts into application through styles. (Resource Dicitionaries) Seems like every font looks same.
I tried include these fonts using pack://application:,,,/Fonts/#font-name
Unfortunately that did not resolved my problem at all.
Fonts.xaml
<FontFamily x:Key="LatoThin">pack://application:,,,/Fonts/#Lato Thin</FontFamily>
<FontFamily x:Key="LatoRegular">pack://application:,,,/Fonts/#Lato Regular</FontFamily>
App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Fonts.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I expect every font looks like it should instead I get default font.
Ok, so I found the answer.
I found out that it works with this code.
<FontFamily x:Key="LatoThin">Fonts/Lato-Thin.ttf #Lato Thin</FontFamily>

Change colour of overlay area when using Flyout in UWP app

I am using a FlyoutBase in my UWP app. I have set the LightDismissOverlayMode Property to "On". This makes the area outside of the light-dismiss UI to be darkened. Is there any way by which I can choose the colour of the area being darkened?
Is there any way by which I can choose the colour of the area being darkened?
There is FlyoutLightDismissOverlayBackground StaticResource in generic.xaml file, and you could modify it's ResourceKey for changing color in Application.Resources like the following.
<Application.Resources>
<ResourceDictionary>
<StaticResource x:Key="FlyoutLightDismissOverlayBackground" ResourceKey="SystemControlAcrylicElementMediumHighBrush" />
</ResourceDictionary>
</Application.Resources>

Default Windows theme for MessageBox with Extended WPF Toolkit

I'm using custom MessageBox in my application and I need to apply my app style to it. So I use WPF Toolkit to create my MessageBox and I'm applying a MessageBoxStyle to my MessageBox :
C#:
Result = Xceed.Wpf.Toolkit.MessageBox.Show(
message,
title,
messageBoxButtons,
messageBoxImage,
(Style)resourceDictionary["MessageBoxStyle"]
);
XAML:
<Style x:Key="MessageBoxStyle" TargetType="{x:Type xctk:MessageBox}">
<!-- My Setters -->
</Style>
The problem is that it displays the basic Windows Vista window theme. I'd like my MessageBox to get the actual Windows theme of the machine (mine is Windows Server 2008 R2 E, but it might change if the application is run on another computer).
Is there a way to set this Windows theme "by default"?
WPF comes with the standard Windows themes on all Windows versions. You can have Aero theme with following steps:
Source: http://mrpmorris.blogspot.com/2008/05/using-vista-aero-theme-in-xp-wpf-apps.html
Add PresentationFramework.Aero to your application's references list as a requires
Edit your App.xaml
<Application.Resources>
<ResourceDictionary>
<!-- Put your stuff here instead -->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Other options are :
<ResourceDictionary Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Classic;component/themes/Classic.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Royale;component/themes/Royale.NormalColor.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Luna.Homestead;component/themes/Luna.Homestead.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Luna.Metallic;component/themes/Luna.Metallic.xaml"/>
<ResourceDictionary Source="/PresentationFramework.Zune;component/themes/Zune.NormalColor.xaml"/>

WPF border background bount to in application resource defined brush won't update when resource is updated

Hi,
so this will be my first question as I did not find anything that could answer my problem.
Initial Situation
I defined a SolidColorBrush in my <Window.Resources>
<SolidColorBrush x:Key="BackgroundBrush" Color="{DynamicResource BackgroundColor}"/>
<Color x:Key="BackgroundColor">PeachPuff</Color>
Then I bound my Border to the SolidColorBrush
<Border Background="{DynamicResource ResourceKey=BackgroundBrush}" />
At application launch I read an xml file where the background color is saved.
// Some XML Loading stuff -> backgroundColor is a Color
this.Resources["BackgroundColor"] = backgroundColor;
This worked like a charme. I could change the Color in the xml file and the background of my border was whatever color I defined in the xml file.
Actual Problem
Now I moved the definition of the SolidColorBrush and the Color to my App.xaml file and changed the method to change the color to:
Application.Current.Resources["BackgroundColor"] = backgroundColor;
But now the background of the border does not change anymore. It's just the default color of the border. No matter what I write in my xml file.
When I debug what's in
Application.Current.Resources["BackgroundColor"]
the color that was assigned by
Application.Current.Resources["BackgroundColor"] = backgroundColor;
is actually in
Application.Current.Resources["BackgroundColor"]
but the background is not changed...
Background
I have two windows. An main window and a preference window. In the preference window I want to be able to change the FontFamily/Type/Weight/Color etc. of the main window.
My first approach was to define all styles in the main window resources and pass the values I want to change to the preference window and read out the changes and then update the resources in the main window resources.
As this worked very well I now wanted to move the styles to the app.xaml and read and update them there so I don't have to pass them to the preference window and read them from there again.
Can not reproduce. The following code works for me (border shows up as azure):
My one suggestion would be to make sure you removed the resource from the window. The XAML binding would bind to the closest resource which would be the window resource (not the app resource), so you would be binding to the resource you're not changing.
MainWindow.xaml:
<Window x:Class="WpfApplication3.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wpfApplication3="clr-namespace:WpfApplication3"
Title="MainWindow" Height="350" Width="525"
x:Name="Window">
<Window.Resources>
</Window.Resources>
<Grid>
<Border Background="{DynamicResource BackgroundBrush}">
<Button Margin="10">Test</Button>
</Border>
</Grid>
</Window>
App.xaml:
<Application x:Class="WpfApplication3.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<SolidColorBrush x:Key="BackgroundBrush" Color="{DynamicResource BackgroundColor}"/>
<Color x:Key="BackgroundColor">PeachPuff</Color>
</Application.Resources>
</Application>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Application.Current.Resources["BackgroundColor"] = Colors.Azure;
}
}
Okay I found the solution.
I put the styles into a ResourceDictionary which actually didn't help anything.
BUT:
When I reload the resource dictionary after modifying it the styles get applied.
Actually I thought it would load the dictionary without the changes applied before clearing the merged dictionaries?
Application.Current.Resources["BackgroundColor"] = this.SelectedBackgroundColor;
Application.Current.Resources.MergedDictionaries.Clear();
var dictionary = new ResourceDictionary();
dictionary.Source = new Uri("ControlStyles.xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Add(dictionary);

How to change XAML value using C#?

I have a style file for Styles in WPF XAML with name Brushes.xaml which stores all colors for the WPF.
Code Here:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="DefaultColor">SteelBlue</Color>
<Color x:Key="LightDefaultColor">LightSteelBlue</Color>
</ResourceDictionary>
I want to change the value of DefaultColor using C# code.
Use the DynamicResource extension instead of the StaticResource extension on all references to keys that can change at runtime.
Then you can use code like the following to change the value.
Application.Current.Resources["Default Color"] = System.Windows.Media.Colors.Red;
This can be done per object to...
public MyWindow()
{
InitializeComponent();
this.Resources["Default Color"] = System.Windows.Media.Colors.Red;
PART_DynamicButton.Resources["Default Color"] = System.Windows.Media.Colors.Red;
}
This is higher performance than clearing your entire merged resource dictionary and adding a new one if you only need to modify a few values.
Just remember that DynamicResource extension only works on DependencyProperties and Freezable objects instantiated in Xaml are usually frozen which prevents modifing their DependencyProperties. So don't try to change the color of a SolidColorBrush if the brush was instaniated in xaml.
Here is a workaround
<! -- Xaml -->
<SolidColorBrush x:Key="App_Page_Background" Color="White"/>
<Page Background="{DynamicResource App_Page_Background}"/>
// C# code
Application.Current.Resources["App_Page_Background"] = new SolidColorBrush(Colors.Red);
Rather than changing the XAML contents you should create one XAML file for each theme.
Then you can change the theme at runtime like this:
ResourceDictionary skin = new ResourceDictionary();
skin.Source = new Uri(#"" + themeName + ".xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(skin);

Categories

Resources