Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
using System;
using Xamarin.Forms;
namespace AndroindIndicator
{
public partial class AndroindIndicatorPage : ContentPage
{
void Off_Clicked(object sender, System.EventArgs e)
{
this.IsBusy = false;
}
void On_Clicked(object sender, System.EventArgs e)
{
this.IsBusy = true;
}
public AndroindIndicatorPage()
{
InitializeComponent();
On.Clicked += On_Clicked;
Off.Clicked += Off_Clicked;
Next.Clicked += NextOnClicked;
}
private void NextOnClicked(object sender, EventArgs eventArgs)
{
MessagingCenter.Send(this, "Next");
}
}
}
using Xamarin.Forms;
namespace AndroindIndicator
{
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new NavigationPage(new AndroindIndicatorPage(){ Title = "Page1"});
MessagingCenter.Subscribe<AndroindIndicatorPage>(this, "Next", o =>
{
var nav = MainPage as NavigationPage;
nav?.PushAsync(new AndroindIndicatorPage() {Title = $"Page{nav.Navigation.NavigationStack.Count}"});
});
}
protected override void OnStart()
{
// Handle when your app starts
}
protected override void OnSleep()
{
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle when your app resumes
}
}
}
using System.ComponentModel;
using androindindicator.Droid;
using Android.Support.V7.Widget;
using Android.Views;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
using AProgressBar = Android.Widget.ProgressBar;
using View = Android.Views.View;
[assembly: ExportRenderer(typeof(NavigationPage), typeof(MyNavigationRenderer))]
namespace AndroindIndicator.Droid
{
public class MyNavigationRenderer : NavigationPageRenderer
{
private AProgressBar _progress;
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
base.OnLayout(changed, l, t, r, b);
for (var i = 0; i < ChildCount; ++i) {
var view = GetChildAt(i);
if (view is Toolbar) {
_progress?.SetZ(view.GetZ() + 1);
_progress?.Layout(l, view.Bottom - 10, r, view.Bottom + 10);
}
}
}
protected override void OnElementChanged(ElementChangedEventArgs<NavigationPage> e)
{
base.OnElementChanged(e);
if (_progress == null) {
_progress = new AProgressBar(Context, null, Android.Resource.Attribute.ProgressBarStyleHorizontal)
{
Indeterminate = true
};
AddView(_progress);
_progress.Visibility = ViewStates.Invisible;
}
if (e.OldElement != null) {
e.OldElement.Pushed -= NewElementOnPushed;
e.NewElement.Popped -= NewElement_Popped;
}
if (e.NewElement != null) {
e.NewElement.Pushed += NewElementOnPushed;
e.NewElement.Popped += NewElement_Popped;
foreach (var page in e.NewElement.Navigation.NavigationStack) page.PropertyChanged += CurrentPageOnPropertyChanged;
}
}
private void CurrentPageOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Element.IsBusy)) {
var page = (Page) sender;
Element.IsBusy = page.IsBusy;
}
}
private void NewElement_Popped(object sender, NavigationEventArgs e)
{
e.Page.PropertyChanged -= CurrentPageOnPropertyChanged;
}
private void NewElementOnPushed(object sender, NavigationEventArgs e)
{
e.Page.PropertyChanged += CurrentPageOnPropertyChanged;
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == nameof(Element.IsBusy)) {
var page = (NavigationPage) sender;
_progress.Visibility = page.IsBusy ? ViewStates.Visible : ViewStates.Invisible;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment