【问题标题】:ActivityIndicator running after press the BackButtonActivityIndi​​cator 在按下 BackButton 后运行
【发布时间】:2016-11-28 17:59:37
【问题描述】:

I have a page with a list of items and when some is selected, the ActivityIndi​​cator turns on and goes to another page, turning off.当我在这个新页面中并单击 NavigationPage 上的 BackButton 时,我返回到带有项目列表的页面,但问题是 ActivityIndi​​cator 处于打开状态(持续存在)。我该如何解决?

[列表页面]

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 代码的一部分]

<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>

【问题讨论】:

  • 是否有可能您在EnvolvidoDetalhe 页面或 ViewModel 中的某个位置设置了IsBusy = true;,并且在用户点击后退按钮之前您从未将其设置回false?我建议要么在EnvolvidoDetalhe.OnDisappearing() 中设置IsBusy = false;,要么在ResultadosBuscados.OnAppearing() 的顶部进行设置。这能解决吗?如果是这样,请找出您将IsBusy 设置为true 的位置,而不是将其设置回false
  • @hvaughan3 默认为假!在其他不使用 ListView 和 OnItemSelected 的页面上,它工作得很好!我可以正常前进和后退。问题只是在 OnItemSelected 中发生!单击 BackButton 后我尝试调试,但页面 ResultadosBuscados 假设默认设置为 isbusy = false。
  • 我不确定IsBusy 在您更改页面时是否会重置,或者无论您转到哪个页面,IsBusy 是否保持相同的值。您是否尝试在ResultadosBuscados.OnAppearing() 中将IsBusy 设置为false?实际阅读IsBusy 的评论,它说“一次在多个页面上将 IsBusy 设置为 true 将导致全局活动指示器运行,直到两者都设置回 false。作者的工作是在清理之前取消设置 IsBusy 标志上一页。”
  • 我的ActivityIndicators 也总是像这样绑定IsVisible&lt;ActivityIndicator IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" Color ="#F4B400"/&gt;
  • @hvaughan3 我做到了! IsVisible="{Binding IsBusy}" IsRunning="{Binding IsBusy}" 但不起作用。另外,.OnAppearing() 是谁?

标签: c# xaml xamarin xamarin.forms activity-indicator


【解决方案1】:

就像我在评论中所说的,检查IsBusy 被设置为true 并且没有被设置回false 的任何地方。当页面更改并重新启动时,绑定不会消失。

另外,我发现这个很棒的代码在大多数时候对我来说都很好用,这使得你不必担心将IsBusy 设置为false(尽管被警告他们正在做一些花哨的东西,以便它可以在 ViewModel 中设置,您可以将代码添加到基础ContentPage,如果您不想让它在您的 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/ViewModels/AvailableLeaguesViewModel.cs#L41

*编辑:

OnAppearing 覆盖示例:

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;
    }
}

【讨论】:

  • Niiii​​iiiice !!工作...非常感谢您的关注!
  • @MCFer 没问题,很乐意提供帮助。但是就像我说的那样,如果这有效,则意味着您在某处将IsBusy 设置为true 而您没有将其设置回false,因此您应该搜索您使用的所有地方的解决方案IsBusy找出可能发生的地方。
  • 我做到了!没有...当有 ViewCell 并使用 ItemSelected 时,它可能是一个错误!因为在其他页面上,它不会发生。我寻找每个 IsBusy。
猜你喜欢
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-24
  • 1970-01-01
相关资源
最近更新 更多