Xamarin选项卡式应用模板:微调器不会停止,异步任务无效

问题描述 投票:0回答:1

UPDATE:在Android设备或Android模拟器上不会发生;这是iOS设备和iOS模拟器的特定错误。

我正在Mac上开箱即用地运行模板。要测试自己,您只需转到Visual Studio:

  1. 文件
  2. 新解决方案
  3. 多平台:应用
  4. 标签表格应用

在iPhone设备或iPhone模拟器上运行时,列表顶部的微调器不会停止旋转。我怀疑这是由于共享表单项目中的此代码所致:

public class ItemsViewModel : BaseViewModel
    {
        public ObservableCollection<Item> Items { get; set; }
        public Command LoadItemsCommand { get; set; }

        public ItemsViewModel()
        {
            Title = "Browse";
            Items = new ObservableCollection<Item>();
            LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());

            MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
            {
                var newItem = item as Item;
                Items.Add(newItem);
                await DataStore.AddItemAsync(newItem);
            });
        }

        async Task ExecuteLoadItemsCommand()
        {
            if (IsBusy)
                return;

            IsBusy = true;

            try
            {
                Items.Clear();
                var items = await DataStore.GetItemsAsync(true);
                foreach (var item in items)
                {
                    Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            finally
            {
                IsBusy = false;
            }
        }
    }

具体来说,LoadItemsCommand = new Command(async () => await ExecuteLoadItemCommand())与方法签名一起将其视为async Task<void>。>

尽管我将Tasks调用为void是很大的no-no

,因为除其他原因之外,CLR状态机无法对任务的结果进行任何处理?另外,此任务更改的任何事情(由IsBusy代表)正在监视,这些委托是否会受到影响?

来自BaseViewModel.cs

public class BaseViewModel : INotifyPropertyChanged
{
    public IDataStore<Item> DataStore => DependencyService.Get<IDataStore<Item>>();

    bool isBusy = false;
    public bool IsBusy
    {
        get { return isBusy; }
        set { SetProperty(ref isBusy, value); }
    }
 ...
}

从视图中,特别是微调器控件ItemsPage.xaml

IsRefreshing="{Binding IsBusy, Mode=OneWay}"

MacOS:

10.15.2,Visual Studio for Mac: 8.3.10(内部版本2),Xcode: 11.3(11C29),iOS模拟器: 11.3(SimulatorApp-912.5 SimulatorKit-570.3 CoreSimulator-681.17)

更新:这不会在Android设备或Android模拟器上发生;这是iOS设备和iOS模拟器的特定错误。我在Mac上开箱即用地运行模板。为了测试自己,您...

c# xamarin asynchronous task void
1个回答
0
投票

在Forms和iOS项目中更新以下内容之后,

© www.soinside.com 2019 - 2024. All rights reserved.