首页 > 解决方案 > Xamarin 表单上的 ActivityIndi​​cator

问题描述

如何将 ActivityIndi​​cator 放在 Xamarin Forms OnStart() 函数上。我正在检查 OnStart() 函数的网络访问。

标签: c#.netxamlxamarin.forms

解决方案


将 ActivityIndi​​cator 绑定到 BaseViewModel (IsBusy) 中的属性。

看法

<ActivityIndicator Color="Accent" IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" />

BaseViewModel(由所有 ViewModel 继承)

private bool _isBusy;

        public bool IsBusy
        {
            get { return _isBusy; }
            set
            {
                _isBusy = value;
                OnPropertyChanged("IsBusy");
            }
        }

为自己准备一个好的 MVVM 框架(Prism),并将网络检查放在起始页的 OnNavigatedTo 方法中。

 public override void OnNavigatedTo(INavigationParameters parameters)
 {
      IsBusy = true;
      await CheckNetwork();
      IsBusy = false;
 }

现在,您可以将相同的 ActivityIndi​​cator 片段粘贴到绑定到继承 BaseViewModel 的 ViewModel 的任何页面 (XAML) 中,并且它只会在您设置 IsBusy 时工作。


推荐阅读