Xamarin 计时器不更新
Xamarin Timer not updating
我正在制作一个健身应用程序,所以我需要 timer.I 在代码隐藏中完成此操作:
public partial class Timer : ContentView
{
private int seconds = 30;
private System.Timers.Timer timer;
public Timer()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(OneSecondPassed);
timer.Enabled = true;
}
private void OneSecondPassed(object source, System.Timers.ElapsedEventArgs e)
{
seconds--;
}
public string Time
{
get => seconds.ToString();
}
}
然后对于 UI,我制作了一个标签并将其文本 属性 绑定到我的时间 属性:
<Label BindingContext ="{x:Reference this}"
Text="{Binding Time}"/>
//"this" is a reference to my class
当我启动应用程序时,计时器保持 30。我知道“秒”肯定会减少,所以 binding.I 一定有问题知道我可以更新文本 属性 内的标签 OneSecondPassed ,但我想了解有关数据的更多信息 binding.Help?
正如杰森所说,实现INotifyPropertyChanged
接口是一个不错的选择。
为了大家更好的理解,我写了一个符合大家要求的可运行项目,供大家参考。
这是 xaml 代码:
<StackLayout>
<Label Text="{Binding DateTime}"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</StackLayout>
这里是cs代码:
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
int dateTime;
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
InitializeComponent();
this.DateTime = 30;
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
if (DateTime > 0)
{
DateTime--;
}
return true;
});
BindingContext = this;
}
public int DateTime
{
set
{
if (dateTime != value)
{
dateTime = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DateTime"));
}
}
}
get
{
return dateTime;
}
}
}
我正在制作一个健身应用程序,所以我需要 timer.I 在代码隐藏中完成此操作:
public partial class Timer : ContentView
{
private int seconds = 30;
private System.Timers.Timer timer;
public Timer()
{
InitializeComponent();
timer = new System.Timers.Timer();
timer.Interval = 1000;
timer.AutoReset = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(OneSecondPassed);
timer.Enabled = true;
}
private void OneSecondPassed(object source, System.Timers.ElapsedEventArgs e)
{
seconds--;
}
public string Time
{
get => seconds.ToString();
}
}
然后对于 UI,我制作了一个标签并将其文本 属性 绑定到我的时间 属性:
<Label BindingContext ="{x:Reference this}"
Text="{Binding Time}"/>
//"this" is a reference to my class
当我启动应用程序时,计时器保持 30。我知道“秒”肯定会减少,所以 binding.I 一定有问题知道我可以更新文本 属性 内的标签 OneSecondPassed ,但我想了解有关数据的更多信息 binding.Help?
正如杰森所说,实现INotifyPropertyChanged
接口是一个不错的选择。
为了大家更好的理解,我写了一个符合大家要求的可运行项目,供大家参考。
这是 xaml 代码:
<StackLayout>
<Label Text="{Binding DateTime}"
FontSize="Large"
HorizontalOptions="Center"
VerticalOptions="Center"/>
</StackLayout>
这里是cs代码:
public partial class MainPage : ContentPage, INotifyPropertyChanged
{
int dateTime;
public event PropertyChangedEventHandler PropertyChanged;
public MainPage()
{
InitializeComponent();
this.DateTime = 30;
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
if (DateTime > 0)
{
DateTime--;
}
return true;
});
BindingContext = this;
}
public int DateTime
{
set
{
if (dateTime != value)
{
dateTime = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("DateTime"));
}
}
}
get
{
return dateTime;
}
}
}