public class MyViewModel()
{
public ObservableCollection<int> Items { get; set; } = new ObservableCollection<int>();
}
private MyViewModel _viewModel = new MyViewModel();
public MainPage()
{
InitializeComponent();
BindingContext = _viewModel;
}
correctButton.Clicked += (sender, e) =>
{
App.DB.IncrementScore();
_viewModel.Items.Add(0);
};
<ContentPage.Resources>
<DataTemplate x:Key="ScoreItemTemplate">
<ViewCell>
<ViewCell.View>
<Label Text="{x:Static local:FontAwesome.FACheck}" FontFamily="FontAwesome" TextColor="Green"/>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ContentPage.Resources>
ListView
无法水平显示其项目。这意味着,您需要下载其中一个可用的HorizontalListView
Nuget包,或者使用内置(仅在Xamarin.Forms 2.3及以上版本中可用!)CarouselView
:<CarouselView ItemTemplate="{StaticResource ScoreItemTemplate}" ItemsSource="{Binding Items}"/>
private Label CreateScoreLabel()
{
return new Label {Text = FontAwesome.FACheck, TextColor = Color.Green, FontFamily = "FontAwesome"};
}
StackLayout
:<StackLayout Orientation="Horizontal" x:Name="LabelStack"/>
correctButton.Clicked += (sender, e) =>
{
App.DB.IncrementScore();
LabelStack.Children.Add(CreateScoreLabel());
};
<Label x:Name="ScoreLabel" FontFamily="FontAwesome" TextColor="Green"/>
public static class StringExtensions
{
public static string Repeat(this string input, int num)
{
return String.Concat(Enumerable.Repeat(input, num));
}
}
correctButton.Clicked += (sender, e) =>
{
App.DB.IncrementScore();
ScoreLabel.Text = FontAwesome.FACheck.Repeat(App.DB.Score); // replace parameter by whatever method allows you to access the current score number
};
StackLayout
或CarouselView
的需要,您可以使用Label
间距选项来放置检查。第三种方法应该被标记为答案,并相应地修改问题。 - BrewMate您可以通过编程方式添加控件。请记住,您需要在主线程上添加一个Label
,因为这是对UI的更改。将以下内容添加到您的Clicked
事件中:
correctButton.Clicked += (sender, e) =>
{
App.DB.IncrementScore();
Device.BeginInvokeOnMainThread(() => // On MainThread because it's a change in your UI
{
Label label = new Label();
label.Text = "{x:Static local:FontAwesome.FACheck}"; // Not sure if this is right syntax...
label.FontFamily = "FontAwesome";
label.TextColor = Color.Green;
stackPanel.Children.Add(label);
});
};
Grid
,您可以使用Control
的SetValue
方法设置Grid.Row
和Grid.Column
:label.SetValue(Grid.RowProperty, 1);
label.SetValue(Grid.RowProperty, 2);
ObservableCollection <string>
。 - Rob Lyndon