How to share a value/variable across all pages in silverlight application - silverlight-4.0

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

Related

Uno Platform - Reference resource dictionary within Shared project

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>

Support multiple package name association using Branch.io

Branch takes care of creating, applying and confirming Android App Links associations when using it. And that's truly great.
However, it comes to be a tricky question where there are a few package names for the same app, as Branch doesn't give an ability to specify few package names in Android setup. Also, it's not possible to upload custom domain associations files.
Have anyone faced it? Please advice.
P.s. The problem is rather a missing feature than a bug.
Currently Branch doesn't support creating DAL associations files for multiple flavors (package names) of the same app.
I've contacted Branch support and applied a feature request for this so maybe it will be possible in the future releases.
I assume you want to add multiple package names so that the same link can give the option to open multiple apps. You can achieve this functionality by adding the same URI scheme intent filter and Android App links intent filter to the Android Manifest of all your apps.
<!-- Branch URI Scheme -->
<intent-filter>
<data android:scheme="androidexample" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<!-- Branch App Links (optional) -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" android:host="example.app.link" />
<data android:scheme="https" android:host="example-alternate.app.link" />
</intent-filter>
If added, whenever a person clicks on the link it will provide to option to open the link via all the apps which can handle the intent. In order to read the link parameters, you can use the same Branch key for all apps.
I am not sure why you need to upload a custom domain associations file. It would be really helpful if you could provide more details about your use case to understand what you wish to achieve.

How to group color codes into one single file in a Windows Phone 8.1 Silverlight App

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>

Windows 10 Universal Merged Dictionaries

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!

Use a resource dictionary as theme in Silverlight

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