【发布时间】:2018-05-20 06:51:41
【问题描述】:
我有一个标签页,它的 ItemSource 与 viewmodel 绑定以动态生成标签。下面是xaml的代码sn-p
<pages:ScrollableTabbedPage
x:Class="Client_to_Kitchen.CategorizedMenuPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Client_to_Kitchen"
xmlns:pages="clr-namespace:Client_to_Kitchen.Views;assembly=Client_to_Kitchen"
Title="Menu"
ItemsSource="{Binding Menu}">
<TabbedPage.BindingContext>
<local:CategorizedMenuViewModel />
</TabbedPage.BindingContext>
<TabbedPage.ItemTemplate>
<DataTemplate>
<ContentPage Title="{Binding GroupName}" BackgroundColor="{StaticResource SecondaryColor}">
<ListView
x:Name="MyListView"
CachingStrategy="RecycleElement"
HasUnevenRows="True"
IsPullToRefreshEnabled="True"
IsRefreshing="{Binding IsLoading}"
ItemTapped="Handle_ItemTapped"
ItemsSource="{Binding MenuItems}"
RefreshCommand="{Binding FoodMenuListCommand}"
SeparatorColor="{StaticResource SecondaryLightColor}"
SeparatorVisibility="None">
选项卡会生成,甚至列表视图也会按预期填充。只有 IsRefreshing 和 RefreshCommand 的绑定不起作用。我做错了什么?
带有动态标签页的屏幕截图:
下面是viewmodel代码的sn-p
public class CategorizedMenuViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private bool _isLoading;
public bool IsLoading
{
get { return _isLoading; }
set
{
_isLoading = value;
OnPropertyChanged(nameof(IsLoading));
}
}
private ObservableCollection<MenuListResponse> _menu;
public ObservableCollection<MenuListResponse> Menu
{
get { return _menu; }
set
{
_menu = value;
OnPropertyChanged(nameof(Menu));
}
}
public CategorizedMenuViewModel()
{
FoodMenuListCommand.Execute(null);
}
private ICommand _foodMenuListCommand;
public ICommand FoodMenuListCommand => _foodMenuListCommand ?? (_foodMenuListCommand = new Command(async () => await GetMenuList()));
public async Task GetMenuList()
{
IsLoading = true;
int counter = 0;
string[] listImages = new string[4];
listImages[0] = "https://www.shareicon.net/data/512x512/2016/04/11/747949_animal_512x512.png";
listImages[1] = "https://www.shareicon.net/data/512x512/2016/08/01/805257_cinema_512x512.png";
listImages[2] = "https://www.shareicon.net/data/512x512/2016/08/19/817540_food_512x512.png";
listImages[3] = "https://www.shareicon.net/data/512x512/2016/08/01/805240_food_512x512.png";
try
{
MenuListStore _menuStoreObject = new MenuListStore();
List<MenuListResponse> response = await _menuStoreObject.GetFoodMenuList();
foreach (var items in response)
{
foreach (var item in items.MenuItems)
{
if (counter > 3)
counter = 0;
item.Image = listImages[counter];
counter++;
}
}
Menu = new ObservableCollection<MenuListResponse>(response);
}
catch (Exception ex)
{
await Application.Current.MainPage.DisplayAlert("Alert!", $"No internet", "Ok");
IsLoading = false;
return;
}
IsLoading = false;
}
请注意:- 在单独的选项卡式页面中,我没有将选项卡式页面的 ItemSource 和 ContentPage Title 绑定到视图模型以生成动态选项卡,而是有 2 个带有 listview 的静态内容页面,RefreshCommand 绑定可以正常工作。
【问题讨论】:
-
请贴出
GetMenuList方法的代码。我怀疑它会返回列表,而您没有将其设置在导致列表不更新的任何地方。 -
感谢@MartinZikmund 的考虑。我添加了 GetMenuList 方法的代码。我相信问题不在这里,因为从构造函数调用列表时会填充一次。我还调试了代码并且它没有在这里登陆,这意味着我没有正确定义刷新命令的绑定。
-
你有没有想过这个问题?我也有同样的问题
-
不,我没有。改变了我的流程对刷新页面的依赖:P
标签: c# listview xamarin.forms viewmodel tabbedpage