I have a ScrollView, and when I press a button then I need to show an ActivityIndicator and hide the ScrollView, but his is not happening. It's like the binding is not working, and the XAML is not detecting that my ShowActivityIndicator boolean value has a new value.
Here is my code snippet. I put here just the part that is important for this:
//viewmodel
public partial class DataEntryStatusViewModel : ObservableObject
{
[ObservableProperty]
private bool showActivityIndicator = false;
public IAsyncRelayCommand CompletionCommand => new AsyncRelayCommand(Completion);
private async Task Completion()
{
ShowActivityIndicator = true;
Thread.Sleep(2000); //await ExecuteProjectUpload();
ShowActivityIndicator = false;
Shell.Current.CurrentPage.ShowPopup(new ProjectCommitDialog(projectUpladService));
}
}
//xaml
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<ScrollView AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="All"
VerticalOptions="FillAndExpand"
HorizontalOptions="FillAndExpand">
<ScrollView.Triggers>
<DataTrigger TargetType="ScrollView"
Binding="{Binding ShowActivityIndicator}"
Value="True">
<Setter Property="IsVisible" Value="False" />
</DataTrigger>
<DataTrigger TargetType="ScrollView"
Binding="{Binding ShowActivityIndicator}"
Value="False">
<Setter Property="IsVisible" Value="True" />
</DataTrigger>
</ScrollView.Triggers>
</ScrollView>
<AbsoluteLayout AbsoluteLayout.LayoutBounds="0.5,0.5,1,1"
AbsoluteLayout.LayoutFlags="All"
IsVisible="{Binding ShowActivityIndicator}">
<Label Text="Uploading"
TextColor="Black"
WidthRequest="80"
FontSize="Caption"
AbsoluteLayout.LayoutBounds="0.53,0.4,0.1,0.1"
AbsoluteLayout.LayoutFlags="All"/>
<ActivityIndicator IsRunning="True" IsVisible="True"
Color="{StaticResource SunMeButton}"
WidthRequest="100" HeightRequest="100"
AbsoluteLayout.LayoutBounds="0.5,0.35,0.1,0.1"
AbsoluteLayout.LayoutFlags="All"/>
</AbsoluteLayout>
</AbsoluteLayout>
If I change in my inner AbsoluteLayout's IsVisible property o hardcoded True value, then the ActivityIndicator shows. I can't figure it out why it is not detecting the ShowActivityIndicator value changes. Did I miss something? thnx
According to your code, I suggest using Task.Delay
instead of Thread.sleep
,
private async Task Completion()
{
ShowActivityIndicator = true;
await Task.Delay(2000);
//Thread.Sleep(2000); //await ExecuteProjectUpload();
ShowActivityIndicator = false;
}
The difference between them, you could refer to When to use Task.Delay, when to use Thread.Sleep?
Hope it helps!