弹出窗口中的选项卡导航断开,WPF用户控件在默认AppDomain中的Winforms中托管

时间:2016-07-27 21:38:23

标签: c# wpf winforms appdomain maf

我有一个使用Popup的WPF用户控件。此控件是一个插件,可以加载到主AppDomain或单独的AppDomain中,并使用ElementHost以Winforms形式托管。当插件在主AppDomain中加载并打开弹出窗口时,弹出窗口的字段之间的选项卡会将焦点移动到弹出窗口父窗口的第一个控件。当它在新的AppDomain中加载时,选项卡行为按预期/期望的方式工作(它会循环显示弹出窗口中的控件)。

我在SO和其他地方已经阅读了许多相似但不完全相同的问题,但这些建议都没有帮助。

看来,标签消息是在AddInHost中处理的(来自我使用FrameworkElementAdapters来在域外情况下跨域边界编组WPF控件)。我的最终目标是将其实现为托管外接程序框架插件,但我已经完全放弃了简化复制的方法。

如果有更完整的上下文有帮助,我有a git repo of the simplified repro

如何才能使这种行为保持一致?

WpfUserControl.xaml

<UserControl x:Class="MyPlugin.WpfUserControl"
         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"
         mc:Ignorable="d" Background="White">
    <Grid Margin="5">
        <Grid.RowDefinitions>
            <RowDefinition Height="28" />
            <RowDefinition Height="28" />
            <RowDefinition Height="28" />
        </Grid.RowDefinitions>

        <TextBox Grid.Row="0" Margin="3" />

        <Button x:Name="DropDownButton" Grid.Row="1" Margin="3" HorizontalAlignment="Left" MinWidth="100" Content="Drop Down" Click="DropDownButton_OnClick" />
        <Popup Grid.Row="1" x:Name="Popup1" Placement="Right" StaysOpen="True" PlacementTarget="{Binding ElementName=DropDownButton}">
            <Border BorderBrush="Black" BorderThickness="1">
                <Grid Background="White">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                    </Grid.RowDefinitions>

                    <Label Content="Username:" Grid.Row="0" Grid.Column="0" Margin="3" />
                    <TextBox Grid.Row="0" Grid.Column="1" Margin="3" MinWidth="150" />

                    <Label Content="Password:" Grid.Row="1" Grid.Column="0" Margin="3" />
                    <TextBox Grid.Row="1" Grid.Column="1" Margin="3" MinWidth="150" />

                    <Button x:Name="SaveButton" Grid.Row="2" Grid.Column="1" Margin="3" HorizontalAlignment="Right"
                                            Content="Save" Click="SaveButton_OnClick" />
                </Grid>
            </Border>
        </Popup>

        <Button x:Name="DoSomethingButton" Grid.Row="2" Margin="3" HorizontalAlignment="Left" MinWidth="100" Content="Do Something" />
    </Grid>
</UserControl>

Plugin.cs

public class Plugin : MarshalByRefObject
{
    public INativeHandleContract CreateWpfUserControl()
    {
        return FrameworkElementAdapters.ViewToContractAdapter(new WpfUserControl());
    }
}

MainForm.cs(选定位)

private void LoadPlugin(bool loadInSameAppDomain)
{
    AppDomain appDomain;
    if (loadInSameAppDomain)
    {
        appDomain = AppDomain.CurrentDomain;
    }
    else
    {
        var appDomainName = Guid.NewGuid().ToString();
        _appDomain = AppDomain.CreateDomain(appDomainName, AppDomain.CurrentDomain.Evidence, new AppDomainSetup
        {
            ApplicationName = appDomainName,
            ApplicationBase = AppDomain.CurrentDomain.BaseDirectory,
            PrivateBinPath = AppDomain.CurrentDomain.BaseDirectory
        });
        appDomain = _appDomain;
    }

    _plugin = (Plugin)appDomain.CreateInstanceAndUnwrap("MyPlugin", "MyPlugin.Plugin");
}

private void loadPluginButton_Click(object sender, EventArgs e)
{
    LoadPlugin(appDomainCheckBox.Checked);

    var pluginControl = FrameworkElementAdapters.ContractToViewAdapter(_plugin.CreateWpfUserControl());
    elementHost1.Child = pluginControl;

    UpdateUi(true);
}

1 个答案:

答案 0 :(得分:0)

我受过一定程度的教育,尽管绝不是权威,但猜测是问题在于WinForms和WPF希望独占访问顶部窗口的消息泵。在自己的AppDomain中运行每个窗口,可以分别控制父窗口及其消息泵。