我的页面导航有问题,当我切换页面,回到我的MainPage时,我的计时器NiveauTimer不工作(我的TextBox中什么也没有出现),但是计时器DispatcherTimer工作得很好。
namespace BassinExpertV1
{
public sealed partial class MainPage : Page
{
PCF8591 ADConverter;
DispatcherTimer dispatcherTimer;
DispatcherTimer NiveauTimer;
public MainPage()
{
//Create Timer Date
dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += DispatcherTimer_Tick;
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
//Create Timer water level
NiveauTimer = new DispatcherTimer();
NiveauTimer.Tick += NiveauTimer_Tick;
NiveauTimer.Interval = new TimeSpan(0, 0, 1);
this.InitializeComponent();
}
private void Page_Loaded(object sender, RoutedEventArgs e)
{
dispatcherTimer.Start(); //Timer Date
NiveauTimer.Start(); //Timer water level
}
//----------------------------------------------------------------------------
// Timer Date
//----------------------------------------------------------------------------
private void DispatcherTimer_Tick(object sender, object e)
{
DateHeure.Text = DateTime.Now.ToString();
}
//----------------------------------------------------------------------------
// Timer WaterLevel
//----------------------------------------------------------------------------
private async void NiveauTimer_Tick(object sender, object e)
{
RecupNiveauAsync();
await System.Threading.Tasks.Task.Delay(2000); //wait for 2 seconds (= 2000ms)
try
{
double value = ADConverter.ReadI2CAnalog_AsDouble(PCF8591_AnalogPin.A0) *5 + 95; // Conversion du la valeur du potentiomètre
value = Math.Round(value, 2, MidpointRounding.AwayFromZero); //Arrondir la valeur
TextBoxNiveau.Text = Convert.ToString(value) + " %"; // Afficher dans la Textbox la valeur du potentiomètre en %
}
catch
{
MessageDialog msg = new MessageDialog("Probleme");
await msg.ShowAsync();
}
}
//----------------------------------------------------------------------------
// Water Level conversion //----------------------------------------------------------------------------
private async System.Threading.Tasks.Task RecupNiveauAsync()
{
ADConverter = await PCF8591.Create();
}
//----------------------------------------------------------------------------
// Bouton automatique
//----------------------------------------------------------------------------
private void Automatique_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Automatique), null); //navigation vers la page automatique
}
//----------------------------------------------------------------------------
// Bouton manuel
//----------------------------------------------------------------------------
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(Manuel)); //navigation vers la page manuel
}
private void TextBlock_SelectionChanged(object sender, RoutedEventArgs e)
{
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
}
}发布于 2018-02-25 08:56:14
您应该覆盖Page上的OnNavigatedTo和OnNavigatedFrom方法,而不是在构造函数中设置计时器。这样,您可以在离开页面时删除计时器,并在返回时重新设置计时器。
protected override void OnNavigatedTo(NavigationEventArgs e)
{https://stackoverflow.com/questions/48906637
复制相似问题