Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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# WPF、Excel和上下文菜单_C#_Excel_Wpf_Vsto - Fatal编程技术网

C# WPF、Excel和上下文菜单

C# WPF、Excel和上下文菜单,c#,excel,wpf,vsto,C#,Excel,Wpf,Vsto,我实现了一个简单的WPF窗口,可以加载到VSTO Addin excel应用程序中 上下文男人闪现,即它被显示并突然消失;这仅在窗口不是模态时发生 重现这个问题很简单;首先,您必须创建一个Excel 200X VSX加载项 添加WPF用户控件,将根节点从UserControl更改为Window。一致地更改后面的代码,即将父类从UserControl替换为Window 这是创建WPF窗口的一个技巧,因为当您使用VSTO外接程序时,项目项之间没有WPF窗口。这也是问题最可能的原因。 该窗口仅包含一个

我实现了一个简单的WPF窗口,可以加载到VSTO Addin excel应用程序中

上下文男人闪现,即它被显示并突然消失;这仅在窗口不是模态时发生

重现这个问题很简单;首先,您必须创建一个Excel 200X VSX加载项

添加WPF用户控件,将根节点从UserControl更改为Window。一致地更改后面的代码,即将父类从UserControl替换为Window

这是创建WPF窗口的一个技巧,因为当您使用VSTO外接程序时,项目项之间没有WPF窗口。这也是问题最可能的原因。
该窗口仅包含一个带有上下文的标签

xaml:

<Window x:Class="ExcelAddIn9.MyWindow"
         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:ExcelAddIn9"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800">
<StackPanel>
    <Label>Some label
        <Label.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Context Menu"/>
            </ContextMenu>
        </Label.ContextMenu>
    
    </Label>
</StackPanel>
</Window>
这个闪烁。要使其工作,请将
w.Show()
更改为
w.ShowDialog()
。同样,这不是在实际应用程序中放置对话框的地方,因为它会阻止excel加载,但是为了显示问题,它是可以的

正如我所说,我认为这可能是他们没有为VSTO项目提供WPF窗口项的原因之一,但同样,我想深入研究这个问题,以便让ContextMenu在非对话应用程序中也能工作。
我想检查这个问题是否是VSTO中WPF窗口基本故障的症状,并评估其他源(可能将它们嵌入WinForms窗口)


VSTO版本2010。

必须指定父Outlook窗口句柄(
所有者
)。使用支持Windows演示基础(WPF)和Win32代码互操作的类。

例如,如果需要在Win32应用程序中宿主WPF对话框。使用对话框的WPF窗口对象初始化WindowInteropHelper。然后可以从handle属性获取WPF窗口的句柄(HWND),并使用owner属性指定WPF窗口的所有者。下面的代码示例演示如何在Win32应用程序中托管WPF对话框时使用WindowInteropHelper

WindowInteropHelper wih = new WindowInteropHelper(myDialog);
wih.Owner = ownerHwnd;
myDialog.Show();

这非常有趣,我尝试了以下方法:WindowInteropHelper wih=newwindowinterophelper(视图);wih.Owner=(IntPtr)Globals.ThisAddIn.Application.Hwnd;view.Show();没有运气。我注意到,当我第一次打开与WPF无关的WinForms窗口时,它可以正常工作。
WindowInteropHelper wih = new WindowInteropHelper(myDialog);
wih.Owner = ownerHwnd;
myDialog.Show();