首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何加载自定义静态资源

如何加载自定义静态资源
EN

Stack Overflow用户
提问于 2015-05-11 12:42:26
回答 1查看 100关注 0票数 0

我有两个文件: ButtonStyle.xaml和ConstantsStyle.xaml

在App.xaml中,我将初始化

代码语言:javascript
复制
    <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>

文件ConstantsStyle.xaml

代码语言:javascript
复制
<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>

文件ButtonStyle.xaml

代码语言:javascript
复制
<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>

应用程序崩溃运行时。因为找不到名称/密钥为GenericButtonStylePadding的资源?

如何正确运行app?请帮帮我

EN

回答 1

Stack Overflow用户

发布于 2015-05-11 13:12:10

尝试使用DyanamicResource而不是StaticResource

代码语言:javascript
复制
<Style x:Key="GenericButtonStyle" TargetType="Button">
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="{DynamicResource GenericButtonStylePadding}"/>
   </Style>

编辑:你可以测试一件事。向Constant.XAML添加另一个常量

代码语言:javascript
复制
<Brush x:Key="BGBrush">Black</Brush>

试着在你的ButtonStyle.XAML中使用它

代码语言:javascript
复制
<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>

查看按钮背景是否变为黑色。如果颜色在变化,那么可以尝试厚度之外的其他颜色。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30159796

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档