【问题标题】:Xamarin Forms Using Reflexion to get Property of Bindable PropertyXamarin Forms 使用反射获取可绑定属性的属性
【发布时间】:2020-09-28 06:40:39
【问题描述】:

我想要的是,当值改变时,它应该调用CreateChart()并使用新值。 我尝试通过反射调用 onPropertyChange 方法OnValueChanged 一个可绑定的属性,但该属性始终为空,我没有得到属性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),
                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;
        }
    }

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>

【问题讨论】:

    标签: xaml xamarin.forms reflection bindableproperty


    【解决方案1】:

    如果你想在值改变时得到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));
                    
    }
    

    【讨论】:

    • 非常感谢。可以这么简单=)
    猜你喜欢
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-20
    • 2017-07-27
    • 2015-01-06
    • 1970-01-01
    相关资源
    最近更新 更多