zoukankan      html  css  js  c++  java
  • Silverlight使用自定义的COM组件

    Hi,

    code first:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ClassLibraryForPrint
    {
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
        public interface IComEvents
        {
            void OnPrintBegin(object sender, EventArgs e);
        }
    
        public delegate void ComDelegate(object sender, EventArgs e);
    
        [ComVisible(true)]
        [ComSourceInterfaces(typeof(IComEvents))]
        [ProgId("ClassLibraryForPrint.PrintDemos")]
        public class PrintDemos
        {
            public string Print()
            {
                if (OnPrintBegin != null) OnPrintBegin(this, new EventArgs());
                return "Printing";
            }
            public event ComDelegate OnPrintBegin;
        }
    
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Runtime.InteropServices.Automation;
    
    namespace SilverlightApplication171
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
                using (dynamic PrintDemos = AutomationFactory.CreateObject("ClassLibraryForPrint.PrintDemos"))
                {
                    AutomationEvent automationEvent = AutomationFactory.GetEvent(PrintDemos, "OnPrintBegin");
                    dynamic printStr = PrintDemos.Print();
                    label1.Content = printStr;
                }
            }
        }
    }

    Hope helpful

    Something need to do:

    1.set the ComVisible(true) in the file AssemblyInfo in properites folder in windows Class Lib.

    2.check the register for com interop in the property of the project.

    3.use the RegAsm.exe to register the dll.

    4.check the appcation is running in oob and in the out-of-browser setting ,be sure that require elevated trust is checked.

  • 相关阅读:
    【STM32F429的DSP教程】第8章 DSP定点数和浮点数(重要)
    【STM32F407的DSP教程】第8章 DSP定点数和浮点数(重要)
    【STM32H7的DSP教程】第7章 ARM DSP源码和库移植方法(IAR8)
    【STM32F429的DSP教程】第7章 ARM DSP源码和库移植方法(IAR8)
    【STM32F407的DSP教程】第7章 ARM DSP源码和库移植方法(IAR8)
    【STM32H7的DSP教程】第6章 ARM DSP源码和库移植方法(MDK5的AC5和AC6)
    【STM32F429的DSP教程】第6章 ARM DSP源码和库移植方法(MDK5的AC5和AC6)
    【STM32F407的DSP教程】第6章 ARM DSP源码和库移植方法(MDK5的AC5和AC6)
    【STM32H7的DSP教程】第5章 Matlab简易使用之常用编程语句
    【STM32F429的DSP教程】第5章 Matlab简易使用之常用编程语句
  • 原文地址:https://www.cnblogs.com/otomii/p/2541466.html
Copyright © 2011-2022 走看看