zoukankan      html  css  js  c++  java
  • 嵌入资源第一讲:Dll嵌入到WPF中

    一、新建类库a:

    namespace a
    {
        public class Class1
        {
            public string call()
            { return "Hello world!"; }
        }
    }

    生成release版的a.dll备用。

    二、新建WPF应用程序b:
    添加a.dll的引用,并复制a.dll到项目b,修改为嵌入的资源,

    修改App.xaml,删掉其中的StartupUri="MainWindow.xaml",
    修改App.xaml.cs,改写OnStartup:

    using System.Reflection;

    namespace
    b { /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); AppDomain.CurrentDomain.AssemblyResolve += (sender, args) => { String projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString(); using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + ".a.dll")) { Byte[] b = new Byte[stream.Length]; stream.Read(b, 0, b.Length); return Assembly.Load(b); } }; MainWindow m = new MainWindow(); m.Show(); } } }

    修改MainWindow.xaml.cs:

    namespace b
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                a.Class1 o = new a.Class1();
                MessageBox.Show(o.call());
            }
        }
    }

    生成,复制b.exe到桌面(确保没有可见的a.dll),运行之,大功告成。(作者:一剑)




  • 相关阅读:
    分页通信
    减少页面加载时间的方法
    config/index.js
    5.20 单词小记
    nginx启动报错(1113: No mapping for the Unicode character exists in the target multi-byte code page)
    Windows系统下hosts文件工作原理(转)
    5.19 英语单词小计
    mybatis sql语句转化
    (转) Java 静态代码块和非静态代码块
    5.18英语单词小记
  • 原文地址:https://www.cnblogs.com/aswordok/p/3735919.html
Copyright © 2011-2022 走看看