问题
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 ?
[List Page]
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;
}
}
[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>
回答1:
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.
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).
Code to make it work: https://github.com/xamarin/Sport/blob/4abddfab1e1cb0e7d14925aa27cae7685dbd5f38/Sport.Mobile.Shared/ViewModels/BaseViewModel.cs#L138
Example of it being used: https://github.com/xamarin/Sport/blob/04f6b99cec752a106d51566ed96231beacfd2568/Sport.Mobile.Shared/ViewModels/AvailableLeaguesViewModel.cs#L41
*Edit:
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;
}
}
来源:https://stackoverflow.com/questions/40850631/activityindicator-running-after-press-the-backbutton