【问题标题】:Windows 8 WPF progress bars don't have glow animationWindows 8 WPF 进度条没有发光动画
【发布时间】:2017-05-24 23:09:11
【问题描述】:

Windows 窗体应用程序中的进度条具有标准的“闪亮”动画,但是当我尝试在 WPF 中添加进度条时,默认情况下我没有得到这样的东西。我们如何在 Windows 8 中使用 WPF 恢复这一点?

Windows 窗体

WPF

【问题讨论】:

  • 注意:WPF 在这里是正确的。 Windows 8 消除了所有的光泽。
  • 您必须重写 ProgressBar 的 ControlTemplate 并自行提供任何动画。
  • Daniel,为什么 Windows 8 会有它? puu.sh/3Lk3W.png

标签: wpf windows-8 wpf-controls


【解决方案1】:

这是一个相当奇怪的修复,但您需要在应用程序中启用 Windows 窗体样式才能使用“光泽度”。以下是我的做法。

  1. 在您的项目中添加对System.Windows.Forms 的引用
  2. 进入项目设置页面,点击View Application Events
  3. 使用以下代码(在 VB.NET,C# 中类似)将处理程序添加到您的 Application.Startup(另外,如果您需要包含参数,请执行此操作)
Class Application

    ' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
    ' can be handled in this file.

    Private Sub Application_Startup() Handles Me.Startup
        System.Windows.Forms.Application.EnableVisualStyles()
    End Sub

End Class

您必须调用它才能使 WPF 进度条工作,这似乎很奇怪,但代码对我有用。

【讨论】:

    【解决方案2】:

    如果您想要最终控制外观(即指示器的颜色),我已经调整了我在另一篇 SO 帖子中找到的控件,它提供了几乎相同的显示。

    调整:

    <Grid Background="LightGray">
        <Grid Width="{Binding ProgressBarWidth, ElementName=uc}" HorizontalAlignment="Left">
            <Grid.Triggers>
                <EventTrigger RoutedEvent="Rectangle.Loaded">
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[1].(GradientStop.Offset)" From="-1" To="0" Duration="0:0:1.5" RepeatBehavior="Forever"/>
                            <DoubleAnimation Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[2].(GradientStop.Offset)" From="0" To="1" Duration="0:0:1.5" RepeatBehavior="Forever"/>
                            <DoubleAnimation Storyboard.TargetProperty="Background.(GradientBrush.GradientStops)[3].(GradientStop.Offset)" From="1" To="2" Duration="0:0:1.5" RepeatBehavior="Forever"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </Grid.Triggers>
            <Grid.Background>
                <LinearGradientBrush>
                    <GradientStop Color="#FF218ED6" Offset="0.0" />
                    <GradientStop Color="#FF4BA5E0" Offset="0.4" />
                    <GradientStop Color="#FF8ECCF5" Offset="0.5" />
                    <GradientStop Color="#FF4BA5E0" Offset="0.6" />
                    <GradientStop Color="#FF218ED6" Offset="1.0" />
                </LinearGradientBrush>
            </Grid.Background>
        </Grid>
    </Grid>
    

    全面实施:

    Progress bar style in WPF is old fashioned. Increments in Bars. How to implement a progress bar with vista or windows-7 shady glow effect?

    预览:

    【讨论】:

      【解决方案3】:

      Milliron X answer 没有帮助我,所以我不得不使用 Windows 窗体 ProgressBar 而不是使用自定义控件重新发明轮子。

      要实现这一点:

      1. 添加对WindowsFormsIntegrationSystem.Windows.Forms 程序集的引用。
      2. 将以下命名空间添加到Window 元素

        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        
      3. ProgressBar 添加到WindowsFormsHost 控件。例如

        <WindowsFormsHost Margin="0,10,0,0">
            <wf:ProgressBar x:Name="DownloadProgressBar" Width="500" Height="50" />
        </WindowsFormsHost>
        
      4. 为了让我们的ProgressBar 看起来不“老式”,您需要在入口点添加以下行(例如Main 方法或Application.OnStartUp 方法):

        System.Windows.Forms.Application.EnableVisualStyles();
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-11-15
        • 2010-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-23
        • 2014-04-23
        相关资源
        最近更新 更多