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);
			}
		}
    }
}

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, I would not display that web view and I would go straight to the native view.

While navigating through the sea of googlementation(http://bfy.tw/7ljK), I saw various examples on how to do stuff with custom renderers and I walked through them one by one hoping to hit my use case. Most search results demonstrated how to replace the view when doing a custom renderer on a control element with the SetNativeControl method. However, this was a page renderer. So it was time for experiments!

The behavior that I got was that the default view was still rendering and my custom view was not. If commented out the content on the default ContentPage then it would successfully output the custom view. However that in turn meant no default behavior. There was no winning! It was like arguing with a cat!

Hours later, I was able to debug and discover that I had access to the Android Activity. Using the activity, I was able to replace the main content with the SetContentView method like so.

...
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(FooPage), typeof(FooPageRenderer))]
namespace FooApp.Droid.Renderer
{
    public class FooPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs e)
        {
            base.OnElementChanged(e);
            var activity = this.Context as MainActivity;
            var newView = activity.FindViewById(Resource.Layout.foo);
            activity.SetContentView(newView);
        }

    }
}

I hope this helps other people save a few hours in their quests of building awesome things.

Keep on coding on friends!

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 with Nullable types and found a solution that said to use

public DateTime? StartDate { get { return startAt.GetValueOrDefault().DateTime; } }

However, that wouldn’t give me the desired results I wanted. I want a null value back, if my original value was null. It would instead give me the default value of a DateTime. So off I went to go back to old school methods of making it happen and I change the code to look something like this

public DateTime? StartDate { get { return (startAt == null) ? null : startAt.Value.DateTime; } }

However, the compiler was still angry, giving me the error “Type of conditional expression cannot be determined because there is no implicit conversion between ‘<null>’ and ‘DateTime’. Which further confused me because when I then broke it down to basic techniques

public DateTime? StartDate { get { if (startAt == null) { return null; } else { return startAt.Value.DateTime; } } }

This worked. So I reset my frame of mind, performed a few more searches with newer clues and I discovered that to make the conditional operator work I had to provide an explicit conversion on the null return value to make it look like the following

public DateTime? StartDate { get { return (startAt == null) ? (DateTime?)null : startAt.Value.DateTime; } }

Then I created a few tests to break down each example and confirm my expected outcomes. When I created a simple example just to revisit using the null conditional operator one more time, I then finally noticed the output of the intellisense after typing the operator. This awesome operator also unwrapped the variable so that you could get straight to business.

I’m including the tests that I wrote to somewhat illustrate how my mind was processing this problem as I went through it. The tests were really just my scratch pad, for anyone who’s concerned about the best ways of writing unit tests.

Bonne nuit!

using System;
using Xunit;

namespace Test.Stuff
{
    public class Nullables
    {
        [Fact]
        public void Test_ConditionalOperator_NullValue()
        {
            DateTimeOffset? foo = null;

            var result = (foo == null) ? (DateTime?)null : foo.Value.Date;
            Assert.Null(result);
        }

        [Fact]
        public void Test_IfStatements_NullValue()
        {
            DateTimeOffset? foo = null;
            DateTime? result = null;

            if (foo == null) { result = null; } else { result = foo.Value.Date; }
            Assert.Null(result);
        }

        [Fact]
        public void Test_NullConditional_NullValue()
        {
            DateTimeOffset? foo = null;
            DateTime? result = null;

            result = foo?.Date;
            Assert.Null(result);
        }

        [Fact]
        public void Test_ConditionalOperator_WithValue()
        {
            DateTimeOffset? foo = new DateTimeOffset(2016, 09, 11, 01, 49, 00, TimeSpan.Zero);

            var result = (foo == null) ? (DateTime?)null : foo.Value.Date;
            Assert.Equal(2016, result.Value.Year);
            Assert.Equal(09, result.Value.Month);
            Assert.Equal(11, result.Value.Day);
            Assert.Equal(0, result.Value.Hour);
            Assert.Equal(0, result.Value.Minute);
        }

        [Fact]
        public void Test_IfStatements_WithValue()
        {
            DateTimeOffset? foo = new DateTimeOffset(2016, 09, 11, 01, 49, 00, TimeSpan.Zero);
            DateTime? result = null;

            if (foo == null) { result = null; } else { result = foo.Value.Date; }
            Assert.Equal(2016, result.Value.Year);
            Assert.Equal(09, result.Value.Month);
            Assert.Equal(11, result.Value.Day);
            Assert.Equal(0, result.Value.Hour);
            Assert.Equal(0, result.Value.Minute);
        }

        [Fact]
        public void Test_NullConditional_WithValue()
        {
            DateTimeOffset? foo = new DateTimeOffset(2016, 09, 11, 01, 49, 00, TimeSpan.Zero);
            DateTime? result = null;

            var blah = foo?.DateTime;

            result = foo?.Date;
            Assert.Equal(2016, result?.Year);
            Assert.Equal(09, result?.Month);
            Assert.Equal(11, result?.Day);
            Assert.Equal(0, result?.Hour);
            Assert.Equal(0, result?.Minute);
        }
    }
}