【发布时间】:2017-11-03 06:03:28
【问题描述】:
首先介绍一下背景知识,我有一个使用 Microsoft AddIn Framework (MAF) 的应用程序,它从插件获取 WPF UI(您可以按照this Microsoft example 创建一个)。在插件内容足够大以至于主表单需要滚动之前,这一切正常。发生这种情况时,它会滚动到应有的范围之外。
在图像中,当您向下滚动一些时,您会注意到Plugin Label Top 超过了Main Label Top,而在底部您只会看到Plugin Label Bottom。我的主表单代码如下:
<Window x:Class="WpfAddinTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAddinTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Background="DarkGray">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
<RowDefinition/>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Content="Main Label Top"/>
<ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto">
<ContentControl Name="PluginHolder"/>
</ScrollViewer>
<Label Grid.Row="2" Content="Main Label Bottom"/>
</Grid>
</Window>
ContentControl 是插件 UI 的所在,我不明白为什么插件 UI 会超过它的父控件。我尝试将它放在不同类型的控件中,例如DockPanel 和Grid,一切都以相同的方式运行。
有什么特殊的方法可以让它正常工作吗?
如果需要更多代码,我很乐意发布它,https://github.com/middas/WpfAddInTest 是我的完整示例项目,用于演示这一点。
编辑:在 WPF Inspector 中加载表单,我只能看到一个 AddInHost 控件,它不显示 ContentControl 中的任何单个控件。这有关系吗?
编辑 2: 在尝试任何我能想到的事情时,我在想可能是它在放置控件时没有得到正确的高度,所以我让插件返回了所需的高度并根据返回的内容手动设置ContentPlaceholder 的高度;没有运气。这是我尝试过的:
我将 AddIn 合同从 GetInt() 更新为 GetHeight() 并且在插件上我现在有了这个方法:
public double GetHeight()
{
_Control.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
return _Control.DesiredSize.Height;
}
然后在托管表格上,我现在有这个:
public MainWindow()
{
_PluginPath = System.IO.Path.Combine(Environment.CurrentDirectory, "Pipeline");
_AddInPath = System.IO.Path.Combine(_PluginPath, "AddIns");
InitializeComponent();
var warnings = new List<string>(AddInStore.Update(_PluginPath));
_PluginToken = AddInStore.FindAddIns(typeof(IPlugin), _PluginPath, _AddInPath).FirstOrDefault();
_Plugin = _PluginToken.Activate<IPlugin>(AddInSecurityLevel.FullTrust);
var control = _Plugin.GetControl();
PluginHolder.Height = _Plugin.GetHeight();
PluginHolder.Content = control;
}
编辑 3: 尝试强制 ZIndex 似乎也不会影响它。
Panel.SetZIndex(control, -1);
【问题讨论】:
-
尝试将第 1 行的高度设置为“自动”
-
将第 1 行的高度设置为“自动”使其根本不滚动,并且内容被推离屏幕而无法看到。如果我能够知道内容总是适合屏幕,这将不是问题。我处理多种分辨率和未知的插件 UI 大小,所以很遗憾,滚动是必需的。