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();
 }
相关推荐
somethingGoWay1 天前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay1 天前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
self_myth2 天前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
c#上位机2 天前
wpf之TextBlock
c#·wpf
玉面小君3 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia
c#上位机4 天前
wpf之Border
c#·wpf
SunflowerCoder4 天前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
周杰伦fans4 天前
WPF中的DataContext以及常见的绑定方式
wpf
没有bug.的程序员5 天前
Redis 数据结构全面解析:从底层编码到实战应用
java·数据结构·redis·wpf
somethingGoWay5 天前
wpf 自定义输入ip地址的文本框
wpf