Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using style from another assembly in Metro application

I start with the Windows 8 C# XAML user and custom controls sample and moved the files

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

to a Metro Class Library named Controls, reference it in the UserAndCustomControls project and correct the local:... references to xmlns:local="using:Controls". This works great.

BUT if create a resource dictionary "Style.xaml" in the class library with

<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 ScenarioList.xaml file

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

i'm get an runtime error

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

if I try apply the 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 external style in Metro application? My idea is create reusable controls and common styles delivered as a single dll file.

like image 607
Luís Rigoni Avatar asked Dec 10 '22 01:12

Luís Rigoni


2 Answers

Your Source path is wrong as the component syntax is not supported. Assuming your control library DLL is called "Controls" then it would be 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.

like image 127
Tim Heuer Avatar answered Dec 26 '22 12:12

Tim Heuer


navits points the answer.

If you have a dll named "CustomControl" with a ResourceDictionary named "Styles.xaml" contained a folder named "Themes".

For C# projects the App.xaml should contain :

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

With the Themes folder in the Source path, for C# projects.

and

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

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

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

like image 24
Luís Rigoni Avatar answered Dec 26 '22 12:12

Luís Rigoni