0

我尝试将WPF-Grid插入到未绑定的 Access-Form 中。我有一个使用 C# 的 Access 表单.dll。这适用于未绑定的 Access-Form。现在我想插入一个 wpf-grid 作为 ActiveX-Element。

xml:

<UserControl x:Class="ComRound.Wpf1InAccess"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:ComRound"
             mc:Ignorable="d" 
             d:DesignHeight="140" d:DesignWidth="220">
    <StackPanel Margin="3">
        <Label  Name="lblHeadline"  Height="25" Content="W P F - H e a d e r" Background="LightSkyBlue" />
        <Separator Height="5"/>
        <Grid Height="99">
            <Label  Name="lblInput" Content="Eingabefeld" Margin="20,0,0,0"/>
            <TextBox Name="fldInput" Height="25" Width="163" Margin="25,20,0,0"
                     VerticalAlignment="Top" HorizontalAlignment="Left"
                     MaxLength="50" Text="" />
            <ComboBox Name="cmbType" Height="25" Margin="25,47,26,27" SelectionChanged="CmbType_SC">
                <ComboBoxItem>up</ComboBoxItem>
                <ComboBoxItem>down</ComboBoxItem>
                <ComboBoxItem IsSelected="True">comercial</ComboBoxItem>
            </ComboBox>
            <StackPanel Orientation="Horizontal" Height="172" VerticalAlignment="Top">
                <Button x:Name="btnOk" Content="OK" HorizontalAlignment="Left" Margin="24,78,0,0" VerticalAlignment="Top" Width="75" Click="BtnOk_Click" Height="20"/>
                <Button x:Name="btnCancel" Content="Abbruch" HorizontalAlignment="Left" Margin="27,78,0,0" VerticalAlignment="Top" Width="62" Click="BtnCancel_Click" Height="20"/>
            </StackPanel>
        </Grid>
    </StackPanel>
</UserControl>

cs

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;

namespace ComRound
{
    [ComVisible(true)]
    [ProgId("ComRound.Wpf1InAccess")]
    [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch), ComSourceInterfacesAttribute(typeof(IUserControlEvents))]

    /// <summary>
    /// Interaktionslogik für Wpf1InAccess.xaml
    /// </summary>
    public partial class Wpf1InAccess : UserControl
    {
        public const String EventsId = "EC2581AC-1994-4C16-8172-6C360D07C3C0";

        private readonly RoundVal round = new RoundVal();

        public Wpf1InAccess()
        {
            InitializeComponent();
        }

        private void CmbType_SC(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            string selectedType = "commercial";
            selectedType = (string)(cmbType.SelectedItem as PropertyInfo).GetValue(null, null);
            round.SetRndTyp(selectedType);
        }

        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            string input = Convert.ToString(fldInput).Trim();
            if (input != "" && Double.TryParse(input, out double value))
            {
                string compute = Convert.ToString(round.RoundIt(value));
                MessageBoxResult result = MessageBox.Show("Ergebnis: " + compute);

            }
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.fldInput.Clear();
            this.cmbType.SelectedIndex = 2;
        }


        private void Wpf1InAccess_Load(object sender, EventArgs e)
        {
            return;
        }

        [ComRegisterFunctionAttribute]

        public static void RegisterClass(string key)

        {

            StringBuilder skey = new StringBuilder(key);

            skey.Replace(@"HKEY_CLASSES_ROOT\", "");



            // reusable GUID under ProgID="ComRound.Wpf1InAccess" above

            Type myType1 = Type.GetTypeFromProgID("ComRound.Wpf1InAccess");

            //Console.WriteLine("ProgID=ComRound.Wpf1InAccess GUID={0}.", myType1.GUID);



            // Writing the GUID in into a Textfile guid.txt

            TextWriter tw;

            tw = File.CreateText("guid.txt");

            tw.WriteLine(skey.ToString());

            tw.WriteLine(myType1.GUID.ToString());

            tw.Close();

            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);

            RegistryKey ctrl = regKey.CreateSubKey("Control");

            ctrl.Close();

            RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);

            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);

            inprocServer32.Close();

            regKey.Close();

        }





        // Unregister COM ActiveX object

        [ComUnregisterFunctionAttribute]

        public static void UnregisterClass(string key)

        {

            StringBuilder skey = new StringBuilder(key);

            skey.Replace(@"HKEY_CLASSES_ROOT\", "");

            RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(skey.ToString(), true);

            regKey.DeleteSubKey("Control", false);

            RegistryKey inprocServer32 = regKey.OpenSubKey("InprocServer32", true);

            regKey.DeleteSubKey("CodeBase", false);

            regKey.Close();

        }

        // Eventhandler interface 
        public delegate void ControlEventHandler(int NumVal);

        [Guid(EventsId)]
        [InterfaceType(ComInterfaceType.InterfaceIsDual)]

        public interface IUserControlEvents
        {
            //[DispId(0x60020001)]

            void OnButtonClick(int NumVal);
        }


    }
}

使用 Access-Form 的简单方法

using System;

namespace ComRound
{

    public class RoundVal
    {

        public enum RndTyp { up, down, comercial }
        private RndTyp rndTyp;

        public void SetRndTyp(string roundType)
        {
            switch (roundType)
            {
                case "up":
                    rndTyp = (RndTyp)0;
                    break;
                case "down":
                    rndTyp = (RndTyp)1;
                    break;
                case "comercial":
                default:
                    rndTyp = (RndTyp)2;
                    break;
            }
        }

        public double RoundIt(double rndVal)
        {
            switch (rndTyp)
            {
                case RndTyp.up:
                    return Math.Ceiling(rndVal);
                case RndTyp.down:
                    return Math.Floor(rndVal);
                case RndTyp.comercial:
                default:
                    return Math.Round(rndVal);
            }
        }
    }
}

当我尝试插入ComRound.Wpf1InAccess时,我收到消息(抱歉:它是德语):Der OLE-Server ist nicht registriert. Damit der OLE-Server registriert wrd, müssen Sie Ihn erneut installieren

chinese (like): the OLE-Server not registered to register the OLE-Server, you have to install it again

我注册了 VS2019 和 RegAsm。没有任何效果。任何有想法的人?

谢谢 Mafusys

4

0 回答 0