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();
 }
相关推荐
bugcome_com7 小时前
WPF样式进阶实战:外置样式+MVVM主题切换+样式优先级全解析
c#·.net·wpf
lalala_Zou15 小时前
场景题:电商平台订单未支付过期如何实现自动关闭订单?
wpf
czhc114007566315 小时前
wpf 16
wpf
cn_mengbei1 天前
鸿蒙PC原生应用开发实战:ArkTS与DevEco Studio从零构建跨端桌面应用全栈指南
华为·wpf·harmonyos
lingxiao168882 天前
WebApi详解+Unity注入--上篇:基于Framework的WebApi
c#·wpf·web
是一个Bug2 天前
Java后端开发面试题清单(50道) - 分布式基础
java·分布式·wpf
无心水2 天前
【分布式利器:腾讯TSF】4、TSF配置中心深度解析:微服务动态配置的终极解决方案
分布式·微服务·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
lingxiao168882 天前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
无心水3 天前
【分布式利器:腾讯TSF】6、TSF可观测性体系建设实战:Java全链路Metrics+Tracing+Logging落地
java·分布式·架构·wpf·分布式利器·腾讯tsf·分布式利器:腾讯tsf
故事不长丨3 天前
C#字典(Dictionary)全面解析:从基础用法到实战优化
开发语言·c#·wpf·哈希算法·字典·dictionary·键值对