【发布时间】:2021-11-26 16:14:02
【问题描述】:
所以我在做一个 MVVM 应用程序并且有一个问题:我需要在应用程序中动态创建一个界面。所以我用Collection创建了一个ViewModel。
public ObservableCollection<ViewCell> dataTemplate { get; set; }
并添加一个添加新元素的按钮
private void NewOther(object obj)
{
IsBusy = true;
try
{
ViewCell other = new ViewCell
{
View = new StackLayout
{
Children = { new Label { Text = "I m New" } }
}
};
dataTemplate.Add(other);
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
在 Xaml 中我有:
<RefreshView Padding="5" Command="{Binding LoadCheckListsCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
<ListView ItemsSource="{Binding dataTemplate}">
<ListView.Header>
<Grid ColumnDefinitions="*,*,*">
<Button Text="Add Measurment" Grid.Column="0" Command="{Binding AddMeasurment}"/>
<Button Text="Add Pattern" Grid.Column="1" Command="{Binding AddPattern}"/>
<Button Text="Add Other" Grid.Column="2" Command="{Binding AddOther}"/>
</Grid>
</ListView.Header>
<ListView.Footer>
<StackLayout Orientation="Horizontal" Margin="0,10,0,0">
<Button Text="Cancel" Command="{Binding CancelCommand}" HorizontalOptions="FillAndExpand"></Button>
<Button Text="Save" Command="{Binding SaveCommand}" HorizontalOptions="FillAndExpand"></Button>
</StackLayout>
</ListView.Footer>
</ListView>
</RefreshView>
所以,在我按下AddOther 之后,我有一个带有文本“Xamrin.Forms.ViewCell”的新元素,我认为这只是viewcell.ToString()。
这里如何绑定view cell的内容?
【问题讨论】:
-
是的,默认情况下 ListView 和 CollectionView 使用
ToString()。您可能必须在 XAML 中声明ListView.ItemTemplate。
标签: c# xaml xamarin xamarin.forms xamarin.android