wpf问题记录

1 把文件资源 比如图片 批量改成可以复制到bin文件里 属性

复制代码
<ItemGroup>
  <Content Include="Resources\b1.jpg" />
  <Content Include="Resources\b2.jpg" />
  <Content Include="Resources\b3.jpg" />
</ItemGroup>


<ItemGroup>
  <Content Include="Resources\*.jpg">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Include="Resources\*.png">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

2 使用form的框架类 在高版本里面使用 直接修改.scproj

<UseWindowsForms>true</UseWindowsForms>

3 你的初始化逻辑不依赖 UI 渲染状态、不访问 ActualWidth/Height、不操作可视化树、不需要等窗口显示出来 ,那么放在构造函数中是完全没问题,甚至是推荐的

4 tools----生成resx

复制代码
 // 获取 .exe 所在目录(例如:bin\Debug)
 string outputDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

 // 跳到项目根目录:假设是 ..\..\
 string projectDirectory = Path.GetFullPath(Path.Combine(outputDirectory, @"..\..\.."));
 string filePath = Path.Combine(projectDirectory, "AA.resx"); // 推荐 .resx 扩展名

 string Resourcpath = projectDirectory + "\\Resources";
 // 4. 获取所有图片文件(支持常见格式)
 string[] imageFiles = Directory.GetFiles(Resourcpath, "*.*")
     .Where(file => new[] { ".jpg", ".jpeg", ".png", ".bmp", ".gif", ".ico" }
         .Contains(Path.GetExtension(file).ToLower()))
     .ToArray();
 if (imageFiles.Length == 0)
 {
     MessageBox.Show("未在 Images 文件夹中找到图片文件。");
     return;
 }


 if (File.Exists(filePath))
 {
     File.Delete(filePath);
 }
 //获取图片的resource
 ResXResourceWriter rw = new ResXResourceWriter(filePath);
 try
 {
     foreach (string imageFile in imageFiles)
     {
         string fileName = Path.GetFileName(imageFile);           // 带扩展名:b1.jpg
         string resourceName = Path.GetFileNameWithoutExtension(imageFile); // 不带扩展名:b1
         if (!fileName.StartsWith("im"))
             continue;
         try
         {
             rw.AddResource(resourceName, Image.FromFile(imageFile));
         }
         catch (Exception ex)
         {
             MessageBox.Show($"无法加载图片: {fileName}, 错误: {ex.Message}");
         }
     }
 }
 finally
 {
     rw.Generate();
     rw.Close();
 }
相关推荐
Psycho_MrZhang4 小时前
Airflow简介和架构
架构·wpf
没有bug.的程序员8 小时前
微服务中的数据一致性困局
java·jvm·微服务·架构·wpf·电商
Aevget8 小时前
DevExpress WPF中文教程:Data Grid - 如何绑定到有限制的自定义服务(二)?
wpf·devexpress·.net 10·data grid
没有bug.的程序员2 天前
SOA、微服务、分布式系统的区别与联系
java·jvm·微服务·架构·wpf·日志·gc
Macbethad2 天前
基于WPF的半导体设备配方管理程序技术方案
wpf
FuckPatience2 天前
WPF Geometry
wpf
武藤一雄3 天前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
MarkHD3 天前
车辆TBOX科普 第70次 AUTOSAR Adaptive、容器化与云原生的融合革命
云原生·wpf
极客智造3 天前
WPF Behavior 实战:自定义 InvokeCommandAction 实现事件与命令解耦
wpf
L、2183 天前
Flutter 与 OpenHarmony 深度集成:构建分布式多端协同应用
分布式·flutter·wpf