I'm using Microsoft's AdControl on several pages... but in order to simplify it i would like to store ApplicationId once and read it as a resource.
Could it be possible to use in the App.xaml and then in the control set the binding to it? But how?
Use "{StaticResource name}" just like you would with <Page.Resources>
To store the resource, create a new <ResourceDictionary> in <ResourceDictionary.MergedDictionaries> and place your resource in that.
For example:
<Application.Resources>
<ResourceDictionary>
<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>
<x:String x:Key="Foo">Bar</x:String>
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Related
I have a style file "dark.xaml" that I will use to style all xaml pages, so I place it in "application.xaml". But why my xaml page can't to access the style of "dark.xml"?
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/rtwin;component/style/Dark.xaml">
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
From your ResourceDictionary source it seems that you are adding style from an assembly (correct if I'm wrong).
Well, in that case, you need to add pack://application:,,, so that your final App.xaml should look like this,
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/rtwin;component/style/Dark.xaml">
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have defined in my WPF application all the styles that I want to apply. The problem is that if I open an other window it doesn't have the same style of the main window because it is defined in an other project.
How can I apply the styles of the main application?
Thank you!
EDIT
This is a part of my App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Assets/Styles/Themes/Dark.xaml" />
<ResourceDictionary Source="Assets/Styles/Border.xaml" />
<ResourceDictionary Source="Assets/Styles/BaseBlock.xaml" />
<ResourceDictionary Source="Assets/Styles/Buttons.xaml" />
.....
What I need is to apply the styles defined in the Main Project to the ones defined in Project1 in order to have uniformity.
Put your styles to the ResourceDictionary of your application resources in App.xaml. So you can use your resources everywhere via {StaticResource keyOfYourStyle}. If you want, that your styles be applied to all controls of the type, then use default styles:
<Style TargetType="Border" BasedOn="{StaticResource keyOfYourStyle}>
<Application.Resources>
<ResourceDictionary>
<ThisResourceWillBeAccessedEveryWhere x:Key="ResName"/>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/YourAssembley;component/Resources/ButtonStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
I have the following project structure:
When I tried to add Resoucse dictionary inside my user control FloorplanHierarchy I am getting file not found error:
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source=".../UserControls/UserControlResources/LookUpEditTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
LookUpEditTemplate.xaml is inside the folder UserControlResources. Please help.
Remove UserControls in the source because UserControlResources and FloorplanHierarchy both are in the same folder.
So changing this:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source=".../UserControls/UserControlResources/LookUpEditTemplate.xaml" />
</ResourceDictionary.MergedDictionaries>
To this:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="../UserControlResources/LookUpEditTemplate.xaml"/>
</ResourceDictionary.MergedDictionaries>
Should works.
I have the following App.xaml:
<Application
x:Class="Genisyss.V2.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Genisyss.V2.Client"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="Resources/ShellResources.xaml" />
<ResourceDictionary>
<local:AppBootstrapper
x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
ShellResources.xaml looks like this:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="Genisyss.V2.Client.Resources.ShellResources"
x:ClassModifier="public">
<ResourceDictionary.MergedDictionaries>
<!-- EXTERNAL RESOURCES -->
<ResourceDictionary
Source="/Teton.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary
Source="Images.xaml" />
</ResourceDictionary.MergedDictionaries>
<!-- controls and templates defined, etc -->
</ResourceDictionary>
Configured this way, the program fails at runtime with "resource not found" or "xaml parse exception".
If I change App.xaml to ALSO include external resources, like this:
<Application
x:Class="Genisyss.V2.Client.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Genisyss.V2.Client"
ShutdownMode="OnExplicitShutdown">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- NOTE ADDITION OF EXTERNAL RESOURCES -->
<ResourceDictionary
Source="/Teton.Wpf;component/Themes/Generic.xaml" />
<ResourceDictionary
Source="Resources/ShellResources.xaml" />
<ResourceDictionary>
<local:AppBootstrapper
x:Key="bootstrapper" />
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Now my program finds the resources at runtime and runs without error. What gives with this? I thought the point of merged resources was ACTUALLY TO MERGE THE RESOURCES, so that you didn't need to declare them in two places.
EDIT
Changed the Source property in ShellResources.xaml to be an absolute pack uri:
pack://application:,,,/Teton.Wpf;component/Themes/Generic.xaml
But this made no difference.
This bug showed up because of the order that things need to be defined in a resource file. I have a WPF controls project that defines some custom controls AND overrides the default styles of several built-in controls.
To make a long story short, I had resources that were defined further down in the Generic.xaml file, and they were being accessed further up - that's a no-no. Resources are apparently parsed only once, in a strict top-down fashion. I was under the impression that they were parsed in a two-or-more pass manner just like normal c# source code.
To avoid this happening, here is a suggested way to lay out your Generic.xaml in a WPF Controls project:
<!-- Shared resources like brushes and colors -->
<!-- DEFAULT control styles, e.g. <Style TargetType="{x:Type TextBox}"... -->
<!-- Control styles for custom controls that are defined in your project -->
Thanks to #Aybe for pushing me to dig deeper on this.
I am programming w Windows 8.1 App using C# and the MVVM-Light Toolkit from GalaSoft.
All I have is the code below:
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator" xmlns:vm="using:Scedule.ViewModel" />
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
The error "Each Dictionary entry must have an associated key attribute" occurs and only disappears when I either remove
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
or
<vm:ViewModelLocator x:Key="Locator" xmlns:vm="using:Scedule.ViewModel" />
Can anyone tell me what the problem here is?
Note that Application.Resources requires an instance of ResourceDictionary, so you have to do something like this:
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" xmlns:vm="using:Scedule.ViewModel" />
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resource Dictionaries/StandardStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
So it's not strange at all, it's also not a bug. If you want your ResourceDictionary to be treated as a resource, you of course have to provide some Key for it, however in this case you really want to assign an instance of ResourceDictionary to the Application.Resources