在Outlook VSTO加载项下显示WPF窗口的对话框

时间:2015-11-30 08:22:00

标签: c# wpf outlook vsto outlook-addin

我创建了Outlook VSTO应用程序。我想在单击按钮时弹出WPF窗口对话框。这是我的WPF窗口:

<Window x:Class="WorkflowSR.View.ArchiveSettingWindow"
             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:WorkflowSR.View"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <CheckBox x:Name="checkBox" Content="CheckBox" HorizontalAlignment="Left" Margin="111,73,0,0" VerticalAlignment="Top" Width="100"/>
    </Grid>
</Window>

在代码中,当我想打开对话框时,我喜欢这样:

var archiveSettingWindow = new ArchiveSettingWindow();
archiveSettingWindow.owner = ???
archiveSettingWindow.ShowDialog();

我应该为窗口所有者设置什么?谢谢。

1 个答案:

答案 0 :(得分:3)

使用IOleWindow和WindowInteropHelper:

        using System.Runtime.InteropServices;
        ...
        IntPtr wnd = new IntPtr(0);         
        object window = _application.ActiveWindow();
        if (window != null)
        {
            IOleWindow oleWindow = window as IOleWindow;
            if (oleWindow != null)
            {
                oleWindow.GetWindow(out wnd);
            }
        }
        ...

        if (wnd != IntPtr.Zero)
        {
            WindowInteropHelper helper = new WindowInteropHelper(archiveSettingWindow);
            helper.Owner = wnd;
            archiveSettingWindow.ShowInTaskbar = false;
        }