How to apply different ResourceDictionary Styles asynchronously every 5 seconds? - c#

Below is the Code , in which i want to change the theme color of the MahApps.Metro pakage.
it can be changed by changing the ResourceDictionary Source pack of MahApps.
[/MahApps.Metro;component/Styles/Accents/Blue.xaml ]
say example its /Blue.xaml now ... we can change colors of the window. to /Red.xaml , /Yellow.xaml etc
So how can i change the color of the window asynchronously in every 5 seconds? is this possible in wpf ?
i am new to wpf and clueless.
<controls:MetroWindow x:Class="NginX.Choose"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="NginX" Height="350" Width="350" ShowMaxRestoreButton="False">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
</Grid>
</controls:MetroWindow>

You can replace the Application's resource dictionary by doing:
Application.Current.Resources.Clear()
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml")
});
Put this in a timer and cycle through Red.xaml, Blue.xaml etc

Related

App.OnExit no suitable method found to override when adding ResourceDirectory

I've came across this error, which I couldn't find the solution for. I'm implementing an mvvm application and in main window I'm setting the main DataContext in xaml using a loader:
in App.xaml:
<Application.Resources>
<viewModel:KinectViewModelLoader x:Key="KinectViewModelLoader"/>
</Application.Resources>
In MainWindow.xaml:
<Window x:Class="KinectFittingRoom.MainWindow"
...
DataContext="{Binding KinectViewModel, Source={StaticResource KinectViewModelLoader}}">
...
</Window>
Everything was running well, but now I wanted to add some dynamic resources, so I've created some xaml files containing styles and other elements.
In example:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ModelUIElement3D x:Key="BirthdayHatModel">
...
</ModelUIElement3D>
</ResourceDictionary >
To use them in MainWindow.xaml I've added them to App.xaml file as ResourceDirectory and then my application began to crash.
App.xaml:
<Application x:Class="KinectFittingRoom.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:viewModel="clr-namespace:KinectFittingRoom.ViewModel"
StartupUri="MainWindow.xaml">
<Application.Resources>
<viewModel:KinectViewModelLoader x:Key="KinectViewModelLoader"/>
<ResourceDictionary x:Key="ResourceDictionary">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/GlassButton.xaml"/>
<ResourceDictionary Source="Resources/Models/BirthdayHat.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
And now I'm getting an error on App.xaml.cs file saying that KinectFittingRoom.App.OnExit(System.Windows.ExitEventArgs) has no suitable method to override.
My overriden method:
protected override void OnExit(ExitEventArgs e)
{
KinectViewModelLoader.Cleanup();
base.OnExit(e);
}
Mabe someone could explain to me why adding ResourceDirectory node causes my application to throw an error? What can I do to include those resources and avoid such problem? I would appreciate any advice.
I think you need to change two things in your XAML:
In your App.xaml file Move the declaration of your ViewModel inside ResourceDictionary section. Also if possible you should delete x:Key="ResourceDictionary". So now your App.xaml should look like:
<Application x:Class..................>
<Application.Resources>
<ResourceDictionary>
<viewModel:KinectViewModelLoader x:Key="KinectViewModelLoader"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Styles/GlassButton.xaml"/>
<ResourceDictionary Source="Resources/Models/BirthdayHat.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
If after following the above step your program does not work then please follow this step along. Change the DataContext Property of your window like the code shown below:
<Window x:Class="KinectFittingRoom.MainWindow"
DataContext="{StaticResource KinectViewModelLoader}">
...
</Window>
If still your application does not work then change the order of the lines in App.OnExit() as follows:
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
KinectViewModelLoader.Cleanup();
}

Apply ResourceDictionary to Pages in Frame in WPF

I have a Window in WPF which simply contains a Frame element. The Frame displays a Page; which Page is displayed changes based on user interaction.
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="720" Width="1280">
<Grid>
<Frame Source="{Binding Source={StaticResource MainPageIntent}, Path=Path}"/>
</Grid>
</Window>
I would like all Pages that appear in that Frame to share a common Resource Dictionary so that they may all be styled in a common way.
Right now I have something like this in every page that this Window loads:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceDictionaries/BaseControlStyles/MenuStyle.xaml"/>
I was hoping that I might just be able to set the resource dictionary on the Window, and they would "inherit" those resources, but that does not appear to be the case. I tried something like this, but the styles found in MenuStyle.xaml are not applied the the controls inside the Page loaded by the Frame:
<Window x:Class="MyWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="720" Width="1280">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/ResourceDictionaries/BaseControlStyles/MenuStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Frame Source="{Binding Source={StaticResource MainPageIntent}, Path=Path}"/>
</Grid>
</Window>
Is there are way to define styles at the Window level such that all pages loaded in child Frames will use those styles?
Note: I do not want to apply these styles to ALL windows in my application, so putting this ResourceDictionary in my App.xaml does not appear to be a valid solution.
If you want to write it once to avoid code duplicates, you can write it in code behind. On frame ContentRendered you can write a code to add resource to the page which is being loaded.
<Frame Name="fr_View" ContentRendered="fr_View_ContentRendered"/>
private void fr_View_ContentRendered(object sender, System.EventArgs e)
{
ResourceDictionary myResourceDictionary = new ResourceDictionary();
myResourceDictionary.Source = new Uri("Dictionary1.xaml", UriKind.Relative);
(fr_View.Content as System.Windows.Controls.Page).Resources.MergedDictionaries.Add(myResourceDictionary);
}
Take a look at this link:
Set up application resources from code

Create new WPF page which is subset of current page

I am creating a WPF application which behaves like a 'Windows Wizard', when I press a button in MainWindow it should navigate to say Page2.
For Page2 I added a new WPF page from VisualStudio. Is there any way for this new page to inherit some attributes from main window, like background,dimension,title,etc since most of the attributes are same.
you can use ResourceDictionaries for stuff like Color Attributes etc.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color x:Key="LimeColor">#FFA4C400</Color>
<SolidColorBrush Color="{StaticResource LimeColor}" x:Key="Lime" />
</ResourceDictionary>
App.xaml
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/YourDictionary.xaml" />
</Application.Resources>
For the correct Source see: Pack URIs in WPF
Page and Window
for example:
<Grid Background="{StaticResource Lime}">
Or you can define Styles and Templates in WPF for any ControlTypes.
For further information : Control Customization on MSDN

Why won't my custom Modern UI style apply?

Ok, so I know a little bit about GUI development, and am new to Windows 8 (Modern UI)
I'd also like to do this with C# (no XAML) if possible.
Style
What I'm trying to do here is create a style I can apply to a button created somewhere else.
public static Style firstButtonStyle()
{
firstButton = new Style(typeof(Button));
ControlTemplate btnControl = new ControlTemplate();
//firstButton.Setters.Add(new Setter(Button.TemplateProperty, btnControl));
firstButton.Setters.Add(new Setter(Button.BackgroundProperty, new SolidColorBrush(Windows.UI.Colors.Blue)));
firstButton.Setters.Add(new Setter(Button.IsPointerOverProperty, new SolidColorBrush(Windows.UI.Colors.PaleGreen)));
firstButton.Setters.Add(new Setter(Button.IsPressedProperty, Windows.UI.Colors.Beige));
firstButton.Setters.Add(new Setter(Button.ForegroundProperty, Windows.UI.Colors.Red));
return firstButton;
}
Application
This is where the button is created and the style applied.
private Button enterButtonCreation(string text)
{
Button enterButton = new Button();
enterButton.Content = text;
enterButton.Margin = new Thickness(200, 80, 20, 0);
Style firstButtonStyl = WikierStyle.firstButtonStyle();
enterButton.Style = firstButtonStyl;
enterButton.Background = new SolidColorBrush(Windows.UI.Colors.Silver);
return enterButton;
}
I can change the background silver using .Background, but when using .BackgroundProperty nothing seems to happen.
first of i suggest you that you should try to do all of your style work in your xaml file like try to declare custum styles in your xaml page. if you want to use the custumstyle in particularpage only then define it your page only like this. and you can apply this style on any button.
<Page
x:Class="Appgridcheck.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Appgridcheck"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.Resources>
<Style TargetType="Button" x:Name="CustomStyle" >
<Setter Property="Background" Value="White" />
</Style>
</Page.Resources>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Style="{StaticResource CustomStyle}" />
</Grid>
secondly if you want to define global custumStyle that you can use throughout your app(mean on all pages ) then define this style in app.xaml like this.
<Application
x:Class="Appgridcheck.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Appgridcheck">
<Application.Resources>
<ResourceDictionary>
<Style TargetType="Button" x:Name="gllobalCustomstyle" >
<Setter Property="Background" Value="Black" />
</Style>
<ResourceDictionary.MergedDictionaries>
<!--
Styles that define common aspects of the platform look and feel
Required by Visual Studio project and item templates
-->
<ResourceDictionary Source="Common/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
now you use this tyle on any page..hope this will help..

How to apply styles to all windows in WPF app?

I have the following App.xaml file:
<Application x:Class="MiniDeviceConfig.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MiniDeviceConfig.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Button.xaml"/>
<ResourceDictionary Source="CheckBox.xaml"/>
<ResourceDictionary Source="ComboBox.xaml"/>
<ResourceDictionary Source="Common.xaml"/>
<ResourceDictionary Source="GroupBox.xaml"/>
<ResourceDictionary Source="Label.xaml"/>
<ResourceDictionary Source="LinkButton.xaml"/>
<ResourceDictionary Source="ListBox.xaml"/>
<ResourceDictionary Source="ListView.xaml"/>
<ResourceDictionary Source="RadioButton.xaml"/>
<ResourceDictionary Source="Tooltip.xaml"/>
<ResourceDictionary Source="Window.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
In my application, my main window is MiniDeviceConfig.xaml (as seen above). In my Button.xaml file, I clearly set the button height to some obscene number. And, this size is reflected in my main window's buttons. However, some action on the main window triggers a modal window that has more buttons on it. I was expecting the same tall buttons but no such luck. How do I get the style to propagate into all windows in the application?
You can try very helpful class ThemeManager from http://wpfthemes.codeplex.com. In our project we had similiar problems and solve it by using it.
Figured it out. There were other resources included that were conflicting.

Categories