我创建了示例Wpf应用程序并安装了Extended WPF Toolkit(NuGet包)。这是我显示BusyIndicator的xaml代码。
<Window x:Class="WpfApp3.Progress"
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:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
xmlns:local="clr-namespace:WpfApp3"
mc:Ignorable="d"
WindowStyle="None"
BorderThickness="0"
Title=""
VerticalAlignment="Center"
HorizontalAlignment="Center"
SizeToContent="WidthAndHeight"
d:DesignWidth="300"
d:DesignHeight="300">
<xctk:BusyIndicator IsBusy="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"></xctk:BusyIndicator>
</Window>
使用以下代码触发显示进度窗口:
private void Button_Click(object sender, RoutedEventArgs e)
{
Progress w = new Progress
{
Owner = Application.Current.MainWindow,
WindowStartupLocation = WindowStartupLocation.CenterOwner
};
w.Show();
}
我的问题很简单。如何在MainWindow屏幕中间显示BusyIndicator。如下图所示,它不应该居中。请注意,我使用SizeToContent="WidthAndHeight"
有一个单独的窗口来显示繁忙的指示器,并将导致不必要的行为。如果原始窗口最大化,移动等会发生什么?
考虑将忙碌指示符添加到主屏幕。通常我会创建一个覆盖区域,用于显示消息对话框,进度条等。
<Window>
<Grid>
<Application stuff ....>
</Application stuff>
<ContentControl regions:RegionManager.RegionName="OverlayRegion"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" />
</Grid>
</Window>
我在这里使用Prism但你可以用任何东西替换ContentControl,例如BusyIndicator并操纵控件的可见性。
通过删除Window.SizeToContent
属性并将VerticalAlignment
和HorizontalAlignment
属性添加到BusyIndicator来解决它(实际上现在我使用了微调器,但这在解决方案中没有任何区别)。
<Window x:Class="Test.Progress"
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:extToolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit/extended"
WindowStyle="None"
AllowsTransparency="True"
BorderThickness="0"
Title=""
WindowStartupLocation="CenterOwner"
ShowInTaskbar="False"
Background="Transparent"
d:DesignWidth="200"
d:DesignHeight="200"
MaxWidth="250"
MaxHeight="250">
<xctk:BusyIndicator IsBusy="True"
HorizontalAlignment="Center"
VerticalAlignment="Center"></xctk:BusyIndicator>
</Window>
问题是因为BusyIndicator
was not designed to use it in separate window
BusyIndicator是一个ContentControl。这意味着BusyIndicator可以在其中包含单个子元素的开放和结束标记。
此外,如果你这样做,首先显示的是小的ui控制(12x12像素),并且在一些延迟之后最终显示进度条/忙碌指示符。
通过指定SizeToContent='WidthAndHeight'
,xaml会自动调整相对于内容的高度和宽度。在这种情况下(12×12)进行ui控制,并且在如上所述的最终延迟时间之后,显示忙指示符。但到那时,xaml ui渲染器已经应用了SizeToContent
,因此你必须手动重新定位进度条。
我不确定xaml ui renderes如何在没有指定SizeToContent
的情况下工作,但显然它在显示后正确地重新定位繁忙的指示器。