Binding Prevents Visio Events from Firing
I know this sounds bizarre, but it is true.
I have a simply WPF application that hosts a Visio control. There are no problems with that. Some essential events, such as DocumentOpened does work.
But if I want to handle other events, for instance, BeforeShapeDeleted, CellChanged, they stop firing once I bind Shapes to a ListBox in the DocumentOpened.
Here is my code:
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; } } }
The issue lies in the DocumentOpened method in the line that defines an ItemsSource...
According to your code, you are signing up for the CellChanged event on the Application object. Do you really want all the CellChanged events for everything in the entire application?
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);
I can't recall the ordering of events that occur when opening a Visio control and activating the window in it... It wouldn't surprise me to learn that there is no ActivePage at DocumentOpened time, or that this.visioControl.Window is not ready for some method calls during your Window_Loaded handler.
Are you observing any exceptions at all? (Or is a framework handling some and hiding them from you, such that you might not be executing all the code you think you are during your handlers...?)
There is a Visio event spy program out there, that you might want to look up. There may be a more appropriate event you could hook to sign up for events related to pages and shapes within a VisOcx instance.
WindowActivated should also fire when the control is going into run mode, and things are generally "more ready" at that later point in time...
I was in contact with Microsoft. It seems like I had some problem with Visio on my machine.
链接地址: http://www.djcxy.com/p/41830.html上一篇: 如何解决.NET Webbrowser控件中的内存泄漏问题?
下一篇: 绑定可防止Visio事件触发