按下返回按钮后的 ActivityIndicator 运行
ActivityIndicator running after press the BackButton
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.当我在这个新页面中并单击 NavigationPage 上的后退按钮时,我 return 到包含项目列表的页面,但问题是 ActivityIndicator 处于打开状态(持续存在)。我该如何解决?
[列表页面]
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>
就像我在评论中所说的那样,检查 IsBusy
被设置为 true
而没有被设置回 false
的任何地方。当页面更改并重新启动时,绑定不会消失。
此外,我发现这个很棒的代码对我来说大部分时间都运行良好,因此您无需担心将 IsBusy
设置为 false
(尽管警告说他们正在做一些花哨的事情,以便可以在 ViewModel 中设置它,如果您不想让它在您的 ViewModel 中工作,您可以将代码添加到所有页面派生的基础 ContentPage
).
*编辑:
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;
}
}
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.当我在这个新页面中并单击 NavigationPage 上的后退按钮时,我 return 到包含项目列表的页面,但问题是 ActivityIndicator 处于打开状态(持续存在)。我该如何解决?
[列表页面]
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>
就像我在评论中所说的那样,检查 IsBusy
被设置为 true
而没有被设置回 false
的任何地方。当页面更改并重新启动时,绑定不会消失。
此外,我发现这个很棒的代码对我来说大部分时间都运行良好,因此您无需担心将 IsBusy
设置为 false
(尽管警告说他们正在做一些花哨的事情,以便可以在 ViewModel 中设置它,如果您不想让它在您的 ViewModel 中工作,您可以将代码添加到所有页面派生的基础 ContentPage
).
*编辑:
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;
}
}