使用SfGauge进行DataBinding

时间:2017-10-01 11:26:43

标签: c# xamarin binding syncfusion gauge

有人知道是否可以将数据绑定到SfGauge? 如果是这样,如何根据指针值,

NeedlePointer needlePointer = new NeedlePointer();
needlePointer.Value = 60;
needlePointer.Color = Color.Gray;
needlePointer.KnobColor = Color.FromHex("#2bbfb8");
needlePointer.Thickness = 5;
needlePointer.KnobRadius = 20;
needlePointer.LengthFactor = 0.8;
scale.Pointers.Add(needlePointer);

由于

这是我的更新代码,但仍然显示为零。

namespace Drip
{


    public partial class Guage : ContentPage
    {
        private const string Url = "https://thingspeak.com/channels/301726/field/1.json";
        private HttpClient _client = new HttpClient();
        private ObservableCollection<Feed> _data2;



        SfCircularGauge circular;
        NeedlePointer needlePointer;

        public Guage()
        {
          ...

            needlePointer = new NeedlePointer();

            needlePointer.Color = Color.Gray;
            needlePointer.KnobColor = Color.FromHex("#2bbfb8");
            needlePointer.Thickness = 5;
            needlePointer.KnobRadius = 20;
            needlePointer.LengthFactor = 0.8;






            scale.Pointers.Add(needlePointer);

            Content = circular;
        }


        protected override async void OnAppearing()
        {
            var content = await _client.GetStringAsync(Url);
            var data = JsonConvert.DeserializeObject<RootObject>(content);
            _data2 = new ObservableCollection<Feed>(data.Feeds);

            this.BindingContext = _data2[0];

            needlePointer.SetBinding(Pointer.ValueProperty, "Field1");




        }
    }

模型

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Drip
{
    public class Feed : INotifyPropertyChanged

    {
        private decimal _field1 = 0;
        public decimal Field1

        {
            get
            {
                return _field1;
            }
            set
            {
                if (_field1 != value)
                {
                    _field1 = value;
                    OnPropertyChanged();
                }


            }


        } 
        public DateTime Created_at { get; set; }
        public int Entry_id { get; set; }


        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged([CallerMemberName] string propertyField1 = null)
        {
            {
                var handler = PropertyChanged;
                if (handler != null)
                {
                    PropertyChangedEventArgs propertyChangedEvent
                    = new PropertyChangedEventArgs(propertyField1);
                    handler(this, propertyChangedEvent);
                }
            }
        }
    }
}

我已将上面的内容更改为我的代码,但是guage仍然显示零值,我错过了什么或者使用了什么错误?

2 个答案:

答案 0 :(得分:0)

假设您要绑定到BindingContext的属性“Value”

needlePointer.SetBinding(NeedlePointer.ValueProperty, "Value");

为了使数据绑定起作用,您需要设置BindingContext

this.BindingContext = _data2[0];

答案 1 :(得分:0)

我们从_data2[0] json得到的值是0。请参阅以下附带的json数据截图。

attached屏幕截图中,突出显示了集合中第0个索引的field1项值。
因此,如果用针指针绑定这些值,它将只显示0值。您能否确保从_data2[0]获得什么价值?如果您收到0,请从field1_data[1]获取_data[2]值。