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_com17 小时前
WPF数据绑定入门:从传统事件到5种绑定模式
wpf
LateFrames18 小时前
我用 WPF 做了一个 “苍蝇飞舞” 的屏保
ui·wpf
wuty0071 天前
完善基于WPF开发的标尺控件(含实例代码)
wpf·wpf标尺·支持横向竖向标尺·ruler
浩浩测试一下2 天前
洪水猛兽攻击 Ddos Dos cc Drdos floods区别
安全·web安全·网络安全·系统安全·wpf·可信计算技术·安全架构
无心水2 天前
分布式环境下定时任务与SELECT FOR UPDATE的陷阱与解决方案
分布式·后端·wpf·xxl-job·quartz·定时任务·selectforupdate
xdpcxq10292 天前
Spring AOP + Guava RateLimiter 用注解实现优雅限流
spring·wpf·guava
Aevget2 天前
界面控件DevExpress WPF v25.2新版亮点:模板工具包全新升级
wpf·界面控件·devexpress·ui开发·.net 10
czhc11400756633 天前
wpf 129
wpf
码界奇点4 天前
基于eBPF技术的高性能网络防火墙系统设计与实现
开发语言·网络·毕业设计·php·wpf·go语言·源代码管理
cjp5604 天前
022.WPF 封装TextBox控件限制只输入数字自定义属性
wpf