Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.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
Visual studio ItemsSource加载大数据时的Xamarin.Forms块UI_Visual Studio_Api_Xaml_Xamarin.forms_Async Await - Fatal编程技术网

Visual studio ItemsSource加载大数据时的Xamarin.Forms块UI

Visual studio ItemsSource加载大数据时的Xamarin.Forms块UI,visual-studio,api,xaml,xamarin.forms,async-await,Visual Studio,Api,Xaml,Xamarin.forms,Async Await,我没有从API加载一个小数据,在C#代码中,它们是预先加载的,一切似乎都很好,但只要我打开ItemsSource=“{Binding BigData}”页面,我的UI就会被阻塞10秒钟。 有没有办法先打开页面,然后在不阻塞UI的情况下开始加载数据?我建议您可以在视图模型构造函数中启动加载数据的任务。使用Async和Wait加载投标数据 我做了一个示例,使用ListView显示100000条记录 <StackLayout> <Label Text="t

我没有从API加载一个小数据,在C#代码中,它们是预先加载的,一切似乎都很好,但只要我打开ItemsSource=“{Binding BigData}”页面,我的UI就会被阻塞10秒钟。
有没有办法先打开页面,然后在不阻塞UI的情况下开始加载数据?

我建议您可以在视图模型构造函数中启动加载数据的任务。使用Async和Wait加载投标数据

我做了一个示例,使用ListView显示100000条记录

 <StackLayout>
        <Label Text="test ui in xamarin.forms asyn" />
        <ActivityIndicator IsRunning="{Binding isBusy}" IsVisible="{Binding isBusy}" />
        <ListView x:Name="listview1" ItemsSource="{Binding Items}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Horizontal">
                            <Label Text="{Binding name}" />
                            <Label HorizontalOptions="CenterAndExpand" Text="{Binding age}" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

  public partial class Page19 : ContentPage
{      
    public Page19()
    {
        InitializeComponent();
    
        this.BindingContext = new ItemsViewModel();
    }    
}
public class ItemsViewModel:ViewModelBase
{
    private bool _isBusy;
    public bool isBusy
    {
        get { return _isBusy; }
        set
        {
            _isBusy = value;
            RaisePropertyChanged("isBusy");
        }
    }
    public ObservableCollection<people> Items { get; set; }
    public ItemsViewModel()
    {
        Items = new ObservableCollection<people>();
        isBusy = true;
        Task.Run(async () => await LoadItems());
    }

    public async Task LoadItems()
    {
       var items = new ObservableCollection<people>(); // new collection
        if (isBusy)
        {               
            await Task.Delay(10000);
            // var loadedItems = ItemsService.LoadItemsDirectory();
            //foreach (var item in loadedItems)
            //    items.Add(item);
            for (int i = 0; i < 100000; i++)
            {
                people p = new people();
                p.name = "people " + i;
                p.age = i;
                items.Add(p); // items are added to the new collection    
            }            
            Items = items; // swap the collection for the new one
            RaisePropertyChanged(nameof(Items));  // raise a property change in whatever way is right for your VM

            isBusy = false;
        }
    }
}
public class people
{
    public string name { get; set; }
    public int age { get; set; }
}

我想你可以使用
CollectionView
Group
对吧?我想你可以使用
paginedlist
不设置
ItemsSource
一次,而是每次设置10或20。当用户滚动显示更多时,您可以添加更多数据。我使用了ListView、StackLayout和带有大数据的列表。谢谢,我将尝试使用PaginatedList并在此处写入。我注意到一件事:当我通过Visual Studio运行应用程序时,UI挂起,没有它,一切都很好。它应该是这样的吗?在调试模式下运行它,然后它将挂起?没有调试和运行,它不会挂起吗?
 public class ViewModelBase : INotifyPropertyChanged
{       
    public event PropertyChangedEventHandler PropertyChanged;       
    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}