This is the code of my base page. I use to set up a binding by default on a property called IsLoading. Thus, in my view models, I just have to change the value of the IsLoading property and my view will display the busy indicator of the page.

In the OnAppearing event of the ContentPage, I call the LoadViewModel method. I have an InitializeViewModel method which is intend to be called once. The LoadViewModel will be called each time the page appear on the screen.

//
// <copyright file=ExtContentPage.cs company=Krossapp>
//     Copyright (c) Krossapp. All rights reserved.
// </copyright>
// <author>Nicolas Krier</author>
//
namespace SDF.XForm.Controls
{
    #region Using

    using SDF.Core.ViewModels;
    using Xamarin.Forms;

    #endregion

    /// <summary>
    /// Content page base.
    /// </summary>
    public class ExtContentPage : ContentPage
    {
        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref=ExtContentPage/> class.
        /// </summary>
        public ExtContentPage()
            : base()
        {
            this.SetBinding(ExtContentPage.IsBusyProperty, new Binding(IsLoading, BindingMode.OneWay));
        }

        #endregion

        #region Property

        /// <summary>
        /// Gets the view model.
        /// </summary>
        /// <value>The view model.</value>
        public ViewModelBaseExt ViewModel
        {
            get
            {
                return this.BindingContext == null ? null : this.BindingContext as ViewModelBaseExt;
            }
        }

        #endregion

        /// <summary>
        /// Raises the appearing event and initialize the view model.
        /// </summary>
        protected override void OnAppearing()
        {
            base.OnAppearing();
            this.ViewModel.LoadViewModel();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *