Xamarin中的Ivalue转换器属性

时间:2019-09-18 08:42:36

标签: xaml xamarin xamarin.forms

我有以下具有两个属性的转换器:

public class ProgressConverter : BindableObject, IMarkupExtension, IValueConverter 
    {
        public static readonly BindableProperty CurrentProgressProperty = BindableProperty.Create("CurrentProgress", typeof(int), typeof(ProgressConverter));
        public static readonly BindableProperty GoalProgressProperty = BindableProperty.Create("GoalProgress", typeof(int), typeof(ProgressConverter));
        public int CurrentProgress
        {
            get { return(int) GetValue(CurrentProgressProperty); }
            set { SetValue(CurrentProgressProperty, value);}
        } 
        public int GoalProgress
        {
            get { return (int)GetValue(GoalProgressProperty); }
            set { SetValue(GoalProgressProperty, value);}
        }
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            double progress = (double)CurrentProgress / (double)GoalProgress;
            return progress;
        }
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
        public object ProvideValue(IServiceProvider serviceProvider)
        {
            //throw new NotImplementedException();
            return this;
        }
        object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider)
        {
            return ProvideValue(serviceProvider);
        }
    }

我正在进度条内部使用它来设置进度(这没问题,没问题,将2并除以5,然后将结果放入进度条progress中):

<ProgressBar x:Name="Progressbar" ProgressColor="Purple" 
             Progress="{Binding Converter={local:ProgressConverter CurrentProgress=1,GoalProgress=5}}">
</ProgressBar>

但是,当我这样做时,它将无法正常工作(Point#1) :(转换器中CurrentProgress和GoalProgress的值均为0)。

<ProgressBar x:Name="Progressbar" ProgressColor="Purple" 
             Progress="{Binding Converter={local:ProgressConverter CurrentProgress={Binding xCurrentProgress},GoalProgress={Binding xGoalProgress}}}">
</ProgressBar>

xCurrentProgress和xGoalProgress都在父itemSource中设置。

型号:

public class badge {
string Name {get;set;}
string Description {get;set;}
int xCurrentProgress {get;set;}
int xGoalProgress {get;set;}
}

Itemsource设置:

public BadgeView(BadgesGroup badgesGroup)
        {
            InitializeComponent();
            BadgeLogo.Source = badgesGroup.Logo;
            var cardsview = new CardsView
            {
                IsClippedToBounds = true,
                IsCyclical = true,
                MoveWidthPercentage = 0.3,
                WidthRequest = 250,
                HeightRequest=250,
                ItemsSource = badgesGroup.Badges,
                ItemTemplate = new DataTemplate(() => new BadgePopUp())
            };
            cardsview.Children.Add(new IndicatorsControl());
            cardsview.Children.Add(new RightArrowControl());
            cardsview.Children.Add(new LeftArrowControl());
            card.Children.Add(cardsview);
        }

我如何使其适用于第1点

如何使转换器从绑定中获取值,如下所示:

<ProgressBar x:Name="Progressbar" ProgressColor="Purple" 
                 Progress="{Binding Converter={local:ProgressConverter CurrentProgress={Binding xCurrentProgress},GoalProgress={Binding xGoalProgress}}}">
    </ProgressBar>

因为使用上述代码将导致CurrentProgress和GoalProgress的值都传递0,而不是存储在其中的实际值。

0 个答案:

没有答案