【发布时间】:2019-05-29 14:38:26
【问题描述】:
我正在尝试在计时器事件中启动另一个视图模型。当我跳过这个计时器事件时,似乎启动器使用得很好。但是,如果启动是在活动期间完成的,则不会。谁能看看问题出在哪里?
using System.Timers;
using Caliburn.Micro.Xamarin.Forms;
using MyProject.Resources;
using System.Windows.Input;
using Xamarin.Forms;
using System.Diagnostics;
namespace MyProject.ViewModels
{
public class PairingDeviceViewModel : BaseScreen {
public INavigationService NavigationService { get; set; }
private Timer timer;
private const float MAX = 20;
private string percentage;
private float rate;
public float Rate { get => rate; set => Set(ref rate, value); }
public string Percentage { get => percentage; set => Set(ref percentage, value); }
protected override void OnInitialize()
{
base.OnInitialize();
timer = new Timer
{
Interval = MAX,
};
}
protected override void OnActivate()
{
base.OnActivate();
timer.Elapsed += Timer_Elapsed;
timer.Start();
}
protected override void OnDeactivate(bool close)
{
base.OnDeactivate(close);
timer.Elapsed -= Timer_Elapsed;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
Debug.WriteLine($"Timer_Elapsed");
Debug.WriteLine($"Rate {Rate}");
Rate++;
Percentage = string.Format(StringResources.PercentageNumber, Rate);
NotifyOfPropertyChange(() => Percentage);
if (Rate == MAX)
{
timer.Stop();
Debug.WriteLine($"navigation begins");
NavigationService.NavigateToViewModelAsync<ConfigurationListViewModel>();
}
Debug.WriteLine($"Timer_Elapsed END");
}
public ICommand SkipBluetoothScanning => new Command(() => {
timer.Stop();
NavigationService.NavigateToViewModelAsync<ConfigurationListViewModel>();
});
}
所以在我的Timer_elapsed 方法中,一旦Rate 等于MAX,我就会调用NavigationService.NavigateToViewModelAsync<>..。这个问题是这个条件Rate==MAX 被多次调用。
请查看日志文件。
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Timer_Elapsed
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Rate 0
Timer_Elapsed END
Timer_Elapsed
Rate 11
Timer_Elapsed END
Timer_Elapsed
Rate 12
Timer_Elapsed END
Timer_Elapsed
Rate 13
Timer_Elapsed END
Timer_Elapsed
Rate 14
Timer_Elapsed END
Timer_Elapsed
Rate 15
Timer_Elapsed END
Timer_Elapsed
Rate 16
Timer_Elapsed END
Timer_Elapsed
Timer_Elapsed END
Rate 17
Timer_Elapsed
Rate 18
Timer_Elapsed END
Timer_Elapsed
Rate 19
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
navigation begins
Timer_Elapsed END
[InputMethodManager] HSIFW - flag : 0 Pid : 6116
[InputMethodManager] HSIFW - flag : 0 Pid : 6116
Timer_Elapsed END
Timer_Elapsed END
Timer_Elapsed END
Timer_Elapsed END
Timer_Elapsed END
Timer_Elapsed END
[InputMethodManager] HSIFW - flag : 0 Pid : 6116
[InputMethodManager] HSIFW - flag : 0 Pid : 6116
Timer_Elapsed END
Timer_Elapsed END
[InputMethodManager] HSIFW - flag : 0 Pid : 6116
Timer_Elapsed END
Timer_Elapsed END
[InputMethodManager] HSIFW - flag : 0 Pid : 6116
Timer_Elapsed END
DispatcherTimer 是针对此功能或其他功能的更好解决方案吗?
非常感谢。
【问题讨论】:
-
首先,将浮点数与“==”进行比较是有风险的。如果要增加 1,最好使用整数。还有,timer的默认间隔是100ms,这真的是你需要的粒度吗?
-
我可以增加 timer.interval。对我来说应该没问题。由于 Timer.Interval 是一个浮点数,这也是我使用 Rate 作为浮点数的原因。我遇到的问题是条件 `if(Rate == Max) 被多次调用。
-
谢谢杰森。这是不调用
NavigateToViewModelAsync的唯一可能原因吗? -
没有。您可能需要使用 BeginInvokeOnMainThread()
标签: c# visual-studio xamarin xamarin.forms caliburn.micro