Click here to Skip to main content
15,885,767 members
Articles / All Topics

Using DeviceFamily specific views with Caliburn.Micro

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
8 Sep 2015CPOL2 min read 5.4K   1  
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 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.

The post Using DeviceFamily specific views with Caliburn.Micro appeared first on Bogdan Bujdea.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Feel IT
Romania Romania
Hey! I'm Bogdan Bujdea, a software developer from Romania. I started to learn programming since my first year of highschool with C++. During my time in college I tried various languages like Java, C#, Haskell, Javascript, Python, etc. but my favorite remains C#.
I started my career with Thinslices as a Windows 8 developer, but now I work for Feel IT Services as a Windows Phone developer.

Comments and Discussions

 
-- There are no messages in this forum --