C# WPF中如何绑定静态加密资源,实现转换实时显示解密后的结果

实际工作中遇到领导要求要把使用的图片视频资源通过方法进行加密再打包,但是图片加密后在设计xaml前端时显示不出来,对前端的编写造成了一定的麻烦,给前端控件挨个命名再在cs文件里挨个指定Source然后再解密也挺麻烦的。
查了一天各种方法,需求是又要使用加密图片,又要在编辑xaml文件中能实时显示图片。
最后使用绑定静态资源和静态转换器的方法。
首先创建一个资源字典ImageDictionary.xaml(这里只放部分资源进行演示)

点击查看代码
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:sys="clr-namespace:System;assembly=mscorlib"
                    xmlns:vm="clr-namespace:CAAT_System.ViewModel">
    <vm:ImageConverter x:Key="ImageConverter"/>
    <sys:String x:Key="bg1">\exam\bg\bg1.txp</sys:String>
</ResourceDictionary>

之后在App.xaml文件中声明一下

点击查看代码
<Application x:Class="CAAT_System.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:CAAT_System"
             StartupUri="View/MainWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/CAAT_System;component/ImageDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

之后就可以直接在xaml文件中使用,也能正常实时显示图片了

点击查看代码
<UserControl x:Class="CAAT_System.View.Test.Exam.E1_1.E1_1_0"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:CAAT_System.View.Test.Exam"
             mc:Ignorable="d" 
             xmlns:hc="https://handyorg.github.io/handycontrol"
             d:DesignHeight="918" d:DesignWidth="1920">
    <Grid>
        <Image Source="{Binding Source={StaticResource bg2}, Converter={StaticResource ImageConverter}}" Stretch="UniformToFill" HorizontalAlignment="Center" VerticalAlignment="Bottom"/>
        <Button Margin="0,298,0,0" Height="472" Width="444" VerticalAlignment="Top" Click="Button_Click">
            <Button.Template>
                <ControlTemplate>
                    <hc:ImageBlock Margin="10" Source="{Binding Source={StaticResource 小狗}, Converter={StaticResource ImageConverter}}" Interval="0:0:0.03" Columns="8" Rows="3" StartColumn="0" StartRow="0" EndColumn="2" EndRow="2" IsPlaying="True"/>
                </ControlTemplate>
            </Button.Template>
        </Button>
    </Grid>
</UserControl>

实时效果如下

转换器代码如下,Help.GetImageSource这个加密方法就不提供了,返回类型为BitmapImage

点击查看代码
public class ImageConverter : IValueConverter
 {
     private static readonly ConcurrentDictionary<string, BitmapImage> _cache = new();

     private static readonly Timer _cleanupTimer = new Timer(CleanCache, null,TimeSpan.FromMinutes(15), TimeSpan.FromMinutes(15));

     private static void CleanCache(object state)
     {
         _cache.Clear(); // 线程安全的清空操作
     }

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
     {
         if (value is string path)
         {
             return _cache.GetOrAdd(path, p => Help.GetImageSource(p));
         }
         return null;
     }

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
     {
         throw new NotImplementedException();
     }
 }

补充一点,后来又发现资源文件夹及资源必须在程序根目录下,xaml开发时才能实时预览,在程序根目录的父级目录或者其他地址下,xaml就不能实时预览了,这个暂时不知道是什么原因,不知道有没有大佬能解答。
顺便一提,本文提供的方案,是不用把资源文件挨个添加进解决方案里的。

posted @ 2025-06-04 17:15  雨过天晴MS  阅读(63)  评论(0)    收藏  举报