xamarin使用timer事件将变量绑定到标签

时间:2018-04-23 12:19:28

标签: xamarin binding

我希望将绑定变量添加到标签中,该标签作为网格中的子项。 变量用 计时器事件改变 .pls检查我的代码帮我找到了我错的地方。我可以看到标签init value 0 ,但在变量执行时不会更新。

public class DevicePage : ContentPage
{
    IDevice device;
    Label testLb = new Label();
    System.Timers.Timer  testtimer;
    Grid gridView;
    byte testt { get; set; } = 0;
    GetSendData communicate;
    private byte _maintext;  
    public byte MainText
    {
        get
        {
            return _maintext;
        }
        set
        {
            _maintext = value;
            OnPropertyChanged();
        }
    }
    public DevicePage(IDevice currDevice)
    {
        device = currDevice;
        this.Title = "Status";
        testtimer = new System.Timers.Timer();
        testtimer.Interval = 1000;
        testtimer.Elapsed += OnTimedEvent;
        testtimer.Enabled = true;

        gridView = new Grid();
        testLb.Text = testt.ToString();
        testLb.SetBinding(Label.TextProperty, ".");
        testLb.BindingContext = MainText;
        gridView.Children.Add(testLb, 0, 0);
        this.Content = gridView;
    }
    private void OnTimedEvent(object sender, System.Timers.ElapsedEventArgs e)
    {

        MainText += 1;
    }

1 个答案:

答案 0 :(得分:0)

实现INotifyPropertyChanged之后,testLb也不会更新。但是我更改了testLb.SetBinding(Label.TextProperty,“。”); testLb.BindingContext = MainText; to testLb.SetBinding(Label.TextProperty,“MainText”); testLb.BindingContext = this;它有效!