I have developed an application which allows the user to switch between themes. I'm doing this by including the xaml file as a resource in my project and using the following code:
MainTheme.ThemeUri = new Uri("SilverlightApplication1;component/Themes/[ThemeName]/Theme.xaml", UriKind.Relative);
This worked well, untill I found these themes: http://timheuer.com/blog/archive/2010/05/17/silverlight-4-tools-released-and-new-application-templates.aspx
The difference is that these themes consist of multiple files. So I made a Theme.xaml file that only includes MergedDictionaries so I could still use the code above. This is the Theme.xaml file for the Cosmopolitan theme.
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="CoreStyles.xaml"/>
<ResourceDictionary Source="SDKStyles.xaml"/>
<ResourceDictionary Source="Styles.xaml"/>
<ResourceDictionary Source="ToolkitStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
However, when I run the c# code above I get the following exception:
System.Windows.Markup.XamlParseException: Failed to assign to property 'System.Windows.ResourceDictionary.Source'.
Just to be clear, using the MergedDictionaries method does work when I set it in my App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/Cosmopolitan/Theme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
What am I doing wrong?
Thanks!
When you are using MergedDictionary you have to use fully qualified name like below.
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/SilverlightApplication1;component/Themes/Cosmopolitan/Theme.xaml"/>
Also, note that you should not miss the slash before the assembly name. In other words, it should be like
Source="/SilverlightApplication1;
not like
Source="SilverlightApplication1;
HTH
Related
I'm building an Uno app and need to reference a Resource Dictionary defined and stored in the Shared Project.
The project is set up like so:
And in MainPage.xaml, I'm using:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///LaunchShowcase.Shared/Themes/CenteredPivotHeadersStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
This results in the error message Cannot locate resource from 'ms-appx:///LaunchShowcase.Shared/Themes/CenteredPivotHeadersStyle.xaml'
What's the proper way to reference this resource dictionary?
The shared project is not a "real" project, as would a library. The resource dictionary file is behaving as if it were directly integrated in the head project, therefore the name LaunchShowcase.Shared does not exist.
Try using this instead:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///Themes/CenteredPivotHeadersStyle.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
I've got an Windows Phone 8.1 Silverlight app, using the Caliburn.Micro framework.
Atm all my color codes are hardcoded into the place they get used.
Foreground="#c8d75a"
Which means i have that color code hardcoded about 150 places in my app.
So i thought i would group all colors into one file and then have a reference to the color in my xaml pages.
I've done a lot of Google searches and they all turn up with the answer "Use a Resource Directory" and then in my xaml page i would be able to use the variables in the Directory like i would with any other static resoruce
{StaticResource LightGreen}
My problem is i don't have any templates called Resource Directory. So my question is this:
Is this even possible in a Windows Phone 8.1 Silverlight app to add a Resource Directory? if not what should i use then?
Thank you for your time.
Of course it is possible. I don't know why you don't have a file template for a ResourceDictionary but you can create one yourself.
Lets say you have a folder named Resources in your main project, then you need to create a file with the .xaml extension, for example Constants.xaml. You can do this outside visual studio and then just copy the file to your project.
The contents of the file should be something like the following:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<!-- SOCIAL NETWORKS -->
<Color x:Key="FacebookColor">#3B5998</Color>
<Color x:Key="GoogleColor">#DB4A39</Color>
<Color x:Key="TwitterColor">#00A0D1</Color>
<SolidColorBrush x:Key="FacebookBrush" Color="{StaticResource FacebookColor}"/>
<SolidColorBrush x:Key="GoogleBrush" Color="{StaticResource GoogleColor}"/>
<SolidColorBrush x:Key="TwitterBrush" Color="{StaticResource TwitterColor}"/>
<!-- BOOLEANS -->
<system:Boolean x:Key="BoolTrue">True</system:Boolean>
<system:Boolean x:Key="BoolFalse">False</system:Boolean>
<!-- COLORS -->
<Color x:Key="LightGreen">#c8d75a</Color>
<!-- BRUSHES -->
<SolidColorBrush x:Key="LightGreenBrush" Color="{StaticResource LightGreen}"/>
</ResourceDictionary>
Then you need to include the created ResourceDictionary to your App.xaml:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Constants.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
or if you want to include the dictionary in a page:
<Page.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/Constants.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Page.Resources>
I'm using the latest Visual Studio:
Microsoft Visual Studio Professional 2015
Version 14.0.24720.00 Update 1
Microsoft .NET Framework
Version 4.6.01038
When I create a new WPF project (TargetFrameworkVersion: v4.5.2) and add MvvmLightLibs (latest Version: 5.2.0.0) by NuGet inside the following XAML
<UserControl x:Class="MergedDirectoriesTest.Views.SubView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:MergedDirectoriesTest.ViewModels"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command="http://www.galasoft.ch/mvvmlight">
...
<TextBox Style="{StaticResource FormTextBoxStyle}" Text="{Binding LastName, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="KeyUp">
<command:EventToCommand Command="{Binding TextEnteredCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</UserControl>
EventToCommand will be marked as error
Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.
If I change the XML namespace to
<UserControl ...
xmlns:command="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Platform">
I get this
The type 'EventToCommand' from assembly 'GalaSoft.MvvmLight.Platform' is built with an older version of the Blend SDK, and is not supported in a Windows Presentation Framework 4 project.
For your information: I use the System.Windows.Interactivity provided by MVVMLight. As far as I know it's the newest version:
<Reference Include="System.Windows.Interactivity, Version=4.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\MvvmLightLibs.5.2.0.0\lib\net45\System.Windows.Interactivity.dll</HintPath>
<Private>True</Private>
</Reference>
So I have a Windows Universal Class Library that has a resource dictionary in it that I want to merge with my Windows 10 Universal Application's main resource dictionary in App.xaml.
My App.xaml simply merges in my main Resource dictionary from the same assembly.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Styles/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
Then from my main resource dictionary (Styles/Styles.xaml) I am merging in other resource dictionaries from the same assembly. This is where I would like to merge in a resource dictionary from another assembly:
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Fields.xaml"/>
<ResourceDictionary Source="DataTemplates.xaml"/>
<!--<ResourceDictionary Source="/{AssemblyName};component/Shared.xaml" />-->
<!--<ResourceDictionary Source="pack://application:,,,/{AssemblyName};component/Shared.xaml" />-->
<ResourceDictionary Source="ms-appx:///{AssemblyName}/Shared.xaml" />
</ResourceDictionary.MergedDictionaries>
I've tried adding this to my main resource dictionary:
<ResourceDictionary Source="/{AssemblyName};component/Shared.xaml" />
and this...
<ResourceDictionary Source="ms-appx:///{AssemblyName}/Shared.xaml" />
Based on this article about Windows 8.x Store Apps this seems like how it should work. But it still doesn't work.
and this...
<ResourceDictionary Source="pack://application:,,,/{AssemblyName};component/Shared.xaml" />
(this is the WPF way, I know, but I thought I would give it a try anyway!)
But none seem to work...
The build action of the resource dictionaries that I have in my application assembly are set to 'Page'. These resource dictionaries are working simply using this in the merge:
<ResourceDictionary Source="Styles/Styles.xaml"/>
I get the following cryptic error:
Failed to assign to property
'Windows.UI.Xaml.ResourceDictionary.Source' because the type
'Windows.Foundation.String' cannot be assigned to the type
'Windows.Foundation.Uri'. [Line: 12 Position: 37]
As Romasz mentioned in comment, you need to reference the project which including the styles. And then using the following code to reference.
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///UserControlLibs/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
XAML Merged Dictionaries are trickier than they seem. All is well if you are referencing a local project, then your Source= paths well.
If you are referencing an external DLL (not in-solution), the referenced DLL folder must also have all *.xml, *.xr.xml, *.xbf, *.jpg/png/gif, etc.
The procedure I follow is:
1. Reference DLL containing the merged dictionaries (XAML style sheets).
2. Ensure the reference path has all required files.
3. Add the merged dictionary reference to your App.xaml
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ms-appx:///NAMESPACE_HERE/Styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
This PostBuild.bat file describes how I have VS copy all required files on successful build:
Echo Starting: PostBuildEvent: Call $(ProjectDir)PostBuild.Bat $(ProjectDir) $(OutDir) $(TargetPath) $(RootNameSpace)
Echo With Parameters: %1 %2 %3 %4
REM ***
REM *** Variables
REM ***
SET BuildLocationBin=..\..\..\..\..\..\..\..\bin
REM ***
Echo *** Publish to Bin
REM ***
MD %BuildLocationBin%
%WINDIR%\system32\attrib.exe %BuildLocationBin%\*.* -r /s
%WINDIR%\system32\xcopy.exe %1Properties\*.rd.xml %BuildLocationBin%\%4\Properties\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.png %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.xbf %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.xml %BuildLocationBin%\%4\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %3 %BuildLocationBin%\*.* /s/r/y
%WINDIR%\system32\xcopy.exe %1%2*.pri %BuildLocationBin%\*.* /s/r/y
Echo *** Postbuild Complete ***
Hope this helps!
Global file does not exist in silver light,
What would be the way to share data across pages, within the same session?
You will need to put it in a resource file and setup MergedDictionaries in your App.xaml (or just put the Data straight in your App.xaml)
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="..." />
<ResourceDictionary Source="..." />
<ResourceDictionary Source="..." />
</ResourceDictionary.MergedDictionaries>
define the data
<sys:String x:Key="SingleString">Hello World</sys:String>
You then have access to it with with StaticResource