gpt4 book ai didi

c# - DispatcherTimer 不触发

转载 作者:行者123 更新时间:2023-11-30 15:27:34 25 4
gpt4 key购买 nike

我刚刚开始开发 Windows 应用商店应用程序,我只想要一个非常简单的应用程序:几乎是一个从左到右填满的进度条,但即使是这个任务对我来说显然也无法完成。

我有以下代码:

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 中执行相同的设置时,它工作得很好,但是当我启 Action 为 Windows 商店应用程序构建的代码时,Tick 事件永远不会触发。构建 Windows 应用商店应用程序时有什么特别需要注意的吗?我四处寻找,遗憾的是在这件事上一无所获。

最佳答案

您的代码运行良好。你只是没有等足够长的时间来注意到。 :)

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();
}

您正在设置 TimeSpan as 100 seconds .您需要使用五参数重载来获取毫秒数。但恕我直言,仅使用 FromMilliseconds() 方法(如上所述)更容易且更易读。

另外,您不需要两次创建 DispatcherTimer 对象,尤其是当您要完全忽略第一个对象时。 :)

关于c# - DispatcherTimer 不触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27413807/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com