其他分享
首页 > 其他分享> > 标签:DispatcherTimer不会触发

标签:DispatcherTimer不会触发

作者:互联网

我刚开始从事Windows Store App开发,我只想要一个非常简单的应用程序:几乎是一个从左到右填充的进度条,但是即使对我来说,这项任务显然也不是可行的.

我有以下代码:

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>

现在,当我在WPF中执行该安装程序时,该安装程序就可以正常运行,但是当我启动作为Windows应用商店应用程序构建的代码时,Tick事件就永远不会触发.构建Windows Store应用程序时,我需要注意哪些特别事项?我看上去高低不平,可悲的是在这件事上什么都没找到.

解决方法:

您的代码运行正常.您只是没有等很久才注意到.

标签:windows-store-apps,c,net
来源: https://codeday.me/bug/20191028/1955735.html