Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在计时器计时时评估textblock绑定_C#_Silverlight_Windows Phone 7_Timer - Fatal编程技术网

C# 如何在计时器计时时评估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

如何从xaml pivot页面绑定usercontrol上textblock中的计时器计时计数。 我曾经从类中获取绑定值,更改的属性不起作用,它返回空值

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