【问题标题】:WPF DispatcherTimer on Modalless window无模式窗口上的 WPF DispatcherTimer
【发布时间】:2011-04-06 10:14:02
【问题描述】:

我有一个如下定义的 wpf 进度窗口:

<Window x:Class="NeoinfoXmlEditor.WPF.Forms.ProgressDisplayForm"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="84" Width="505" x:Name="root" WindowStartupLocation="CenterScreen">
    <Grid>
        <ProgressBar Height="15" x:Name="MessageProgessBar" HorizontalAlignment="Stretch" VerticalAlignment="Top" Maximum="10000" Margin="10,2,10,2" >
            <ProgressBar.Triggers>
                <EventTrigger RoutedEvent="ProgressBar.Loaded">
                    <BeginStoryboard>
                        <Storyboard x:Name="sb">
                            <DoubleAnimation Storyboard.TargetName="MessageProgessBar"
                                 Storyboard.TargetProperty="Value"
                                 From="0" To="10000" Duration="0:0:45"/>
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger>
            </ProgressBar.Triggers>
        </ProgressBar>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="5" Text="{Binding ElementName=root, Path=Message}" />
    </Grid>
</Window>

还有一个代码隐藏文件如下:

public partial class ProgressDisplayForm : Window
    {
        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof (string), typeof (ProgressDisplayForm));

        public string Message
        {
            get { return (string) GetValue(MessageProperty); }
            set { SetValue(MessageProperty, value); }
        }

        public ProgressDisplayForm()
        {
            InitializeComponent();
        }

        public void DisplayWindow()
        {
            this.Show();
            this.BeginStoryboard(sb);
        }

    }

你可以看到我尝试通过两种方式启动一个progressBar动画: - 在 ProgressBar.Loaded 上使用 EventTrigger - 从代码后面,明确地

问题是 - 两者都不起作用。

注意 - 我需要打开此窗口并将动画作为无模式窗口启动,因此 ShowDialog() 不是选项。另外,我尝试使用 DispatcherTimer,但不知何故它不起作用,在使用 System.Timers.Timer 类时 this.Dispatcher.Invoke() 也不行。

我正在从主应用程序窗口调用 DisplayWindow() 方法。

我错过了什么?

提前致谢

【问题讨论】:

  • 嗯。这对我来说可以。我将您的 XAML 粘贴到新的 WPF 项目中,并在启动后进行了动画处理。

标签: c# wpf wpf-controls window dispatcher


【解决方案1】:

我无法重现您的问题,您的 XAML 动画工作正常!请尝试将您的 XAML 代码复制到没有代码隐藏的新项目。我试过了,效果很好:D

【讨论】:

    【解决方案2】:

    我发现了问题所在 - 我调用了 NewWindow.Show(),然后继续进行一些高 CPU 计算,假设如果不使用 ShowDialog() 调用新窗口将位于单独的线程上。

    我使用 BackgroundWorker 修复了它!

    还是谢谢你的帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-14
      • 2011-10-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多