WPF如何在后台动态加载资源字典(Dictionary)中的样式文件

本文介绍了如何在XAML中定义并动态加载样式到界面按钮,展示了如何在App.xaml.cs中操作资源字典,并在UI元素上应用自定义样式。通过实例演示了如何在按钮上创建切换效果,适合前端与后端开发者学习WPF或UWP应用的样式管理。
摘要由CSDN通过智能技术生成
一、首先在资源字典中定义好样式。
样式:
<!--按钮1样式-->
    <Style x:Key="btn1" TargetType="Button">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="Background" Value="#6B6B6B"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="{TemplateBinding Background}"  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" CornerRadius="5">
                        <StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
                            <TextBlock Text="{TemplateBinding Content}" FontWeight="{TemplateBinding FontWeight}" FontSize="{TemplateBinding FontSize}" TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        </StackPanel>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" Value="#77BEFF"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

二、在App.xaml.cs文件中添加以下代码(注意在App.xaml中不用引用资源字典样式):

/// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public static App Instance;
        public static String Directory;
        public App()
        {
            Instance = this;
            Directory = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
        }

        
        /// <summary>
        /// Dynamically load a style from a file
        /// </summary>
        public void LoadStyleDictionaryFromFile(string inFileName)
        {
            if (File.Exists(inFileName))
            {
                try
                {
                    using (var fs = new FileStream(inFileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        // Read in ResourceDictionary File
                        var dic = (ResourceDictionary)XamlReader.Load(fs);
                        // Clear any previous dictionaries loaded
                        Resources.MergedDictionaries.Clear();
                        // Add in newly loaded Resource Dictionary
                        Resources.MergedDictionaries.Add(dic);
                    }
                }
                catch (Exception ex)
                {
                    Services.LogHelper.ErrorLog("LoadStyleDictionaryFromFile:",ex);
                }
            }
        }
    }

三、在界面的控件中用样式动态绑定的方式绑定样式中的key值:

<Grid  Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Bottom">
                <Button x:Name="One" Content="1" Click="One_Click" Style="{DynamicResource ResourceKey=btn2}"/>
            </Grid>
            <Grid  Grid.Row="0" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Bottom">
                <Button x:Name="Two" Click="Two_Click" Content="2"  Style="{DynamicResource ResourceKey=btn2}"/>
            </Grid>

四、在该界面的后台文件(cs文件)中添加动态加载样式文件:

//stylefile :是资源文件路径
string stylefile = $"{App.Directory}\\{"Dictionary"}\\{"MainNight.xaml"}";
App.Instance.LoadStyleDictionaryFromFile(stylefile);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值