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();
 }
相关推荐
weixin_447103583 小时前
WPF之绑定!
c#·wpf
蓝点lilac1 天前
C# WPF 内置解码器实现 GIF 动图控件
c#·.net·wpf·图像
@Jackasher1 天前
Redis如何实现一个分布式锁?
redis·分布式·wpf
FuckPatience3 天前
WPF 表格中单元格使用下拉框显示枚举属性的一种方式
wpf
超人也会哭️呀3 天前
Redis(九):Redis高并发高可用(集群Cluster)
数据库·redis·wpf·redis cluster·redis 集群·redis 集群搭建
望获linux4 天前
【实时Linux实战系列】实时数据流处理框架分析
linux·运维·前端·数据库·chrome·操作系统·wpf
baivfhpwxf20234 天前
wpf Image 转 90 度
wpf
麻花20135 天前
WPF的C1FlexGrid的单元格回车换行输入
wpf
zzyzxb5 天前
WPF中引用其他元素各种方法
wpf