在我的Xamarin Forms应用程序中,我使用DataTemplates和DataTemplateSelectors创建了一个包含不同单元格的ListView。对于一种类型的单元格,它上面有一个按钮,当它被点击时,导航到另一个页面,该页面只包含一个仅由字符串组成的ListView。
我的问题来自于在页面加载后调用的OnNavigatedTo方法,因此我传递给这个新的' ListView的列表只是'页面,在创建和填充ListView之后更新我将ListView绑定到的值,但它没有填充,因为数据来自上一页通过OnNavigatedTo方法。
public class ListPageViewModel : _ViewModelBase
{
private INavigationService _navigationService;
public ListPageViewModel(INavigationService navigationService)
{
_navigationService = navigationService;
_strings = new List<string>()
{
"Jaguar", "Elephant", "Nemo", "Cat", "Dog", "Iguana", "Puma", "Crow", "Hawk", "Owl", "Badger", "Meerket",
"Shark", "Whale", "Monkey", "Snake", "Lion", "Zebra", "Hippo", "Squirrel", "Rhino", "Bear", "Moose",
"Camel", "Chicken", "Cow", "Sheep", "Turkey", "Jellyfish", "Salmon", "Parrot", "Wolf", "Gecko"
// Placeholder before we have lists of customers
};
}
private IEnumerable<string> _strings;
public IEnumerable<string> Strings { get { return _strings; } set { SetProperty(ref _strings, value); } }
public override void OnNavigatedTo(NavigationParameters parameters)
{
base.OnNavigatedTo(parameters);
if (parameters.ContainsKey("dataList"))
{
_strings = (List<string>)parameters["dataList"];
}
}
}
因此,当我导航到此页面时,_strings
列表首先填充动物列表,然后页面加载显示ListView中的所有动物,然后在ListView加载后更改_strings
列表的值与随NavigationParameters一起传递的列表相同。那么每次更改或更新_strings
列表时,如何更新ListView?
答案 0 :(得分:1)
无论
Strings = (List<string>)parameters["dataList"];
或使用ObservableCollection