Month: September 2016

  • XamarinForms – Overriding PageRenderer and NavigationService


    After further implementation on my pet project, I realized that my previous solution on the page renderers would cause the NavigationService to fail from going back. So with more experimentation, I’ve discovered a way to preserve that functionality and still allow the page renderers to override the default view functionality.

    using Xamarin.Forms;
    using Xamarin.Forms.Platform.Android;
    
    [assembly: ExportRenderer(typeof(FooPage), typeof(FooPageRenderer))]
    namespace FooApp.Droid.Renderer
    {
        public class FooPageRenderer : PageRenderer
        {
            private Android.Views.ViewGroup parentView;
            private Android.Views.View origView;
            private Android.Views.View newView;
    
            protected override void OnElementChanged(ElementChangedEventArgs e)
            {
                base.OnElementChanged(e);
    			
                var activity = this.Context as MainActivity;
                var newView = activity.FindViewById(Resource.Layout.foo);
                parentView = activity.Window.DecorView as ViewGroup;
                origView = parentView.GetChildAt(0);
    			
                parentView.RemoveView(origView);
                parentView.AddView(newView);
            }
    
    		private void Restore()
    		{
    			if (origView != null)
    			{
    				parentView.RemoveView(newView);
    				parentView.AddView(origView);
    			}
    		}
        }
    }
    
    Read the rest

  • XamarinForms – Overriding a ContentPage with a custom PageRenderer on Android

    The short version:

    If you want to have default behavior on all platforms except for Android. Do the following

    • Create your content page with all of it’s goodies
    • Create a custom PageRenderer
    • In the OnElementChanged function
      • Cast the Context property of the renderer to the Activity type associated
      • Take that value and execute the SetContentView method while passing in your new view!

    The awesome story version:

    I’m building a Xamarin Forms application which has a default implementation in most platforms and a special snowflake behavior on Android. While working through the Xamarin documentation I realized that custom renderers were the way to go.

    The default behavior for all platforms is performed through a WebView, which has some custom logic when pages are navigated. While the functionality still works, it doesn’t feel seamless. However, in Android there is already native functionality that really improves the user experience, so I wanted to make sure that if I was on the Android platform, … Read the rest

  • C# – How the Null Conditional Operator works with Nullable types

    The short answer:

    The null conditional operator also unwraps the nullable variable. So after the operator, the “Value” property is no longer needed.

    ex:

    DateTimeOffset? startAt;
    ...
    System.Writeline(startAt?.TimeOfDay);
    

    The longer story:

    I had a scenario with a Nullable DateTimeOffset and I was trying to perform something like the following.

    DateTimeOffset? startAt;
    ...
    public DateTime? StartDate { get { return startAt.Value.DateTime; } }

    As I tested, I immediately ran into a situation where that date was null, the program threw an exception and I needed to null check it. So I thought I wanted to use that latest, hottest stuff that Microsoft had to offer in C#.

    The null conditional operator. 

    I changed my code to the following

    public DateTime? StartDate { get { return startAt?.Value.DateTime; } }

    But instead it just got angry with me and for whatever reason I wasn’t looking at the obvious clues. I immediately ran off to search the interwebs on how to use this operator … Read the rest