Where to add ObjectDataProvider in Application.Resources with ResourceDictionary - c#

I followed this Tutorial and got stuck on the Create Instance of Config Settings Class-part. Here they just add the ObjectDataProvider in the Application.Resources. In my case there is already a ResourceDictionary in this context and I think this causes the problem.
My App.xaml:
<Application x:Class="MyApp.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:config="clr-namespace:MyApp.ViewModels"
StartupUri="MainWindow.xaml">
<Application.Resources>
<!-- create instance of config-settings class -->
<ObjectDataProvider x:Key="AppSettingsDataProvider" ObjectType="{x:Type config:AppSettingsManager}"/>
<ResourceDictionary x:Key="MainDictionary">
<!-- also not working: -->
<!--<ObjectDataProvider x:Key="AppSettingsDataProvider" ObjectType="{x:Type config:AppSettingsManager}"/>-->
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/Folder/file1.xaml" />
<ResourceDictionary Source="pack://application:,,,/Folder/file2.xaml" />
<!-- <ResourceDictionary Source="{Binding Source={StaticResource AppSettingsDataProvider}, Path=LoadMethod, Mode=OneWay}"/>-->
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
When I run the program my ResourceDictionary is not loaded anymore (Application.Current.Ressources.MergedDictionaries is empty), without the ObjectDataProvider everything works fine. Putting the ObjectDataProvider in the ResourceDictionary like suggested in the post Problems adding an ObjectDataProvider in resources did not help (is it different in the Application-class?). So where to put the ObjectDataProvider?

Related

Reference ResourceDictionary by key and use it as window/control resource

Is it possible to reference "nested" ResourceDictionary by x:Key instead of relative path?
App.xaml
<Application>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary x:Key="MyConstants" Source="Foo/Bar/MyConstants.xaml"/>
<!-- other stuff -->
</ResourceDictionary>
</Application.Resources>
</Application>
MainWindow.xaml
<Window.Resources>
<ResourceDictionary KeyReference="MyConstants"/>
</Window.Resources>

Why does this XAML merged resource configuration fail at runtime?

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.

Change XAML control from a different xaml file

I have 2 XAML files: MainPage.xaml and Settings.XAML
I'd like to change the MainPage.xaml LayoutRoot.Background property from the Settings.XAML. What is the best way to do this?
Create a ResourceDictionary. Create a new xaml file (e.g. Style.xaml) in your project with these contents
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=PresentationFramework">
<Color x:Key="MainBackGroundColor">#F6F5E0</Color>
</ResourceDictionary>
Update App.xaml like this. By the way if you want to split your settings into different files you can put each file in this MergedDictionaries section.
<Application x:Class="SonoCine.CineReader.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Style.xaml" />
</ResourceDictionary.
</ResourceDictionary>
</Application.Resources>
</Application>
Now you should be able to use MainBackGroundColor in your MainPage.xaml like this
Background="{StaticResource MainBackGroundColor}"

Each Dictionary entry must have an associated key attribute

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

MergedDictionaries nightmare

I'm trying to use a WPF Template and I'm supposed to include this in my App.xaml, but I get "Nested properties are not supported: ResourceDictionaries.MergedDictionaries" error and "The attachable property "MergedDictionaries" was not found in type "ResourceDictionary".
<Application x:Class="Unico.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary.MergedDictionaries>
<!-- Set default skin -->
<ResourceDictionary Source="\ExpressionDark.xaml"/>
<ResourceDictionary Source="\WindowStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Rectangle}" />
</Application.Resources>
</Application>
I've tried everything but still can't fix this. Any idea? Thanks.
MergedDictionaries is a property of ResourceDictionary.
Change to:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
...
</ResourceDictionary.MergedDictionaries>
<Style TargetType="{x:Type Rectangle}" />
</ResourceDictionary>
</Application.Resources>

Categories

Resources