Pao*_*one 2 c# xaml observablecollection xamarin
对于我的xamarin.forms应用程序,我在尝试向可观察集合中添加元素时遇到了一些问题.这是一个代码示例:
public partial class ItemListPage : ContentPage
{
public ObservableCollection<string> Elements { get; set; } = new ObservableCollection<string>();
public ItemListPage()
{
InitializeComponent();
ElementsView.ItemsSource = Elements;
}
public async void OnAddButtonClicked()
{
string result = await GetCodeAsync();
if (result != null)
{
try
{
Elements.Add(result);
}catch(Exception exc)
{
throw exc;
}
}
}}`
Run Code Online (Sandbox Code Playgroud)
在这里,有一个我的Xaml代码示例:
<ListView x:Name="ElementsView">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="PlaceHolderText" />
</StackLayout>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)
从try catch部分,我得到一个InvalidCastException,但我不知道为什么!我错过了什么吗?应该是一个可以隐藏的集合的元素,实现一个特定的接口?非常感谢你!
DataTemplate元素的子元素必须是ViewCell类型或派生自ViewCell类型.
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="PlaceHolderText" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
408 次 |
| 最近记录: |