Using a Style from a Different Assembly in a Metro App

I start with Windows 8 C # sample user and XAML user controls and move files

Themes/Generic.xaml BasicCustomControl.cs BasicUserControl.xaml BasicUserControl.xaml.cs ImageWithLabelControl.cs 

to the Metro class library called Controls , refer to it in the UserAndCustomControls project and correct the local:... links to xmlns:local="using:Controls" . This works great.

BUT, if you create the "Style.xaml" resource library in the class library using

 <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Controls"> <Color x:Key="ColorBackground">Red</Color> </ResourceDictionary> 

and include in the file ScenarioList.xaml

 <Page.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Controls;component/Style.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Page.Resources> 

I get a runtime error

 XamlParseException Failed to assign to property 'Windows.UI.Xaml.ResourceDictionary.Source'. 

if I try to apply color to the grid

 <Grid> <Grid.Background> <SolidColorBrush Color="{StaticResource ColorBackground}" /> </Grid.Background> <ListBox x:Name="Scenarios" ... [...] </Grid> 

[Q] The question is, how do I correctly declare, reference and use the external style in a Metro application? My idea is to create reusable controls and common styles that come as a single dll file.

+4
source share
2 answers

navits indicates an answer.

If you have a dll named "CustomControl" with ResourceDictionary in the name "Styles.xaml" contains a folder called "Themes".

For C # projects, App.xaml should contain:

 <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ms-appx:///CustomControls/Themes/Styles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

With Themes folder in source path, for C # projects.

and

For VB.Net projects, App.xaml should contain:

 <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ms-appx:///CustomControls/Styles.xaml"/> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> 

Without Themes folder in the source path, for VB.Net projects.

+4
source

The source path is incorrect because the component syntax is not supported. Assuming your control library DLL is called Controls, it will look like this:

 <ResourceDictionary Source="ms-appx:///Controls/Files/Style.xaml" /> 

You can look at http://timheuer.com/blog/archive/2012/03/07/creating-custom-controls-for-metro-style-apps.aspx for a deeper explanation.

+10
source

Source: https://habr.com/ru/post/1403948/


All Articles