WPF -- 一种添加静态资源的方式

本文介绍使用独立的xaml文件添加静态资源的方式。

步骤
  1. 创建XAML文件,如ImageButton.xaml,添加ResourceDictionary标签,并添加静态资源;
  2. 在App.xaml的Application.Resources标签中添加xaml资源文件;
  3. 在xaml界面文件中使用StaticResource使用静态资源。
示例
// ImageButton.xaml
<ResourceDictionary xmlns...>
    <Style x:Key="CustomImageButton" TargetType="Button">
        ...
    </Style>
</ResourceDictionary>

// App.xaml
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/WpfApplication1;component/CustomControls/ImageButton.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

// 使用ImageButton的xaml
<StackPanel>
    <Button Width="50" Height="30" Content="Button" FontSize="14" Style="{StaticResource CustomImageButton}" />
</StackPanel>
posted @ 2021-01-19 12:05  louzi  阅读(726)  评论(0编辑  收藏  举报