【问题标题】:Why activity indicator takes nearly 3 seconds to start when I click button为什么单击按钮时活动指示器需要将近 3 秒才能启动
【发布时间】:2019-12-30 09:53:19
【问题描述】:

我在单独的 xaml 文件中有 ActivityIndicator。我在 MainPage xaml 中将其用作 CustomLoader。此 CustomLoader 可见性由 bool 变量 VisibleLoader 控制

<custom:CustomLoader IsVisible="{Binding VisibleLoader}" 
           AbsoluteLayout.LayoutBounds="0.5,0.5,1,1" 
           AbsoluteLayout.LayoutFlags="All" Grid.Row="2">
</custom:CustomLoader>

MainPageViewModel 中,当我单击说明ActivityIndicator 的按钮时,开始需要一些时间。见下方代码

MainPageViewModel代码

private bool _visibleLoader = false;
public MainPageViewModel()
{
    VisibleLoader = true;
    current = this;
    GetAllProducts();
    SyncCommand = new Command(SyncDevice);
}

public bool VisibleLoader
{
    get
    {
        return _visibleLoader;
    }
    set
    {
        this._visibleLoader = value;
        NotifyPropertyChanged("VisibleLoader");
    }
}

按钮点击事件代码

public async void SyncDevice()
{
    try
    {
        VisibleLoader = true;
        IsBusy = true;
        PageMessage = string.Empty;
        var strSerialNumber = DependencyService.Get<IDataHelper>().GetIdentifier();
        //VisibleButtons = true;
        resourceService = new ResourceService(new RequestProvider());
        if (GlobalSettings.deviceId == 0)
        {
            DeviceDetails deviceDtls = await resourceService.GetDeviceDetails(strSerialNumber);
            GlobalSettings.deviceId = deviceDtls.Id.Value;
        }
        if (GlobalSettings.deviceId.ToString() != null)
        {
            DevicesList data = await resourceService.GetProducts(GlobalSettings.deviceId);
            //this.lstProducts = data.Devices[0].Products;
            this.lstProducts = data != null ? data.Devices[0].Products : null;
            if (lstProducts != null && lstProducts.Count != 0)
            {
                //Filter Products from Parameter file
                var filterProducts = GlobalSettings.GetFilterProducts();
                if (filterProducts.Count > 0)
                {
                    //lstProducts = lstProducts.Where(product => !filterProducts.Any(fp => product.Name.ToLowerInvariant().Contains(fp.ToLowerInvariant()))).ToList();
                    lstProducts = lstProducts.Where(product => filterProducts.Any(fp => product.Name.ToLowerInvariant().Contains(fp.ToLowerInvariant()))).ToList();
                }
                TotalProducts = lstProducts.Count;
                lstProducts = lstProducts.OrderBy(s => s.Order).ThenBy(s => s.Name).ToList();
            }
            else
            {
                PageMessage = "No products found.";
            }
        }
        else
        {
            PageMessage = "Error occured. Please try again later.!";
        }
        NotifyPropertyChanged("lstProducts");
        resourceService = new ResourceService(new RequestProvider());
        await resourceService.SyncDevice(GlobalSettings.deviceId);
    }
    catch (Exception)
    {
        VisibleLoader = false;
    }
    VisibleLoader = false;
    IsBusy = false;
}
}

xaml 文件中的按钮

 <Button Text="Sync device" Command="{Binding SyncCommand}" HorizontalOptions="FillAndExpand"
                    BackgroundColor="LightBlue"  Margin="5" TextColor="White" BorderRadius="10" HeightRequest="40"></Button>

我该如何解决这个问题?

【问题讨论】:

  • ActivityIndi​​cator 将在主线程被阻塞时可见,例如当您执行一些异步/等待任务时。如果可能,请尝试以将其用作异步任务的方式重新设计您的 SyncDevice 方法。
  • 我不明白你的意思。谢谢。
  • 就像@G.hakim 下面的答案一样,您必须将您的 void 更改为 Task 或以某种方式重组您的调用,以便立即更改布尔值。因此,在调用 SyncDevice() 之前,可能在其他地方设置 VisibleLoader = true。

标签: xamarin xamarin.forms viewmodel activity-indicator


【解决方案1】:

首先,如果您的同步设备方法不是一个事件,我建议您将其作为一个任务,因为异步无效不是一个好习惯。

public async Task SyncDevice()

另外,可能是你的布尔值在主线程中没有改变,这反过来又导致了这种情况,所以尝试类似

Device.BeginInvokeOnMainThread(()=>{ //Change visibility... });

确保只在此处添加相关代码,因为这会阻塞主线程,进而导致 UI 冻结

更新

那么你的命令看起来像:

new Command(async ()=>{await SyncDevice()});

【讨论】:

  • 真的没什么区别。我无法将 void 更改为 Task,收到错误“错误的返回类型”。
  • 您能否分享更多代码,因为我认为不应该是这种情况?
  • 我添加了一个修复看看!
猜你喜欢
  • 2011-10-14
  • 1970-01-01
  • 2019-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多