Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Listview在初始加载时不绑定,但在xamarin格式的RefreshCommand上工作_C#_Xamarin_Xamarin.forms - Fatal编程技术网

C# Listview在初始加载时不绑定,但在xamarin格式的RefreshCommand上工作

C# Listview在初始加载时不绑定,但在xamarin格式的RefreshCommand上工作,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,由于应用程序处于打开状态,我的ListView不会为客户端加载数据,但当我从顶部滑动调用Refresh命令时,它可以正常工作 ClientViewModel.cs public class ClientViewModel : BaseViewModel { private ObservableCollection<Client> _clients { get; set; } private IClientManager ClientManage

由于应用程序处于打开状态,我的ListView不会为客户端加载数据,但当我从顶部滑动调用Refresh命令时,它可以正常工作

ClientViewModel.cs

public class ClientViewModel : BaseViewModel
    {
        private ObservableCollection<Client> _clients { get; set; }
        private IClientManager ClientManager { get; }
        public ClientViewModel(INavigationManager navigationManager, IClientManager clientManager)
            :base(navigationManager)
        {
            Title = "Client";
            Clients = new ObservableCollection<Client>();
            ClientManager = clientManager;
        }

        public ObservableCollection<Client> Clients 
        {
            get => _clients;
            set
            {
                _clients = value;
                OnPropertyChanged();
            }
        }

        public Command LoadClientsCommand => new Command(async () => await ExecuteLoadClientsCommand());

        public Command SelectedItemCommand => new Command<Client>(OnClientSelected);

        //public Command AddClientCommand => new Command(() => OpenClientAddPage());

        private void OnClientSelected(Client obj)
        {
            NavigationManager.NavigateToAsync<ClientDetailViewModel>(obj);
        }

        public override async Task InitializeAsync(object data)
        {
            await ExecuteLoadClientsCommand();
        }

        private async Task ExecuteLoadClientsCommand()
        {
            if (IsBusy)
                return;

            IsBusy = true;

            try
            {
                Clients.Clear();
                Clients = (await ClientManager.GetAllClient()).ToObservableCollection();
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                IsBusy = false;
            }
        }
    }
公共类ClientViewModel:BaseViewModel { 私有可观测集合_客户端{get;set;} 专用IClientManager客户端管理器{get;} 公共客户端视图模型(INavigationManager导航管理器、IClientManager客户端管理器) :base(navigationManager) { Title=“客户”; Clients=新的observeCollection(); ClientManager=ClientManager; } 公共可观测收集客户端 { get=>\u客户端; 设置 { _客户=价值; OnPropertyChanged(); } } 公共命令LoadClientsCommand=>new命令(async()=>await ExecuteLoadClientsCommand()); public命令SelectedItemCommand=>new命令(OnClientSelected); //公共命令AddClientCommand=>new命令(()=>OpenClientAddPage()); 选定客户端时的私有无效(客户端对象) { NavigationManager.NavigateToAsync(obj); } 公共覆盖异步任务初始化同步(对象数据) { 等待ExecuteLoadClientsCommand(); } 专用异步任务ExecuteLoadClientsCommand() { 如果(忙) 返回; IsBusy=true; 尝试 { Clients.Clear(); Clients=(wait ClientManager.GetAllClient()).ToObservableCollection(); } 捕获(系统异常) { 投掷; } 最后 { IsBusy=false; } } } ClientPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:local="clr-namespace:Scheduler.Behaviours"
             xmlns:localconverter="clr-namespace:Scheduler.Converters"
             mc:Ignorable="d"
             xmlns:utility="clr-namespace:Scheduler.Utility;assembly=Scheduler"
                  utility:ViewModelLocator.AutoWireViewModel="True"
             Title="{Binding Title}"
             x:Class="Scheduler.Views.ClientPage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <localconverter:SelectedItemConverter x:Key="SelectedClient" />
        </ResourceDictionary>
    </ContentPage.Resources>

    <ContentPage.ToolbarItems>
        <ToolbarItem Text="Add"/>
    </ContentPage.ToolbarItems>

    <StackLayout>
        <ListView x:Name="ClientListView"
                  ItemsSource="{Binding Clients}"
                  VerticalOptions="FillAndExpand"
                  HasUnevenRows="true"
                  RefreshCommand="{Binding LoadClientsCommand}"
                  IsPullToRefreshEnabled="True"
                  IsRefreshing="{Binding IsBusy, Mode=OneWay}"
                  CachingStrategy="RecycleElement">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <!--<ViewCell.ContextActions>
                            <MenuItem Clicked="OnDelete_Clicked" Text="Delete" CommandParameter="{Binding .}"/>
                        </ViewCell.ContextActions>-->
                        <StackLayout Padding="10">
                                   <Label Text="{Binding FullName}"
                                   d:Text="{Binding .}"
                                   LineBreakMode="NoWrap"
                                   Style="{DynamicResource ListItemTextStyle}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.Behaviors>
                <local:EventToCommandBehaviour EventName="ItemSelected" 
                                               Command="{Binding SelectedItemCommand}" 
                                               Converter="{StaticResource SelectedClient}" />
            </ListView.Behaviors>
        </ListView>
    </StackLayout>
</ContentPage>

首先,在这里调用同一个API两次

Clients.Clear();
await ClientManager.GetAllClient();
Clients = (await ClientManager.GetAllClient()).ToObservableCollection();
它可以像

Clients.Clear();
Clients = (await ClientManager.GetAllClient()).ToObservableCollection();
第二,我看不到你也这么说
在这种情况下,数据从何而来?我建议您在上一页中进行上述API调用,并将其传递到此处

首先,在这里调用同一个API两次

Clients.Clear();
await ClientManager.GetAllClient();
Clients = (await ClientManager.GetAllClient()).ToObservableCollection();
它可以像

Clients.Clear();
Clients = (await ClientManager.GetAllClient()).ToObservableCollection();
第二,我看不到你也这么说 在这种情况下,数据从何而来?我建议您在上一页中进行上述API调用,并将其传递到此处