转载修改自https://blog.csdn.net/lin381825673/article/details/39122257

添加Resources

1、双击Properties/Resources.resx
2、添加资源,选择dll文件

添加加载代码

在主窗体代码初始化之前添加如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public MainWindow()
{
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
InitializeComponent();
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
string dllName = args.Name.Contains(",") ? args.Name.Substring(0, args.Name.IndexOf(',')) : args.Name.Replace(".dll", "");
dllName = dllName.Replace(".", "_");
if (dllName.EndsWith("_resources")) return null;
ResourceManager rm = new ResourceManager(GetType().Namespace + ".Properties.Resources", Assembly.GetExecutingAssembly());
byte[] bytes = (byte[])rm.GetObject(dllName);
return Assembly.Load(bytes);
}