将一坨WinForm文件包装成一个exe

WinRAR:绿色自解压应用的简便解决方案
本文分享了如何使用WinRAR创建绿色小应用,通过简单步骤打包和设置,实现自解压并配置启动路径,避免.netcore3.1新特性问题。

什么 .net core 3.1 新特性,ILMerge,Costura.Fody 都试了一遍,真心都不好使。

还是 WinRAR 最实在~

第一步选中 Release 或它的子文件夹,就是最终包含你所有执行文件的那个目录。

右键 -> 添加到压缩文件

起个你喜欢的名字,记得后缀改成 .exe

打开自解压选项

填入一个启动路径,原则就是假想你当前解压到的位置

模式里,勾上解包到临时文件夹,安静模式选择全部隐藏

完美解决,绿色小应用的福音!

WinForm 界面封装为 DLL 并在 WPF 项目中调用,主要涉及 WinForm 控件的封装与 WPF 对 WinForm 控件的承载。WPF 提供了 `WindowsFormsHost` 类,允许将 WinForm 控件嵌入 WPF 应用程序中。 ### 3.1 创建 WinForm 控件库 首先,创建一个 WinForm 控件库项目(Class Library),并添加一个用户控件(UserControl)作为界面组件: ```csharp public partial class MyWinFormUserControl : UserControl { public MyWinFormUserControl() { InitializeComponent(); var label = new Label(); label.Text = "这是一个 WinForm 控件"; this.Controls.Add(label); } } ``` 在项目属性中,将输出类型设置为“类库”(Class Library),然后编译生 `.dll` 文件。 ### 3.2 在 WPF 项目中引用 WinForm DLL 在 WPF 项目中引用 WinForm 控件库的 DLL,并添加对 `System.Windows.Forms` 和 `WindowsFormsIntegration` 的引用。 在 XAML 中使用 `WindowsFormsHost` 来承载 WinForm 控件: ```xml <Window x:Class="MyWpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <WindowsFormsHost Name="winFormsHost"/> </Grid> </Window> ``` 在代码后台中实例化 WinForm 控件并添加到 `WindowsFormsHost` 中: ```csharp public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var winFormControl = new MyWinFormUserControl(); winFormsHost.Child = winFormControl; } } ``` ### 3.3 多线程处理与 UI 同步 由于 WinForm 和 WPF 都采用单线程单元(STA)模型进行 UI 操作,跨线程更新控件时需使用 `Invoke` 方法确保线程安全。例如: ```csharp public class WPFHost : IWorkHost { private Dispatcher _parent; public object Parent => _parent; public WPFHost(Control parent) { _parent = parent.Dispatcher; } public void Invoke(Delegate method) { _parent.Invoke(method); } } ``` 该方式可确保在 WPF 主线程中执行 WinForm 控件的更新操作,避免跨线程异常。 ### 3.4 注意事项 - 确保在 WPF 项目中启用混合模式支持,即在 App.xaml 中添加对 `WindowsFormsIntegration` 的引用。 - 避免在 WinForm 控件中使用与 WPF 不兼容的 API。 - WinForm 控件的性能和样式在 WPF 中可能不完全一致,需进行适配和测试。 ---
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值