【问题标题】:Triggering Activity Indicator in Xamarin Forms在 Xamarin 表单中触发活动指示器
【发布时间】:2015-09-01 14:01:46
【问题描述】:

我的 Xamarin.Forms 应用程序上有一个图标,当单击它时,我想将其更改为活动指示器。看起来我应该使用Trigger,事件触发器看起来不错,但由于我的图像是在 XAML 中声明的,我不确定它如何组合在一起?

目前,我在 XAML 中的 stacklayout 底部有这个:

<Button x:Name="NewDeviceButton" 
          Image="glyphish_31_circle_x.png" 
          HorizontalOptions="End"
          VerticalOptions="EndAndExpand" />

当它被点击时,我想显示它一段时间,然后触发一些 C# 功能:

<ActivityIndicator Color="Black" IsRunning="true" />

我不确定是否最好使用触发器在 XAML 中进行全部配置,或者我是否可以在 XAML 中使用占位符类型项目,然后在 C# 中使用所有定义?

【问题讨论】:

    标签: c# xaml xamarin


    【解决方案1】:

    有几种方法可以做到这一点。

    您可以简单地给每个人一个 x:Name,然后打开/关闭 IsRunning 和 IsVisible 如果您想隐藏它。

    我假设您正在进行一些数据绑定。由于 IsRunning 是一个布尔值,您可以简单地将其绑定到后面代码中的布尔值。例如,在我的 ViewModel 中,我有一个 IsBusy 属性并实现 INotifyPropertyChanged:

    public class MyViewModel : INotifyPropertyChanged
    {
    
    
        public MyViewModel()
        {
        }
    
        private bool busy = false;
    
        public bool IsBusy
        {
            get { return busy; }
            set
            {
                if (busy == value)
                    return;
    
                busy = value;
                OnPropertyChanged("IsBusy");
            }
        }
    
    
        public async Task GetMonkeysAsync()
        {
            if (IsBusy)
                return;
    
    
            try
            {
                IsBusy = true;
    
                //do stuff here that is going to take a while
    
            }
            finally
            {
                IsBusy = false;
            }
        }
    
        #region INotifyPropertyChanged implementation
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        public void OnPropertyChanged(string name)
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;
    
            changed(this, new PropertyChangedEventArgs(name));
    
        }
    
        #endregion
    }
    

    然后在 XAML 中你可以绑定到 IsBusy:

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

    我认为应该处理它。如果您需要一个计时器,您可以使用与我在这里相同的绑定并使用计时器类中内置的 Xamarin.Forms。

    【讨论】:

    • 您是否选择了属性名称 ass IsBusy 特殊选择?因为页面也有 IsBussy 属性。数据绑定适用于 DataContext 成员,但无论如何。我想知道为什么 IsBusy? developer.xamarin.com/api/property/Xamarin.Forms.Page.IsBusy
    • 这正是我一直所说的属性,但它可以是任何你想要的名称。
    • 使用页面的“IsBusy”属性作为标志的一个优点是,如果 XF 的忙碌指示器得到修复,它也会显示出来:D
    【解决方案2】:

    James 的回答是正确的,但是,我更喜欢使用页面自己的 IsBusy 属性,原因有两个:

    1. 我很懒,如果没有必要,我不想设置 INotifyPropertyChanged 实现
    2. XF 是suppossed to use that property to display a notification by default。这似乎不起作用,但如果确实如此,这种方法已经让我们能够利用它

    与 James 回答的唯一区别(除了删除 INPC 实现)是您需要为页面命名 (x:Name="myPage"),然后使用 ActivityIndi​​cator 的 @ 使用 {Binding Source={x:Reference myPage}, Path=IsBusy} 对它的引用声明绑定987654326@ 和 IsRunning 值。

    即:

    MainPage.xaml:

    <ContentPage ... x:Name="myPage">
        ...
        <ActivityIndicator IsVisible="{Binding Source={x:Reference myPage}, Path=IsBusy}" IsRunning="{Binding Source={x:Reference myPage}, Path=IsBusy}" />
        ...
    </ContentPage>
    

    MainPage.xaml.cs:

    ...
    async void OnDoSomethingLong(...)
    {
        if (!this.IsBusy)
        {
            try
            {
                this.IsBusy = true;
    
                //await long operation here, i.e.:
                await Task.Run(() => {/*your long code*/});
            }
            finally
            {
                this.IsBusy = false;
            }
        }
    }
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多