按BackButton

时间:2016-11-28 17:59:37

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

我有一个包含项目列表的页面,当选择了一些项目时,ActivityIndi​​cator会打开并转到另一个页面,关闭。当我在这个新页面中并单击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>

1 个答案:

答案 0 :(得分:0)

就像我在评论中说的那样,检查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;
    }
}