Xaml Xamarin使用反射形成可绑定属性的属性

Xaml Xamarin使用反射形成可绑定属性的属性,xaml,xamarin.forms,reflection,bindableproperty,Xaml,Xamarin.forms,Reflection,Bindableproperty,我想要的是,当值更改时,它应该调用CreateChart()并使用新值。 我尝试调用onPropertyChange方法OnValueChanged一个带有反射的可绑定属性,但该属性始终为null,并且我无法获取属性值的值 public partial class CorrectWrongRingChart : ContentView { public CorrectWrongRingChart() { InitializeComp

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

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),
                typeof(CorrectWrongRingChart),
                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;
        }
    }
public部分类更正错误ringchart:ContentView
{
公共环图()
{
初始化组件();
}
公共静态只读BindableProperty ChartProperty=
BindableProperty.Create(
姓名(图表),
类型(图表),
类型(环图);
公共海图
{
获取{return(Chart)GetValue(ChartProperty);}
set=>SetValue(ChartProperty,value);
}
公共静态只读BindableProperty ValueProperty=
BindableProperty.Create(
名称(值),
类型(值),
类型(校正环图),
propertyChanged:OnValueChanged);/*(b,o,n)=>{((CorrectErrorRingChart)b).OnPropertyChanged(“文本”);}*/
公共价值
{
获取{return(correctErrorValue)GetValue(ValueProperty);}
set=>SetValue(ValueProperty,value);
}
私有静态void OnValueChanged(BindableObject bindable、object oldValue、object newValue)
{
((更正错误的环图)可绑定)。OnPropertyChanged(“文本”);
//((CorrectErrorRingChart)可绑定)。OnPropertyChanged(“Correct”);
//((更正错误的环图)可绑定)。OnPropertyChanged(“错误”);
var valueProperty=valueProperty.GetType().GetProperty(“值”);
var value=(correctErrorValue)valueProperty.GetValue(“值”);
var ChartProperty=ValueProperty.GetType().GetProperty(“纠正错误图表”);
if(值!=null)
{
SetValue(“correctErrorChart”,CreateChart(value));
}
}
公共静态只读BindableProperty TextProperty=
BindableProperty.Create(
名称(文本),
类型(字符串),
类型(校正环图),
默认值:string.Empty);
公共字符串文本=>$“{Value?.CorrectCount±0}/{Value?.TotalCount±0}”;
//公共双精度=>Value.CorrectPercentage;
//public double Error=>Value.ErrorPercentage;
私有静态图表CreateChart(更正错误值)
{
var chart=new Microcharts.DonutChart();
chart.IsAnimated=false;
ChartEntry corretEntry=新的ChartEntry((浮动)值。CorrectPercentage)
{
Color=SKColor.Parse(“#00FF00”)
};
ChartEntry ErrorEntry=新的ChartEntry((浮动)值。错误百分比)
{
Color=SKColor.Parse(“#FF0000”)
};
chart.Entries=new List(){corretEntry,ErrorEntry};
收益表;
}
}
Xaml:


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

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;


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

if (value != null)
{

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

非常感谢你。它可以如此简单=)
if (value != null)
{

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