【问题标题】:How to center BusyIndicator to the center of MainWindow?如何将 BusyIndi​​cator 居中到 MainWindow 的中心?
【发布时间】:2019-03-05 09:51:50
【问题描述】:

我创建了示例 Wpf 应用程序并安装了扩展 WPF 工具包(NuGet 包)。这是我用于显示 BusyIndi​​cator 的 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 屏幕中间显示 BusyIndi​​cator。如下图所示,它没有像应有的那样居中。注意我使用SizeToContent="WidthAndHeight"

【问题讨论】:

  • 这是一个基本的东西,而不是试图弄清楚如何使用它为什么不自己创建它?
  • 自己创造。微调控件?如果是这么基本的事情,你能提供一个基本的答案吗
  • extToolkit:BusyIndi​​cator 似乎只是一个带有文本框和进度条的边框。如果您自己创建它,您就可以完全控制它。但是,当使用外部工具包时,您需要依赖它是正确的,命名要标准化等。在我看来,为复杂的事情获取外部工具以节省时间是有道理的,但如果您只想显示一个进度条您应该依靠并提高您的个人技能。可能是个人意见...
  • @DenisSchaf 我同意,但如果你有时间的话,就像你提到的那样。另一件事是 BusyIndi​​cator 用于整个应用程序。尽管通过使用 xaml 尝试不同的东西,您可以获得 Xaml 行为方式的一些基本知识。虽然关于渲染的一些事情对我来说现在和将来都是一个谜:)

标签: wpf wpftoolkit


【解决方案1】:

有一个单独的窗口来显示忙碌指示器,这会导致不必要的行为。如果原始窗口最大化、移动等会发生什么?

考虑将忙碌指示器添加到主屏幕。通常我会创建一个覆盖区域,用于显示消息对话框、进度条等。

<Window>
<Grid>
<Application stuff ....>
</Application stuff>
  <ContentControl regions:RegionManager.RegionName="OverlayRegion"
                        HorizontalAlignment="Stretch"
                        VerticalAlignment="Stretch" />
    </Grid>
</Window>

我在这里使用 Prism,但您可以将 ContentControl 替换为任何东西,例如 BusyIndi​​cator 并操纵控件的可见性。

【讨论】:

    【解决方案2】:

    通过删除Window.SizeToContent 属性并将VerticalAlignmentHorizontalAlignment 属性添加到BusyIndi​​cator 来解决它(实际上现在我使用了微调器,但这对解决方案没有任何影响)。

    <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>
    

    问题是因为BusyIndicatorwas not designed to use it in separate window

    BusyIndi​​cator 是一个 ContentControl。这意味着 BusyIndi​​cator 可以在其打开和关闭标记中包含单个子元素。

    此外,如果您这样做,首先显示的是小型 ui 控件(12x12 像素),经过一些延迟后,最后会显示进度条/忙碌指示器。

    通过指定SizeToContent='WidthAndHeight',xaml 会自动调整相对于内容的高度和宽度。在这种情况下,采用 (12x12) ui 控制,并在经过一些延迟时间后,最终显示上述繁忙指示器。但到那时,xaml ui 渲染器已经应用了SizeToContent,因此您必须手动重新定位进度条。

    如果不指定 SizeToContent,我不太确定 xaml ui 渲染是如何工作的,但显然它会在显示繁忙指示器后正确重新定位它。

    【讨论】:

      猜你喜欢
      • 2011-11-16
      • 2011-10-06
      • 1970-01-01
      • 2012-01-07
      • 2012-07-10
      • 2012-09-05
      • 1970-01-01
      • 1970-01-01
      • 2011-06-25
      相关资源
      最近更新 更多