按下BackButton后,ActivityIndi​​cator运行 [英] ActivityIndicator running after press the BackButton

查看:77
本文介绍了按下BackButton后,ActivityIndi​​cator运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含项目列表的页面,当选择了一些项目时,ActivityIndi​​cator打开并转到另一页面,然后关闭。当我进入这个新页面并单击NavigationPage上的BackButton时,我返回到具有项目列表的页面,但是问题是ActivityIndi​​cator处于打开状态(持久)。我该如何解决?

I have a page with a list of items and when some is selected, the ActivityIndicator turns on and goes to another page, turning off. When i am in this new page and i click the BackButton on NavigationPage, i return to the page with the List of items, but the problem is that the ActivityIndicator is on (persists). How can i fix it ?

[列表页面]

public partial class ResultadosBuscados : ContentPage
    {
        public ResultadosBuscados(IEnumerable dadosPesquisados)
        {
            IsBusy = false;
            InitializeComponent();
            BindingContext = this;
            ListaBuscados.ItemsSource = dadosPesquisados;

        }

        public void OnItemSelected(object sender, SelectedItemChangedEventArgs e)
        {
            IsBusy = true;
            stackActivity.IsVisible = true;
            Envolvido envolvSelec = (Envolvido)e.SelectedItem;
                if (envolvSelec == null)
                    return;

            IsBusy = false;
            stackActivity.IsVisible = false;
            this.Navigation.PushAsync(new EnvolvidoDetalhe(envolvSelec));

            this.ListaBuscados.SelectedItem = null;
        }

    }

[XAML代码的一部分]

[part of XAML code]

<StackLayout x:Name="stackActivity" IsVisible="False" Padding="12"
            AbsoluteLayout.LayoutFlags="PositionProportional"
            AbsoluteLayout.LayoutBounds="0.5,0.5,-1,-1">
    <Frame Padding="50" OutlineColor="Black" HasShadow="true" AbsoluteLayout.LayoutFlags="PositionProportional" Opacity="0.8" BackgroundColor="Black" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
        <StackLayout>
          <ActivityIndicator  IsRunning="{Binding IsBusy}" Color ="#F4B400"/>
          <Label Text="Aguarde..." TextColor="#F4B400"/>
        </StackLayout>
      </Frame>
  </StackLayout>


推荐答案

就像我在评论中说的那样,检查是否存在 IsBusy 被设置为 true ,而不是被设置回 false 。当页面更改并重新启动时,绑定不会消失。

Like I said in the comment, check for anywhere that IsBusy is being set to true and not being set back to false. The bindings do not go away when the page changes and is brought back up.

此外,我发现这段很棒的代码在大多数情况下都对我有效这样,您就不必担心将 IsBusy 设置为 false (尽管要警告他们正在做一些事情)这样的花哨的东西,以便可以在ViewModel中进行设置,如果不想让它工作,则可以将代码添加到所有页面派生的基本 ContentPage

Also, I found this awesome code that has worked well for me most of the time and which makes it so that you do not need to worry about setting IsBusy to false (though be warned that they are doing some fancy stuff so that it can be set in the ViewModel, you could instead add the code to a base ContentPage that all your pages derive from if you do not want to get it working in your ViewModel).

使其生效的代码:
https://github.com/xamarin/Sport/blob/4abddfab1e1cb0e7d14925aa27cae7685dbd5f38/Sport.Mobile.Shared/ViewModels/BaseViewModel.cs# L138

正在使用的示例:
https://github.com/xamarin/Sport/blob/04f6b99cec752a106d51566ed96231beacfd2568/Sport.Mobile.Shared/ViewModel。 #L41

*编辑:

OnAppearing覆盖示例:

OnAppearing override example:

public partial class ResultadosBuscados : ContentPage {

    public ResultadosBuscados(IEnumerable dadosPesquisados) {
        IsBusy = false;
        InitializeComponent();
        BindingContext = this;
        ListaBuscados.ItemsSource = dadosPesquisados;
    }

    protected override void OnAppearing() {
        base.OnAppearing();
        IsBusy = false;
    }
}

这篇关于按下BackButton后,ActivityIndi​​cator运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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