Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# e、 data.GetData始终为空_C#_Drag And Drop_Dsl_Vsx_Visual Studio 2010 - Fatal编程技术网

C# e、 data.GetData始终为空

C# e、 data.GetData始终为空,c#,drag-and-drop,dsl,vsx,visual-studio-2010,C#,Drag And Drop,Dsl,Vsx,Visual Studio 2010,我正在与VisualStudio2010合作,开发一个扩展 我需要从工具窗口中的WPF树视图拖放到DSL图上,但当我调用e.data.GetData时,我无法获得值,我想知道我做错了什么 private void OnDragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(typeof(SqlServerTable))) { try

我正在与VisualStudio2010合作,开发一个扩展

我需要从工具窗口中的WPF树视图拖放到DSL图上,但当我调用e.data.GetData时,我无法获得值,我想知道我做错了什么

    private void OnDragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(SqlServerTable)))
        {
            try
            {
                SqlServerTable table = (SqlServerTable)e.Data.GetData(typeof(SqlServerTable));
                MessageBox.Show(table.Name);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
第一个if语句解析为True。这会告诉我它就是那种东西。 以下是WPF树视图中的内容:

        private void DataSourceExplorerTreeView_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            if (DataSourceExplorerTreeView.SelectedValue is TableViewModel)
            {
                Table table = ((TableViewModel)DataSourceExplorerTreeView.SelectedValue).Table;
                DragDrop.DoDragDrop(DataSourceExplorerTreeView, table, DragDropEffects.Copy);
            }
        }
    }
SqlServerTable继承自表。如果我插入一个断点并调用

  e.Data.GetFormats()

我可以看到我的完全限定类型名

我已经能够使用反射解决这个问题:


我没有测试你的代码,但我认为问题在于装箱和拆箱。在MouseMove或DragDrop事件中,您似乎有错误的类型。若要接收SqlDataTable,则应发送SqlDataTable not Table,反之亦然。如果可以执行强制转换,则GetData()函数将返回null


请注意:使用反射检索私有成员不是一种好的做法。如果它们是私有的,这是有原因的。

如果您将其更改为“Object table=e.Data.GetData(typeof(SqlServerTable));”,您会得到什么吗?不幸的是,如果我将e.Data.GetData(typeof(SqlServerTable))粘贴到快速查看中,它总是为空,谁能解释为什么这对于如此明显的功能如此困难?为什么这些字段是非公共的?!?
        private void OnDragDrop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(typeof(SqlServerTable)))
        {
          FieldInfo info;

          object obj;

          info = e.Data.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);

          obj = info.GetValue(e.Data);

          info = obj.GetType().GetField("innerData", BindingFlags.NonPublic | BindingFlags.Instance);

         System.Windows.DataObject dataObj = info.GetValue(obj) as System.Windows.DataObject;

         SqlServerTable table = dataObj.GetData("Project.SqlServerTable") as SqlServerTable ;
        }
    }