Thanh Le
Thanh Le

Reputation: 773

how to load custom staticresource

I have to 2 files: ButtonStyle.xaml and ConstantsStyle.xaml

in App.xaml, i will init

    <Application.Resources>        
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ConstantsStyle.xaml"/>
                    <ResourceDictionary Source="/Zalo;component/ResourceDictionary/480x800/ButtonStyle.xaml"/>
                 </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
......
    </Application.Resources>

file ConstantsStyle.xaml

<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=System.Windows">

    <Thickness x:Key="GenericButtonStylePadding">0,7</Thickness>

</ResourceDictionary>

file ButtonStyle.xaml

<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=System.Windows"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
    xmlns:usercontrols="clr-namespace:Zalo.UserControls">

    <Style x:Key="GenericButtonStyle" TargetType="Button">
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="{StaticResource GenericButtonStylePadding}"/>
   </Style>

</ResourceDictionary>

App crash runtime. Because Cannot find a Resource with the Name/Key GenericButtonStylePadding???

How can i run app correctly??? Please help me

Upvotes: 0

Views: 184

Answers (1)

Parvinder Singh
Parvinder Singh

Reputation: 19

Try using DyanamicResource instead of StaticResource

<Style x:Key="GenericButtonStyle" TargetType="Button">
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="{DynamicResource GenericButtonStylePadding}"/>
   </Style>

Edit: You can test one thing. Add another constant to Constant.XAML

<Brush x:Key="BGBrush">Black</Brush>

Try to use it in your ButtonStyle.XAML

<Style x:Key="GenericButtonStyle" TargetType="Button">
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Width" Value="200"></Setter>
        <Setter Property="Background" Value="{StaticResource BGBrush}"></Setter>
    </Style>

See if button background changes to Black. If color is changing then try something else than Thickness.

Upvotes: 1

Related Questions