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;

}

When the app loads.

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)