Viso VSTO - ShapeAdded 事件未触发(有时) [英] Viso VSTO - ShapeAdded event not firing (sometimes)

查看:45
本文介绍了Viso VSTO - ShapeAdded 事件未触发(有时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Visio 加载项中,我为ShapeAdded"设置了一个处理程序.这会为添加的前 2 或 3 个形状触发,但随后完全停止触发.

In my Add-in for Visio, I have set a handler for 'ShapeAdded'. This fired for the first 2 or 3 shapes that are added, but then just stop firing altogether.

以下是我的加载项功能的基本概述:

Here's the basic outline of how my add-in functions:

  1. 用户向页面添加形状
  2. 在 ShapeAdded 上,显示一个表单.
  3. 用户在表单中输入文本,按下搜索按钮
  4. 调用存储过程(参数 = 用户文本)
  5. Form 的 datagridview 填充了结果.
  6. 需要用户双击结果行.
  7. 表单关闭,所选值变为形状文本.

如果我在第 (3) 项之后注释掉我的代码 - 那么我的事件处理程序会继续触发而不会出现问题.我可以整天添加新形状.

If I comment out my code after item (3) - then my event handler continues firing without issue. I can add new shapes all day long.

但是 - 一旦我让代码调用存储过程(第 4 步),就会出现问题.非常具体:da.Fill(dt)我可能会管理 1 到 6 个形状添加,但迟早,事件会停止触发.(* 1 月 8 日更新:记录集大小似乎影响了这个问题.每次返回 1100 行时,我设法向我的页面添加了大约 6 个形状.每次返回 3 行时,我最多可以在返回之前添加 18 个形状事件停止触发.这告诉我,我处理数据调用的方式有些不干净"——但我看不到它是什么!!)

BUT - once I let code call the stored procedure (step 4), then that is where problems arise. Very specifically : da.Fill(dt) I may manage 1 to 6 shape adds, but sooner or later, the event just stops firing. (*update 8th Jan: Recordset size seems to affect the issue. When 1100 rows are returned each time, I manage to add around 6 shapes to my page. When 3 rows returned each time, I get to add up to 18 shapes before the events stop firing. That tells me there is something not 'clean' about the way I am handling my data calls - but I cannot see what it is !!)

我完全不明白为什么调用存储过程会干扰我的事件处理程序!?!?尤其是没有任何错误消息.

I am totally baffled as to why calling the stored procedure would interfere with my event handler !?!? Especially without any error messages.

有人对我可能做错了什么有任何想法吗?甚至,关于如何以更好的方式调试这个的想法?

Has anyone any ideas whatsoever as to what I might be doing wrong ? Or even, ideas around how to debug this in a better manner ?

public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Globals.ThisAddIn.Application.MarkerEvent += new Visio.EApplication_MarkerEventEventHandler(Application_MarkerEvent);
        }

        private void Application_MarkerEvent(Visio.Application visapp, int SequenceNum, string ContextString)
        {
            if (ContextString.Contains("soln=myApplication") && ContextString.Contains("/cmd=DocCreated"))
            {
                SetDocEvents();
            }
        }

        public void SetDocEvents()
        {
             Microsoft.Office.Interop.Visio.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

            // set event handler
            try
            {
                doc.ShapeAdded += new Microsoft.Office.Interop.Visio.EDocument_ShapeAddedEventHandler(onShapeAdded);
            }
            catch (Exception err)
            {
                System.Diagnostics.Debug.WriteLine(err.Message);
                throw;
            }
        }

        private void onShapeAdded(Visio.Shape Shape)
        {
            Form_Entity fe = new Form_Entity(Shape.Text);
            fe.ShowDialog();
            fe.Dispose();
        }

    // ... other stuff
}

表格代码:

public partial class Form_Entity : Form
{
    public Int32 eid { get { return m_id; } }
    public string ename { get { return m_name; } }
    public string eabbr { get { return m_abbr; } }

    private Int32 m_id;
    private string m_name;
    private string m_abbr;

    public Form_Entity()
    {
        InitializeComponent();
    }
    public Form_Entity(String search)
    {
        InitializeComponent();
        txt_search.Text = search;
    }

    private void Cmd_Search_Click(object sender, EventArgs e)
    {
        String sample = txt_search.Text;

        DataTable dt = new DataTable();

SqlConnection myConn = new SqlConnection("Server=xxxxxxx;Database=xxxxxxxx;Trusted_Connection=True;");
        myConn.Open();
        SqlCommand myCmd = new SqlCommand("[dbo].[mySearch]", myConn);
        myCmd.Parameters.Add(new SqlParameter("@searchText", sample));
        myCmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter(myCmd);
        da.Fill(dt);
        dataGridView1.DataSource = dt;

        myCmd.Dispose();
        myConn.Close();

    }
}

** 此项目的文件

  • Visual Studio 解决方案
  • T-SQL 创建示例表/data.procedure
  • Viso 模板
  • 自述文件

http://www.netshed.co.uk/temp/Vis_Sample.zip

推荐答案

我认为这里的问题是您没有将 doc 对象保留在范围内,并且 Visio 将停止报告事件没有参考.

I think the problem here is that you're not keeping the doc object in scope and Visio will stop reporting events for which there is no reference.

您可以按如下方式添加字段(或属性),然后应维护引用和相关事件:

You can add a field (or property) as follows and then the reference and associated events should be maintained:

public partial class ThisAddIn
{
    private Visio.Document _targetDoc = null;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        Application.MarkerEvent += Application_MarkerEvent;
    }

    public void SetDocEvents()
    {
        _targetDoc = Globals.ThisAddIn.Application.ActiveDocument;

        // set event handler
        try
        {
            _targetDoc.ShapeAdded += onShapeAdded;
        }
        catch (Exception err)
        {
            System.Diagnostics.Debug.WriteLine(err.Message);
            throw;
        }
    }

这篇关于Viso VSTO - ShapeAdded 事件未触发(有时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆