Xamarin形式的触发活动指示器 [英] Triggering Activity Indicator in Xamarin Forms

查看:60
本文介绍了Xamarin形式的触发活动指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an icon on my Xamarin.Forms app, which when it is clicked, I would like to change it to an activity indicator. It looks as though I should use a Trigger, Event Triggers look good, but as my image is declared in XAML, I am not sure quite how it would fit together?

此刻,我在 stacklayout 底部的XAML中拥有此功能:

At the moment, I have this in XAML at the bottom of a stacklayout:

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

单击它后,我想在指定的时间内显示此内容,然后触发一些C#功能:

When it is clicked, I would like to show this, for a set amount of time, then trigger some C# functionality:

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

我不确定是否最好在XAML中使用触发器来配置所有内容,或者我是否可以在XAML中仅拥有一个占位符类型项,然后在C#中具有所有定义?

I am not sure whether it would be best to configure it all in XAML with a trigger, or if i can just have a placeholder type item in XAML and then have all the definitions in C#?

推荐答案

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

There are a few ways of doing this.

您可以简单地给每个变量一个x:Name,然后如果要隐藏它,则打开/关闭IsRunning和IsVisible.

You could simply give each of them an x:Name and then turn on/off IsRunning and IsVisible if you want to hide it.

我假设您正在进行一些数据绑定.由于IsRunning是一个布尔变量,因此您只需在后面的代码中将其绑定到布尔值即可.例如,在我的ViewModel中,我具有IsBusy属性并实现INotifyPropertyChanged:

I assume though that you have some data binding going on. Since IsRunning is a bool you could simply bind it to a boolean in your code behind. For instance In my ViewModel I have an IsBusy property and implement 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:

Then in the XAML you can bind to IsBusy:

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

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

I think that should handle it. If you need a timer you could use the same binding that I have here and use Xamarin.Forms built in timer class.

这篇关于Xamarin形式的触发活动指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆