微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Xamarin使用反射形式来获得可绑定属性的属性

如何解决Xamarin使用反射形式来获得可绑定属性的属性

我想要的是,当值更改时,它应该调用CreateChart()并使用新值。 我尝试通过反射调用onPropertyChange方法OnValueChanged的可绑定属性,但是该属性始终为null,并且我没有获得属性Value的值

public partial class CorrectWrongRingChart : ContentView
    {
        public CorrectWrongRingChart()
        {
            InitializeComponent();
            
            
        }
        
        public static readonly BindableProperty ChartProperty =
            BindableProperty.Create(
                nameof(CorrectWrongChart),typeof(Chart),typeof(CorrectWrongRingChart));

        public Chart CorrectWrongChart
        {
            get { return (Chart)GetValue(ChartProperty); }
            set => SetValue(ChartProperty,value);
        }

        public static readonly BindableProperty ValueProperty =
          BindableProperty.Create(
              nameof(Value),typeof(CorrectWrongValue),typeof(CorrectWrongRingChart),propertyChanged: OnValueChanged);/*(b,o,n) => { ((CorrectWrongRingChart)b).OnPropertyChanged("Text");});*/

        public  CorrectWrongValue Value
        {
            get { return (CorrectWrongValue)GetValue(ValueProperty); }
            set => SetValue(ValueProperty,value);
        }

        private static void OnValueChanged(BindableObject bindable,object oldValue,object newValue)
        {
             ((CorrectWrongRingChart)bindable).OnPropertyChanged("Text");            
             //((CorrectWrongRingChart)bindable).OnPropertyChanged("Correct"); 
             //((CorrectWrongRingChart)bindable).OnPropertyChanged("Wrong");
            var valueProperty = ValueProperty.GetType().GetProperty("Value");
            var value = (CorrectWrongValue)valueProperty.GetValue("Value");
            var ChartProperty = ValueProperty.GetType().GetProperty("CorrectWrongChart");
            
            if (value != null)
            {

                ChartProperty.SetValue("CorrectWrongChart",CreateChart(value));
                
            }
            
            

        }

        public static readonly BindableProperty TextProperty =
            BindableProperty.Create(
                nameof(Text),typeof(string),defaultValue: string.Empty);

        public string Text => $"{Value?.CorrectCount ?? 0}/{Value?.TotalCount ?? 0}";
        //public double Correct => Value.CorrectPercentage;
        //public  double Wrong => Value.WrongPercentage;


        private static Chart CreateChart(CorrectWrongValue value)
        {
            var chart = new microcharts.DonutChart();
            chart.IsAnimated = false;
            
            ChartEntry corretEntry = new ChartEntry((float)value.CorrectPercentage)
            {
                Color = SKColor.Parse("#00FF00")
            };
            ChartEntry wrongEntry = new ChartEntry((float)value.WrongPercentage)
            {
                Color = SKColor.Parse("#FF0000")
            };
            chart.Entries = new List<ChartEntry>() { corretEntry,wrongEntry };
            return chart;
        }
    }

Xaml:

<Grid >
            <forms:ChartView x:Name="chart1" WidthRequest="130" HeightRequest="130" Chart="{Binding CorrectWrongChart,Source={x:Reference Root}}">
               
</forms:ChartView>
            <Label Text="{ Binding Text,Source={x:Reference Root} }"                  
                   VerticalOptions="Center"
                   HorizontalOptions="Fill"
                   HorizontalTextAlignment="Center"
                   TextColor="Black"
                   FontSize="19"
                   FontFamily="{ StaticResource AppBoldFontFamily }" />
        </Grid>

解决方法

如果您想在更改值时获取Value,则可以像下面这样直接获取

private static void OnValueChanged(BindableObject bindable,object oldValue,object newValue)
{
    var currentValue = newValue;
   
   // do something you want 
   
    // you could get other property like following 
    // var view = bindable as CorrectWrongRingChart;
    // var currentText = view.Text;


}

属性Value会在更改source的值时自动更改。因此,我们不再需要调用以下行,这可能会导致无限循环。

if (value != null)
{

    ChartProperty.SetValue("CorrectWrongChart",CreateChart(value));
                
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?