我希望打印一个字符串数组,每个字符串之间都有一个分隔符 示例代码块
string[] array;
string[] assignment = {"world","testing","array","hello"};
for(int i =0 ; i>assignment.Length; i++)
{
array[i] = assignment[i];
}
XAML
<controls:TappableCustomFontLabel
x:Name="array"
Text="{Binding array}"
XAlign="Start"
LineHeight="2.1"/>
XAML.CS
array.fillX();
我想在单独的标签上显示数组中的每个字符串,然后是分隔符 即 世界
测试
数组
您好
答案 0 :(得分:0)
使用 ListView
或 CollectionView
并将您的 ItemsSource
设置为 string[]
数组。您可以在 View 或 ViewModel 中定义 string[]
数组。建议在ViewModel中定义数组。
请注意,View.cs(代码隐藏) 中较大的数组、列表或集合存在性能问题。 View 中定义的较大集合会使滚动 ListView
或 CollectionView
粗糙且不均匀。因此,滚动稍大的 ListView
或 CollectionView
时滚动滞后。因此,在 ViewModel 中定义数组、列表和集合。
如果您只使用 View.cs
public string[] Fruits
{
get { return new string[] { "Mango", "Apple", "Pineapple", "Grapes", "Orange" }; }
}
public ListDemoPage // Page Constructor
{
InitializeComponent();
BindingContext = this;
}
或者如果您使用的是 ViewModel:(如果您不使用 ViewModel,请跳过此部分)
public class ListDemoPageViewModel : INotifyPropertyChanged
{
public string[] Fruits
{
get { return fruits; }
set { fruits = value; OnPropertyChanged(nameof(Fruits)); }
}
string[] fruits;
public ListDemoPageViewModel() // ViewModel Constructor
{
Fruits = new string[] { "Mango", "Apple", "Pineapple", "Grapes", "Orange" }; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
仅当您使用 ViewModel 时,将绑定上下文设置为 View.cs 中 ViewModel 类的实例:(如果您不使用 ViewModel,请跳过此步骤)
ListDemoPageViewModel listDemoPageViewModel = new ListDemoPageViewModel();
public ListDemoPage // Page Constructor
{
InitializeComponent();
// set your Binding Context to instance of ViewModel class
BindingContext = listDemoPageViewModel;
}
最后在您的 XAML 中
<ListView ItemsSource="{Binding Fruits}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding .}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- OR You can use CollectionView -->
<CollectionView ItemsSource="{Binding Fruits}">
<CollectionView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Frame BorderColor="Gray" Margin="5">
<Label Text="{Binding .}" />
</Frame>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>