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),运行之,大功告成。(作者:一剑)




  • 相关阅读:
    NOIP模拟题 管道
    NOIP模拟题 序列
    NOIP模拟题 栅栏
    NOIP模拟题 斐波那契数列
    CodeForces 797F Mice and Holes
    CodeForces 589H Tourist Guide
    CERC2016 爵士之旅 Jazz Journey
    BZOJ3832 Rally
    BZOJ1061 NOI2008 志愿者招募
    js数组的操作
  • 原文地址:https://www.cnblogs.com/aswordok/p/3735919.html
Copyright © 2011-2022 走看看