首页 文章

绑定可防止Visio事件被触发

提问于
浏览
1

我知道这听起来很奇怪,但确实如此 .

我有一个简单的WPF应用程序,承载Visio控件 . 这没有问题 . 一些重要事件,例如DocumentOpened确实有效 .

但是如果我想处理其他事件,例如,BeforeShapeDeleted,CellChanged,一旦我将Shapes绑定到DocumentOpened中的ListBox,它们就会停止触发 .

这是我的代码:

public partial class MainWindow : Window
{
    private AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl visioControl = new AxMicrosoft.Office.Interop.VisOcx.AxDrawingControl();

    public MainWindow()
    {
        InitializeComponent();
        this.host.Child = this.visioControl;
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        this.visioControl.DocumentOpened += new AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEventHandler(visioControl_DocumentOpened);
        this.visioControl.Window.Application.BeforeShapeDelete += new Microsoft.Office.Interop.Visio.EApplication_BeforeShapeDeleteEventHandler(Application_BeforeShapeDelete);
        this.visioControl.Window.Application.CellChanged += new Microsoft.Office.Interop.Visio.EApplication_CellChangedEventHandler(Application_CellChanged);
    }

    void Application_CellChanged(Microsoft.Office.Interop.Visio.Cell Cell)
    {
        MessageBox.Show("Changed");
    }

    void Application_BeforeShapeDelete(Microsoft.Office.Interop.Visio.Shape Shape)
    {
        MessageBox.Show("Deleted");
    }

    void visioControl_DocumentOpened(object sender, AxMicrosoft.Office.Interop.VisOcx.EVisOcx_DocumentOpenedEvent e)
    {
        //if I comment the line bellow BeforeShapeDelete and CellChanged will work, if I leave it uncommented, they won't work...
        lstShapes.ItemsSource = this.visioControl.Window.Application.ActivePage.Shapes;
    }

    private void mnuOpen_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlgOpenDiagram = new Microsoft.Win32.OpenFileDialog();

        if (dlgOpenDiagram.ShowDialog() == true)
        {
            this.visioControl.Src = dlgOpenDiagram.FileName;
        }
    }
}

问题在于定义ItemsSource的行中的DocumentOpened方法...

2 回答

  • 0

    根据您的代码,您正在注册Application对象上的CellChanged事件 . 您是否真的希望所有CellChanged事件都适用于整个应用程序中的所有内容?

    this.visioControl.Window.Application.BeforeShapeDelete += new Microsoft.Office.Interop.Visio.EApplication_BeforeShapeDeleteEventHandler(Application_BeforeShapeDelete);
        this.visioControl.Window.Application.CellChanged += new Microsoft.Office.Interop.Visio.EApplication_CellChangedEventHandler(Application_CellChanged);
    

    我无法回想起打开Visio控件并激活其中的窗口时发生的事件的顺序......我不会感到惊讶的是,在DocumentOpened时间没有ActivePage,或者this.visioControl.Window是在Window_Loaded处理程序期间没有准备好进行某些方法调用 .

    你在观察任何例外吗? (或者是一个框架处理一些并将它们隐藏起来,这样你可能不会执行你认为你在处理程序中的所有代码......?)

    有一个Visio事件 Spy 程序,你可能想要查找 . 可能有一个更合适的事件可以挂钩以注册与VisOcx实例中的页面和形状相关的事件 .

    WindowActivated也应该在控件进入运行模式时触发,并且事情通常在稍后的时间点“更准备好”......

  • 0

    我和微软有联系 . 好像我的机器上的Visio有些问题 .

相关问题