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

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

Jenkins & Git & Windows Server – oh my!

Got a weird freezing issue in Jenkins when attempting to pull the tags from a git repository. Turns out that the credential manager was the interactive windows popup and it just locked the build process. The fix was as simple as going into the following location and removing the credential helper

[Git Install Folder]\mingw64\etc\gitconfig

Example from this
[credential]
helper = manager

to this
[credential]
# helper = manager

Episode 1 of the #WhyWait Hackathon is available to watch!

The first episode of the hackathon is out. I’m not entirely aware of how much of the build it will show as the episodes are brief, but fear not! Once it’s all done, I will create tutorials and walkthroughs!

http://whywait.kinja.com/watch-two-teams-of-innovators-compete-in-the-whywait-i-1716856087

Otherwise, enjoy!

Resurrecting Persistent Windows

Well, my amazing Wife gave me the green light on ordering 3x Dell P2415Q 4K monitors. So I enthusiastically connected them all via DisplayPort on my NVidia 970GTX card on my desktop and low and behold the windows are exhibiting the crazy re-arranging behavior!

Thus, I’m reviving the project and now since I’ll be able to test it immediately(Instead of building it at home, running it on my work desktop, shipping log files home back home to diagnose), I should be able to improve it even more.

Stay tuned for updates!

Displayport & AMD Radeon with latest Catalyst drivers

AMD_Radeon_graphics_logo_2014.300

Thanks AMD!

The DisplayPort issue is fixed and no longer re-arranging windows across my monitors when I lock my screen!

It seems as though my PersistentWindows utility is no longer necessary for ATI cards. I’ve just validated it on multiple machines and life is returning to normal with DisplayPort!

Looking at the feedback from the surveys, it seems as though my utility works on 50% of the use cases reported and hopefully NVidia also resolves the issue at a driver level.

While I appreciate all the feedback on the PersistentWindows tool, unless I’m back fighting the issue, it’s unlikely that I’ll put more work into it. The code is available to anyone else who wants to pick it up and I’m happy to provide assistance for those who are curious about WinAPI, PInvoking or anything else.

Otherwise, back to my other hobby projects!

Persistent Windows 1.0.3 & CodePlex

So I’ve updated yet another version of the PersistentWindows app, but this update is literally nothing but better logging, so that I can figure out what’s going on in various instances. I would love the logging output to help isolate the root cause and then I could move on to the convenience features.

Also, I’ve publish the code on CodePlex if anyone would like to contribute or are just generally interested.

I would love to make a video on this, but man it’s hard being a Dad, a Husband and a workaholic. I’ve missed this month, but maybe during my Thanksgiving break I can squeeze out a random coding video.

Bonne nuit!

Persistent Windows 1.0.2

Made a few changes that you can find in the release notes. What’s pretty awesome is that I posted this app as a solution in StackExchange’s Super user and it was well received by the OP. Enough, in fact where not only did he mark it as a solution, he also wanted to contribute in resolving the other edge case issues. So I’ll eventually plop on CodePlex and hopefully solve one of the world’s tiny problems 🙂

PersistentWindows 1.0.1 released

So I’ve been using the utility at work and it sometimes works and sometimes doesn’t. I’ve noticed a few issues

  1. Occasional race conditions occur when the monitor changes so I’ve improve the logic on resolution detection.
  2. I’ve also modified the little GUI to provide a bit more informative feedback.
  3. Improved the handler on maximized windows

Finally, I’ve also noticed that sometimes it doesn’t move a few windows and I was spinning the wheel a while to figure out why. Turns out those processes were running in admin, so if you want those restored, you have to run the utility as admin as well.

Slowly and surely, I’ll continue to improve the product.

Good night world!