DispatcherTimer does not fire

I just got started with the Windows Store App and I just needed a very simple application: A fairly multi-program bar that fills in from left to right, but even that task doesn't seem to be the case for me.

I have the following code:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace TimeLoader
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private DispatcherTimer refreshTimer;

        public MainPage()
        {
            this.InitializeComponent();
        }

        void refreshTimer_Tick(object sender, object e)
        {
            TimePassedBar.Value += 5;
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            TimePassedBar.Value = 50;
            new DispatcherTimer();
            this.refreshTimer = new DispatcherTimer();
            this.refreshTimer.Interval = new TimeSpan(0, 0, 0, 100);
            this.refreshTimer.Tick += refreshTimer_Tick;
            this.refreshTimer.Start();
        }
    }
}

<Page
    x:Class="TimeLoader.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TimeLoader"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" Loaded="Page_Loaded">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ProgressBar Grid.Row="0" Grid.Column="0" Height="150" Value="75" VerticalAlignment="Center" Name="TimePassedBar"/>
    </Grid>
</Page>

      

Now the same setup works very well when I do it in WPF, but the Tick event never fires when I run this code generated as a Windows store app. Is there something special that I need to pay? Attention when building Windows Store apps? I looked high and low and unfortunately couldn't find anything about it.

+3


source to share


1 answer


Your code worked fine. You just didn't wait long enough to notice. :)

private void Page_Loaded(object sender, RoutedEventArgs e)
{
    TimePassedBar.Value = 50;
    this.refreshTimer = new DispatcherTimer();
    this.refreshTimer.Interval = TimeSpan.FromMilliseconds(100);
    this.refreshTimer.Tick += refreshTimer_Tick;
    this.refreshTimer.Start();
}

      



You set TimeSpan

as 100 seconds . To get milliseconds you need to use a five parameter overload. But IMHO it is easier and clearer to just use the method FromMilliseconds()

(as stated above).

Plus, you don't have to create the object twice DispatcherTimer

, especially if you ignore the first completely. :)

+4


source







All Articles