如何解决Xamarin Forms MVVM iOS ListView不显示; SelectedItem有数据
我遵循了这个示例https://medium.com/swlh/xamarin-forms-mvvm-how-to-work-with-sqlite-db-c-xaml-26fcae303edd
我在OnRouteSelected事件处理程序中放置了一个断点,并且e.SelectedItem具有选定的对象,即使ListView不显示它也是如此。
我在下面缺少明显的东西吗?
这是我的代码: RoutesPage.xaml
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ContentPage.Content>
<ListView ItemsSource="{Binding Routes}" SelectedItem="{Binding SelectedRoute,Mode=TwoWay}" HasUnevenRows="False" SeparatorColor="Gray" ItemSelected="OnRouteSelected">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Label TextColor="Black" Text="{Binding ROName}"/>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage.Content>
</ContentPage>
RoutesPage.xaml.cs
public partial class RoutesPage : ContentPage
{
public RoutesPage()
{
InitializeComponent();
var routeStore = new RouteStore(DependencyService.Get<IsqliteDb>());
var pageService = new PageService();
viewmodel = new RoutesPageviewmodel(routeStore,pageService);
}
protected override void OnAppearing()
{
base.OnAppearing();
viewmodel.LoadDataCommand.Execute(null);
}
void OnRouteSelected(object sender,selecteditemchangedEventArgs e)
{
viewmodel.SelectRouteCommand.Execute(e.SelectedItem);
}
public RoutesPageviewmodel viewmodel
{
get { return BindingContext as RoutesPageviewmodel; }
set { BindingContext = value; }
}
}
RoutesPageviewmodel.cs LoadData()方法获取数据并将其成功添加到Routes集合中。
public class RoutesPageviewmodel : Baseviewmodel
{
private Routeviewmodel _selectedRoute;
private IRouteStore _routeStore;
private IPageService _pageService;
private bool _isDataLoaded;
public ObservableCollection<Routeviewmodel> Routes { get; private set; }
= new ObservableCollection<Routeviewmodel>();
public Routeviewmodel SelectedRoute
{
get { return _selectedRoute; }
set { SetValue(ref _selectedRoute,value); }
}
public ICommand LoadDataCommand { get; private set; }
public ICommand AddRouteCommand { get; private set; }
public ICommand SelectRouteCommand { get; private set; }
public ICommand DeleteRouteCommand { get; private set; }
public ICommand CallRouteCommand { get; private set; }
public RoutesPageviewmodel(IRouteStore routeStore,IPageService pageService)
{
_routeStore = routeStore;
_pageService = pageService;
LoadDataCommand = new Command(async () => await LoadData());
AddRouteCommand = new Command(async () => await AddRoute());
SelectRouteCommand = new Command<Routeviewmodel>(async c => await SelectRoute(c));
}
private async Task LoadData()
{
if (_isDataLoaded)
return;
_isDataLoaded = true;
var routes = await _routeStore.GetRoutesAsync();
foreach (var route in routes)
Routes.Add(new Routeviewmodel(route));
}
private async Task AddRoute()
{
// await _pageService.PushAsync(new RoutesDetailPage(new Routeviewmodel()));
}
private async Task SelectRoute(Routeviewmodel route)
{
if (route == null)
return;
SelectedRoute = null;
// await _pageService.PushAsync(new RoutesDetailPage(route));
}
}
viewmodels中的属性设置如下: 私有字符串_roName;
public string ROName
{
get { return _roName; }
set
{
SetValue(ref _roName,value);
OnPropertyChanged(nameof(ROName));
}
}
构造函数:
public Routeviewmodel(Route route)
{
//other properties
ROName = route.ROName;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。