这是我的Xamarin Forms应用程序中的代码,该应用程序应使用Activities
DetaledActivity
列表填充xaml ListView,该列表从上一页填充。当我单步执行并调试代码时,_activities
包含许多项目,但它们不会显示在xaml页面中。
如果我使用此页面的构造函数中的项目填充列表,那么我可以单击这些单元格,但不会显示任何文本。
public class DetailedActivityPageViewModel : _ViewModelBase
{
INavigationService _navigationService;
public DetailedActivityPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
}
public override void OnNavigatedTo(NavigationParameters parameters)
{
base.OnNavigatedTo(parameters);
if (parameters.ContainsKey("properties"))
{
var localAcs = new List<DetailedActivity>();
var properties = (Dictionary<string, object>)parameters["properties"];
foreach (var prop in properties)
{
if (prop.Value != null)
localAcs.Add(new DetailedActivity { Key = prop.Key.ToString(), Value = prop.Value.ToString() });
}
_activities = localAcs;
}
}
private IEnumerable<DetailedActivity> _activities;
public IEnumerable<DetailedActivity> Activities { get { return _activities; } set { SetProperty(ref _activities, value, "Activities"); } }
}
public class DetailedActivity
{
public string Key;
public string Value;
}
<ListView ItemsSource="{Binding Activities}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal">
<Label Text="{Binding Key}" />
<Label Text="{Binding Value}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
答案 0 :(得分:0)
我会尝试使用ObservableCollection
代替localAcs
代替List
。使用ObservableCollection
时,应在ObservableCollection
添加或删除元素时更新绑定。在添加元素之前,您可能需要将_activities
设置为`localAcs',例如:
public override void OnNavigatedTo(NavigationParameters parameters)
{
base.OnNavigatedTo(parameters);
if (parameters.ContainsKey("properties"))
{
var localAcs = new List<DetailedActivity>();
_activities = localAcs;
var properties = (Dictionary<string, object>)parameters["properties"];
foreach (var prop in properties)
{
if (prop.Value != null)
localAcs.Add(new DetailedActivity { Key = prop.Key.ToString(), Value = prop.Value.ToString() });
}
}
}