C# 如何在计时器计时时评估textblock绑定
如何从xaml pivot页面绑定usercontrol上textblock中的计时器计时计数。 我曾经从类中获取绑定值,更改的属性不起作用,它返回空值C# 如何在计时器计时时评估textblock绑定,c#,silverlight,windows-phone-7,timer,C#,Silverlight,Windows Phone 7,Timer,如何从xaml pivot页面绑定usercontrol上textblock中的计时器计时计数。 我曾经从类中获取绑定值,更改的属性不起作用,它返回空值 public class sample { int timercount = 0; public int Timercount { get { return timercount; } set { timercount = value; base.OnPr
public class sample
{
int timercount = 0;
public int Timercount
{
get
{
return timercount;
}
set
{
timercount = value;
base.OnPropertyChanged("Timercount");
}
}
}
public class ViewModelBaseEx : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
xaml:
<TextBlock Height="34" Name="txtTimer" Width="57" TextAlignment="Left" FontSize="20" TextWrapping="Wrap" Text="{Binding Timercount,Mode=TwoWay}" VerticalAlignment="Top" />
qplayer是一个usercontrol,TimerPageModel是一个继承ViewModelBaseEx的viewmodel
PropertyChanged正在工作,但usercontrol文本块中没有绑定任何内容
绑定在计时器上工作
public class TimerPageModel : ViewModelBaseEx
{
private DispatcherTimer timer = new DispatcherTimer();
private Random random = new Random();
static int tmrRnd = 30;
public TimerPageModel()
{
timer.Interval = new TimeSpan(0, 0, 5);
timer.Tick += OnTimer;
timer.Start();
}
}
int timercount = 0;
public int Timercount
{
get
{
return timercount;
}
set
{
timercount = value;
base.OnPropertyChanged("Timercount");
}
}
void OnTimer(object sender, EventArgs eventArguments)
{
//CurrentValue = random.Next(100);
Timercount = tmrRnd--;
}
你的代码有问题<代码>示例不继承任何内容,但您正在调用
base.OnPropertyChanged
。sample
是否应该从ViewModelBaseEx
继承?您还应该在设置DataContext
的地方发布。
public class TimerPageModel : ViewModelBaseEx
{
private DispatcherTimer timer = new DispatcherTimer();
private Random random = new Random();
static int tmrRnd = 30;
public TimerPageModel()
{
timer.Interval = new TimeSpan(0, 0, 5);
timer.Tick += OnTimer;
timer.Start();
}
}
int timercount = 0;
public int Timercount
{
get
{
return timercount;
}
set
{
timercount = value;
base.OnPropertyChanged("Timercount");
}
}
void OnTimer(object sender, EventArgs eventArguments)
{
//CurrentValue = random.Next(100);
Timercount = tmrRnd--;
}