Bogdan Bujdea

Windows Platform Developer

Using DeviceFamily specific views with Caliburn.Micro

I was reading an article about building adaptive layout in Windows 10 with RelativePanel and AdaptiveTrigger, and one of the topics was about device family specific views. I was using a Caliburn.Micro project to test what I was reading, and it was all working perfect until I created a view for the mobile family.

My view just had a button that was supposed to show a MessageDialog when pressed, but when I run the app I got an AmbiguousMatchException with the text “Ambiguous match found”.

AmbiguousMatchException

AmbiguousMatchException

I’m using Caliburn.Micro 3.0.0-beta1, so I am aware that this is not a stable release, that’s why I went to the Github repository and forked it, so I could debug the source code. After I removed the Nuget package and added the binaries that I built, I found the issue in the ViewLocator:

var method = element.GetType().GetTypeInfo()
                .GetDeclaredMethod("InitializeComponent")

The function GetDeclaredMethod() will throw that exception if there is more than one method with that name, and as you can see in the image below, there actually is another method called InitializeComponent with an Uri parameter.

InitializeComponent(Uri uri)

 

If you want to know more about this you can read this article. My idea to fix this was to call GetDeclaringMethods(), and use the first one returned, so the line above would turn into something like this:

 var method = element.GetType().GetTypeInfo()
                .GetDeclaredMethods("InitializeComponent").FirstOrDefault();

I haven’t put much thought into this so maybe there are cases where this won’t work and if this is the case please let me know, but for the moment it fixes the exception and my code works with different views on mobile and desktop. I submitted a pull request on Github with this fix, but until/if it gets merged, you can download the source code and do this change by yourself, it’s very easy.

I’m a Caliburn.Micro user for almost 3 years and I’m sorry that this is just my first contribution, but I will try to get more involved in this project, and I encourage everyone to do the same.

, , , ,