【发布时间】:2019-09-16 12:41:42
【问题描述】:
我有一个包含卡片视图的页面。
BadgeView.cs 的 C# 代码(BadgeView XAML 具有名为 card 的 stacklayout):
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);
}
卡片视图项目源设置为具有此模型的徽章对象列表:
public class badge {
string Name {get;set;}
string Description {get;set;}
int CurrentProgress {get;set;}
int GoalProgress {get;set;}
}
itemtemplate BadgePopUp 是:
<StackLayout
Orientation="Vertical"
HorizontalOptions="Center"
VerticalOptions="Start"
Padding="5"
BackgroundColor="Goldenrod">
<StackLayout.Resources>
<ResourceDictionary>
<local:ProgressConverter x:Key="xprogress" xCurrentProgress="{Binding CurrentProgress}"
xGoalProgress="{Binding GoalProgress}"/>
</ResourceDictionary>
</StackLayout.Resources>
<StackLayout Orientation="Vertical">
<Label Text="{Binding Title}"></Label>
<Label Text="{Binding Description}"></Label>
</StackLayout>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Name}"></Label>
<ProgressBar x:Name="Progressbar" ProgressColor="Purple"
Progress="{Binding Converter={StaticResource xprogress}}">
</ProgressBar>
<Label Text="{Binding Description}"></Label>
</StackLayout>
</StackLayout>
在 cardview 里面有两个标签,分别绑定了 Name 和 description 。 还有一个progressBar。
我想要做的是把 currentProgress 除以 GoalProgress 并将结果设置为 progressbar.progress 。
我尝试使用转换器,但它对我不起作用。 这是转换器的代码
public class ProgressConverter : IValueConverter
{
public int xCurrentProgress { get; set; }
public int xGoalProgress { get;set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (float) xCurrentProgress / xGoalProgress;
}
这对我有用:
<local:ProgressConverter x:Key="xprogress" xCurrentProgress="2"
xGoalProgress="3"/>
但是这行不通
<local:ProgressConverter x:Key="xprogress" xCurrentProgress="{Binding CurrentProgress}"
xGoalProgress="{Binding GoalProgress}"/>
出现以下错误:
No property, bindable property, or event found for 'xCurrentProgress', or mismatching type between value and property.
有什么建议吗?
【问题讨论】:
-
提供完整的代码或示例,以便人们更好地帮助您。
-
@LucasZhang-MSFT 谢谢,刚刚编辑并添加了示例代码。
标签: xaml xamarin mvvm progress-bar