Reputation: 13
Is it possible to create an activity indicator without using XAML? I have a view already ready in .cs and I did not want to have to recreate it.
Upvotes: 0
Views: 531
Reputation: 9723
You can create every control that you can create via XAML from code, too. Please see the following example
class MyView : ContentView
{
AbsoluteLayout _layout;
ActivityIndicator _activityIndicator;
void InitializeControls()
{
_layout = new AbsoluteLayout();
this.Content = _layout;
// ...
_activityIndicator = new ActivityIndicator();
AbsoluteLayout.SetLayoutFlags(_activityIndicator, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(_activityIndicator, new Rectangle(.5,.5,-1,-1));
_layout.Children.Add(_activityIndicator);
}
async void UpdateData()
{
_activityIndicator.IsRunning = true;
this.Data = GetData();
_activityIndicator.IsRunning = false;
}
}
In InitializeControls
we are first initializing a layout (an AbsoluteLayout
in this case) and later create a new ActivityIndicator
. We are setting AbsoluteLayout.LayoutFlags
and AbsoluteLayout.LayoutBounds
with the AbsoluteLayout.Set...
methods (these are setters for attached properties, see here). Last we add the ActivityIndicator
to the AbsoluteLayout
.
The UpdateData
method is a made-up method to show how to use the ActivityIndicator
when updating the data of the view. We first set IsRunning
on the ActivityIndicator
to show it, then fetch the data and last unset IsRunning
to hide the `Activity
Upvotes: 1