Category Archives

Solving the #Xamarin error “Unable to copy appname.dll…”

.NET, Technical stuff, Visual Studio, Work, Xamarin
No Comments

If you recently updated Xamarin to the latest stable version in Visual Studio 2015 or Visual Studio 2017, you might have encountered an annoying error when trying to build:

Unable to copy appname.dll from obj to bin because it is being used by another process

This error typically happens the second time that you try to build after you start Visual Studio. The first time everything works OK, then suddenly you cannot build anymore. Cleaning the solution doesn’t help, the only “fix” is to restart Visual Studio which is of course really annoying when you are developing.

This error is known, as shown by the Bugzilla issue #56275. There is also an old forum discussion on the Xamarin forums, which is a bit confusing because it might be a recurring error in the Xamarin updates.

Fortunately, the Bugzilla discussion also carries a workaround. This is a Visual Studio extension that you can install, which should fix the issue. I also talked to Microsoft about this and got confirmation that the error is fixed in a future release of Visual Studio. The engineer who replied to me also gave me a VSIX to fix the issue. I am not entirely sure what the difference is with the one in the Bugzilla issue, so I will just copy it here for your convenience. As far as I can tell, both VSIX files work well.

Happy coding
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook
 

Using the Windows Template Studio with #MVVMLight

.NET, Build, MVVM, Technical stuff, Universal Windows Platform UWP, Visual Studio, Windows 10, Work
1 Comment

Here is a story of a great collaboration between Microsoft and the community.

Last year in August, as I was speaking at VS Live in Redmond, I was asked by my friend Clint Rutkas for coffee because he had an idea he wanted to run by me. Now if you know Clint, that is usually really intriguing and I was really excited to hear about his idea.

Clint told me about the project he was thinking of starting. This was just conceptual at the time, but his idea made a lot of sense: Why not make the process of creating a new Universal Windows application a lot easier. Why not help people with all the tedious tasks that you get right after File, New, Project.

Usually when you get started, you need to add a bunch of pages and helpers to your application. The Blank application template is just that, blank. In MVVM Light, I added a project template for the supported frameworks (Universal Windows Platform, Xamarin.iOS, Xamarin.Android, Windows Presentation Foundation, Silverlight).

2017-05-24_11-09-27
The MVVM Light templates in Visual Studio 2017

However creating a meaningful project template is really difficult. You are constantly navigating between “too much content” and “too little content”. If you have too much, the developer ends up having to remove a whole lot of code, which is tedious. If you have too little, then the developer needs to spend time adding the basics, as well as sometimes having to look for guidance on how to do common things like navigation, dialogs, a Settings page, etc.

Building a wizard

Of course I thought of building a wizard to help people getting started. This is not easy though. First of all, the Visual Studio automation can be complex. You need to support multiple versions of Studio, and the extensibility model changes from version to version, so that is a big commitment. And this is where Microsoft has a huge advantage, they own the extensibility model, they know in advance is something is going to change, and they have the resource to create and maintain the code. And since the WTS is open source and available on Github, you can also contribute, and help make this project even better.

Installing the Windows Template Studio

Before you get started, you will need to add the Windows Template Studio (WTS) to Visual Studio 2017. Thankfully this is really very easy. In Visual Studio 2017, just select Tools, Extensions and Updates. Click the Online item on the left, and enter Windows Template Studio in the search box on the top right.

2017-05-24_09-47-08
The Extensions and Updates dialog with WTS

In Visual Studio 2017, you are now required to close the application to install the extension, so after the download is complete, close Studio and it will get installed.

Getting started

2017-05-24_11-16-46

The WTS starts in the File, New, Project dialog. Under the usual Universal Windows templates, you will see the entry for the WTS. Select it, give a name to your new application and press OK.

The project type

2017-05-24_11-19-16
The project type and framework dialog

The next screen is about the foundation of your app. You can select a type of navigation (currently you can select an app with a navigation pane, or an app with tabs), or simply a blank template without navigation.

The framework

Then comes the framework selection. This is where you will notice MVVM Light front and center. The other options at the moment are Code Behind, or a basic “no-name” MVVM implementation which can be useful in situations where the usage of 3rd party frameworks is completely forbidden.

The pages

2017-05-24_11-20-12
The pages dialog

The next step is probably the one I am the most excited about. It is usually so tedious to add the same pages over and over in every application I create. Here, you can choose to add pages such as blank page, map page, master/details, web view, settings etc. I can’t stress enough how much of a time saver this step is.

Note that the studio also supports you in finding out which licenses the 3rd party frameworks use. For example, if you add a Settings page which uses JSON.Net for the serialization of the settings, a link to the license is added to the Summary pane on the right, and so you don’t have any surprise.

The features

The features dialog is as exciting. With one click, you can add new experiences to your application such as Suspend/Resume, Background tasks, notifications, live tiles and more.

2017-05-24_11-54-40
The features dialog

This is probably the easiest way to offer a fully featured application to your users.

Why only MVVM Light at this time?

Microsoft ran numbers of the Nuget downloads and saw a confirmation that MVVM Light is the most widely used framework to build MVVM applications on Windows. In addition, its modularity and simplicity of use makes it a prime candidate for the first version of the WTS. However this is an open source project and so the developers of other projects (such as my good friends at the Prism or MVVM Cross projects) are already working to add support for their own framework in the WTS.

What about other application types?

At the moment, the WTS only works for Universal Windows applications. However nothing would prevent it to be updated for other application types. Personally I would welcome Xamarin support! And here we can predict that this will probably happen sooner or later, supported by the community of open source developers. I can see a lot of advantages in adding support for Xamarin or even WPF desktop applications. So I encourage everyone to head to Github and start contributing!

Resources

Here are some videos and links to get started:

I hope that you also see the potential of this great tool and that you will contribute.

Happy coding!
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook
 

Live blogging the Microsoft Build keynote, day 2 #msbuild

.NET, Build, Conferences, Technical stuff, Universal Windows Platform UWP, Visual Studio, Windows 10, Work, Xamarin, XAML
No Comments

And the show continues on day 2! In this post, I will be live blogging the Microsoft Build keynote on day 2. This should be a very exciting event with focus on client development, Windows and Mixed Reality. Get ready!!

Live blogging the Microsoft Build keynote, day 1 #msbuild

.NET, Build, Conferences, HoloLens, Technical stuff, Universal Windows Platform UWP, Visual Studio, Windows 10, Work, Xamarin, XAML
No Comments

Build day 1 is a wrap and you can find all the live notes taken during the keynote in this post!

My plans for Build 2017 #msbuild

.NET, Blog, Build, Conferences, Technical stuff, Universal Windows Platform UWP, Visual Studio, Windows 10, Xamarin
No Comments

Days are passing and the time is getting closer to pack my stuff and fly to Seattle again. This will be my first trip to my third home this year, and I am really impatient to be there. I just love that place.

Getting there

I will be arriving in town on Saturday evening, quite late. My plan is to pick a rental car and get to the hotel in Pioneer Square and get to bed :) The next morning, based on experience, I will be up super early (thank you jet lag). This is a droning day! I am not 100% sure yet where I will drive but I am considering getting close to Rainier National Park if the weather is good enough. Alternatively maybe take the ferry to Bainbridge to make footage of the islands. Or maybe the Snoqualmie area. Or maybe all of the above. This will be a day like I love, driving around on my own schedule, relaxing, flying, filming, editing pictures at a cafe… Should be a lot of fun if the clouds are cooperating!

Visiting the office

Monday will be an office day. Seattle is an important hub for Valorem, and this is the office where it all started for me. I love going there and seeing the new and improved space. So as usual I will be looking forward to meet new colleagues and meet old colleagues again!

The conference: Live blogging

During the conference itself, I will try something new this year: I will be live blogging the keynotes and the sessions that I can attend. In previous years, I was always taking notes during the events and posting these notes to my blog every so often. Surprisingly, this has been quite popular, even though these are my raw notes without much commentary. I found out that not everyone can watch the live stream, and people were reading my notes and getting some of this keynote feeling there. But of course posting a blog post is a lot slower than taking the actual note. So this year I installed a new plugin on my blog called 24LiveBlog. This will allow me to publish my notes about the keynote in real time as well as hopefully post pictures too. That should be a fun exercise!

You will be able to follow the live blogs at the following URLs. However please note that these links will only be active on and after the keynote day 1 and 2 respectively!

Keynote day 1 (Wednesday May 10, 2017): http://blog.galasoft.ch/posts/?p=1601
This keynote will take place from 8AM to 11AM PST
(4PM London time, 5PM Zurich time).

Keynote day 2 (Thursday May 11, 2017): http://blog.galasoft.ch/posts/?p=1606
This keynote will take place from 8:30AM to 10AM PST
(4:30PM London time, 5:30PM Zurich time).

I am really looking forward to this exercise and hope that it will be useful to you all!

Wednesday: Mix2017 party with iHeartRadio and Valorem

Yes yes I know, the name is really significant… this is not a revival of the MIX conference! I know we all miss this amazing conference… No, the Mix2017 event is Valorem’s and iHeartRadio’s party at Build!

Join iHeartRadio and Valorem for #Mix2017, a fantastic night where music meets mixed reality. iHeartRadio’s DJ will mix your favorite music and Valorem will mix your reality with exciting HoloLens experiences on Wednesday, May 10, at the Living Computers Museum + Labs. As a bonus, event attendees get free admittance to the museum.

More details and registration on the Valorem website! I hope to see many of you at this event!

Happy coding!
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook
 

Setting the device brightness on Windows with WPF

.NET, Technical stuff, Windows 10, Windows 7, Windows 8, Work, WPF
2 Comments

I am working on a project which I can’t really detail, but one aspect caused me a few headaches. Finally I reached out to the Windows team at Microsoft and got great help from Katie Anderson who works on the brightness team. This was quite tricky, and we had to have a couple of roundtrips before we managed to find a way that worked.

Well, why?

In the application I am working on, it is necessary for the user to set the device brightness from the application itself. You might wonder why, because most Windows devices have hard keys to set the monitor brightness. For example on a Surface, you can use the Fn-Del and Fn-Backspace key combinations to alter the brightness of the screen. Or you can of course swipe from the right side and use the Brightness button to change the value in the Action Center.

However on that particular project, the user doesn’t have access to the keyboard, nor to the Action Center. This is a kind of kiosk scenario if you will where many of the Windows features are deactivated by policies, and no hard keyboard is provided. I had to find a way to do this programmatically.

First approach: Not so good

At first I thought I would have my application’s main window run in full screen and set a black Rectangle on top of everything, and change the Rectangle’s opacity in my code. That worked well but of course it wouldn’t work if other applications were in the foreground (yes, I forgot to mention, it’s a kiosk app which can start other apps and put them in the foreground…).

Second approach: Better but not great

OK, no problems I thought, let’s have my app open a modal full screen window which is always on top. I will then have this window made insensitive to touch or mouse clicks with the following code. Finally, this window will have the black Rectangle and be on top of everything all the time.

public static class WindowsServices
{
    private const int GwlExstyle = -20;
    private const int WsExTransparent = 0x00000020;

    public static void SetWindowExTransparent(IntPtr hwnd)
    {
        var extendedStyle = GetWindowLong(hwnd, GwlExstyle);
        SetWindowLong(hwnd, GwlExstyle, extendedStyle | WsExTransparent);
    }

    [DllImport("user32.dll")]
    static extern int GetWindowLong(IntPtr hwnd, int index);

    [DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
}

This worked OK but somehow it was not satisfying. I really wanted to have a solution where my app would modify the actual device brightness. There are a few reasons why the client also wanted that, and so I started to investigate deeper. Unfortunately, Bing searches (and the equivalent Google searches) didn’t really return anything satisfactory.

Using Powershell

After searching a bit, I got a first clue that what I wanted to achieve was doable: You can query and modify the device brightness from Powershell with the following script:

CODE TO GET THE BRIGHTNESS INSTANCE

PS C:> Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness

CODE TO SET THE BRIGHTNESS

PS C:\Users\lbugn> $monitor = Get-WmiObject -ns root/wmi -class wmiMonitorBrightNessMethods
PS C:\Users\lbugn> $monitor.WmiSetBrightness(50,10)

If you run these commands in Powershell, you will marvel at the result: Yes it does work and the screen’s brightness is modified. Great Scott, I am on the right track! The key to this is the WMI (Windows Management Interface) class WmiMonitorBrightness.

Converting to .NET

The next step was obvious: I needed to convert the Powershell script to some usable .NET code. Unfortunately, easier said than done. This is where I reached out to Microsoft for help and thankfully Katie really followed through and after a few iterations we got it to work.

First she used the WMI Code Creator tool to convert the Powershell script to .NET. I had no idea that such a tool existed, but then again I never had to dive so deep in the entrails of Windows. Unfortunately when running the code that the tool created (which I won’t post here to avoid confusion), I got some errors. One more roundtrip to Redmond (via email) and Katie found the way: The following code will indeed modify the brightness of the screen, yay!!

public static class WindowsServices
{
    private static ManagementObject _brightnessInstance;
    private static ManagementBaseObject _brightnessClass;

    static WindowsServices()
    {
        // Querying the Windows service to get the Brightness API.
        var searcher = new ManagementObjectSearcher(
            "root\\WMI", 
            "SELECT * FROM WmiMonitorBrightness");

        var results = searcher.Get();
        var resultEnum = results.GetEnumerator();
        resultEnum.MoveNext();
        _brightnessClass = resultEnum.Current;

        // We need to create an instance to use the Set method!
        var instanceName = (string)_brightnessClass["InstanceName"];
        _brightnessInstance = new ManagementObject(
            "root\\WMI",
            "WmiMonitorBrightnessMethods.InstanceName='" + instanceName + "'",
            null);
    }

    public static int GetDeviceCurrentBrightness()
    {
        // Getting the current value.
        var value = _brightnessClass.GetPropertyValue("CurrentBrightness");
        var valueString = value.ToString();
        return int.Parse(valueString); // Direct cast fails.
    }

    public static void SetDeviceBrightness(int newValue)
    {
        if (newValue < 0)
        {
            newValue = 0;
        }

        if (newValue > 100)
        {
            newValue = 100;
        }

        var inParams = _brightnessInstance.GetMethodParameters("WmiSetBrightness");
        inParams["Brightness"] = newValue;
        inParams["Timeout"] = 0;
        _brightnessInstance.InvokeMethod("WmiSetBrightness", inParams, null);
    }
}

I made a small sample here which gets the current value of the screen brightness and then increases it by 10% every time you click on a button. When you reach 100%, it goes back to zero on the next click. Simple enough, and does the trick.

I hope that this will be useful to a reader looking for the same feature. I’d love to be the first one to ever blog about this. I doubt that I am, and probably someone will point me to the article that I never found :) but seriously, I really couldn’t find any mention of something like that on the whole WWW.

Happy coding!
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)

Share on Facebook
 

Two millions #mvvmlight downloads

.NET, MVVM, Technical stuff, Universal Windows Platform UWP, Work, WPF, Xamarin, XAML
3 Comments

Sometimes between last week and this week, we crossed a threshold: Two millions MVVM Light downloads.

I don’t have many metrics about MVVM Light, because the toolkit itself is not instrumented in any way. You get the raw binaries without added sugar :) So the only numbers I can rely on are the number of downloaded copies, either on Nuget or as the VSIX extension for Visual Studio. Obviously this doesn’t say much about the number of applications created with MVVM Light, since once the VSIX is downloaded, every File / New Project command creates a new app without any download. Still, it’s been interesting to see the evolution in downloads since I started to track the numbers.

Recently, I had a memory on my Facebook wall reminding me that 2 years ago, I was posting about 500’000 downloads, just before my Visual Studio Live sessions in Las Vegas. Two years later I was in Vegas again, for VS Live again, and about to cross two millions. Clearly the curve went up quite a bit. I tried to analyze what was the most significant change, and I think the port to Xamarin definitely played a significant role there. I also saw an uptake in interest in conference submissions since MVVM Light is truly cross platform, especially since Xamarin Evolve 2014 where I had a session. Since then a lot more sessions were given about Building Truly Universal Applications with MVVM Light, and quite a few full rooms too.

Tracking the downloads

As I mentioned, it is difficult to track downloads in a significant manner. There are automated build stations that will restore Nuget packages every so often. On the other hand there are people who download the Visual Studio extension once and create new applications without downloading anything. The only significant metric, I guess, is the evolution in downloads over time. Here is a chart (click to enlarge):

2017-03-21_11-13-37

Right now I track downloads on the following pages:

What’s coming

Currently I am working on the following topics:

  • A post about Working with MVVM Light in Visual Studio 2017. This is a pretty long post, which will also end up in the documentation pages. It will also explain how the sample applications (created by the templates) work and what features they demonstrate.
  • Improving the project template for WPF. I want to add more features there and be on par with what I have for Windows 10, iOS and Android.
  • Adding a project template for iPad and iOS Universal.
  • MVVM Light for Xamarin Pluralsight course. No ETA on this at the moment, sorry.

And of course the backlog is still active! Don’t hesitate to send your feedback :)

Happy coding!
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook
 

Code and slides posted for my latest talks

.NET, Conferences, HoloLens, MVVM, Technical stuff, Universal Windows Platform UWP, UX, Windows 10, Work, Xamarin, XAML
No Comments

October and November have been quite busy with travel. I talked at a few occasions in some cool events and want to take a moment to share the code and slides below:

Xamarin Dev Day, Zurich, Switzerland

I love the concept of the Xamarin Dev Day. It’s a great way to reach out to local communities and have a day of fun teaching and training with Xamarin content. This year I took part to the Zurich, Switzerland occurrence. It definitely didn’t hurt that it was help in a very cool space, under a train viaduct in the previously industrial area of the city, which is these days a pretty cool place to live and work.

I held my talk titled “Building truly cross platform applications with Windows, Xamarin and MVVM Light” which comments on the fact that the so called “UWP” (Universal Windows Platform) applications are only for Windows (hence the W) and that if you want to target mobile iOS and Android devices, you need something more. Xamarin and MVVM Light are great solutions in this scenario, because they maximize the amount of code that can be shared, all the while increasing the testable surface of the application. You can find the code and slides at this page.

KC .NET User Group & Xamarin KC Dev, Kansas City, Missouri

You might have read that the company I joined in 2008 (IdentityMine) was acquired this year by a company based in Kansas City named Valorem Consulting. On November 1st and 2nd, I spent time at the head office to meet with a maximum of new colleagues. It was a nice stay in a city (and state) I had never visited before. In fact I even “visited” two states during that stay, because Kansas City is laid over Missouri and Kansas! Earlier on I had tweeted about me visiting KC, and the local .NET developer user group contacted me and asked if I wanted to speak at a user group meeting on that evening. Of course the answer was a resounding yes! And the best part is that the Xamarin KC user group also joined the fun for their first ever joint event. We had a great attendance with a full room of about 65 people, and a very nice interaction. After the event, a small group gathered at a local bar and we had pie and drinks (I had a root beer float, because why not). Excellent way to make new friends in a new city! I also held my talk “Building Truly Universal Applications with Windows, Xamarin and MVVM Light”.  You can find the slides and code here.

DevIntersection Europe, Haarlem, The Netherlands

Right after my return from the US (with the visit to Kansas City, a few days in the office in Seattle and then the MVP and Regional Directors Summit in Bellevue/Redmond), I had another short trip to the Netherlands. I spoke at the DevIntersection conference in Haarlem, a small city not far from Amsterdam. The location was superb, a convention center close to the historical center of the city, the hotel was great too (though the bed was wayyyyy too soft for me :) and the audience was very nice. It is definitely smaller than DevIntersection in the US (where they have events in Vegas and Orlando), but I had a good albeit small audience nonetheless and some great interactions with attendees and fellow speakers.

I spoke about the Windows 10 Universal Windows Platform, a dense session which contains information about creating a new UWP app; adapting the user interface to the various form factors, resolutions, orientations etc; porting classic windows applications to the Windows 10 ecosystem with the Desktop App Converter (also known as Centennial bridge); the Windows Continuum; and Microsoft HoloLens. Unfortunately because of unknown technical issues, I was not able to stream the HoloLens output to my phone nor to my Surface like I used to do. Unfortunately we ran out of time before I was able to solve the issue (which would probably have required restarting the HoloLens and the phone). I want to apologize to the audience for this technical glitch. I hope that the people who joined me later for a quick trial of the HoloLens were happy anyway, and that the others didn’t go back home with a bad image of this amazing technology :) I recorded a few videos showing the various demos I wanted to give, you can find all the material including code and slides on this page.

Thanks all for attending and happy coding!
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook
 

#Microsoft #Experiences in Paris: Code and slides

.NET, Conferences, HoloLens, MVVM, Technical stuff, Windows 10, Work, WPF, Xamarin, XAML
No Comments

Last week I had the pleasure of taking the TGV from Zurich to Paris, and after a quick 4 hours of comfortable travel, to make my way to the Porte Maillot and the Palais des Congrès.

TL;DR: All slides and source code is available from here.

A podcast

After a good night sleep I made my way to the speakers lounge and prepared the last steps for my talk. Then I took a moment to head to the meeting room where the Live Tiles podcast was being recorded. A band of podcasters got together and talked about the future of Microsoft. It was so nice to “see these familiar voices” in action. Even nicer when Christophe (Toss .NET) and Denis saw me and pulled me in to talk about HoloLens and our experiences with this device. You can hear the recording here, my contribution starts at 36:30.

My talk: Really universal apps with Xamarin and MVVM Light

My talk was titled (in French) “Construisons des applications vraiment universelles avec Windows, Xamarin and MVVM Light”. This favorite topic of mine shows how you can structure an application to share a maximum of code without any compromises. You can build healthy layers of Model and ViewModel, unit test them so you can feel secure about not breaking anything with future changes. Then you can build a thin layer of real native user interface for any supported framework with XAML or Xamarin (Windows Presentation Foundation, Windows 10 Universal, Xamarin.iOS, Xamarin.Android). And if you are in an enterprise scenario and looking for maximum productivity, you can also of course use Xamarin.Forms and render the same UI to all supported platforms.

The MVVM Light Toolkit assists you in these tasks, notably by providing a Data Binding framework for Xamarin.iOS and Xamarin.Android, as well as components that make it very easy to connect a list control to an ObservableCollection, with automatic refresh in case the collection changes. This open source toolkit is in it’s version 5.3, has been downloaded almost 1.7 million times and is even used by Microsoft to build parts of Windows 10.

I was really pleased when I headed to my conference room and saw a long queue in front of the entrance. I gave my talk to a standing room of about 270 people, and loved the interaction during and after the talk. A lot of very positive feedback, and we stayed and talked until the hostess kicked us out of the room :)

Thank you to everyone who came and made this a memorable moment. You will find all the details of the talk, the slides and the source code from this portal.

Some tea

Before I made my way to the Palais des Congrès, I took a pit stop at one of the most wonderful shops on Earth: the Mariage Frères tea store. This old fashioned store is stock full of amazing teas from all over the world. It’s a must-visit if you are a tea enthusiast or if you just want to spend a moment living in an atmosphere of luxury and pleasure.

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook
 

Slides and sample code for my presentations at #VSLive Boston

.NET, Conferences, Universal Windows Platform UWP, VSLive, Windows 10, Work, WPF, XAML
2 Comments

Thanks to everyone who came to my sessions at VSLive Boston. I had a great time. I hope it was informative and useful. I am aware that you take time out of your job to come and see us speak and I really hope that you found it worth your time.

I had two sessions:

Windows 10 – The Universal Application: One App To Rule Them All?

You can find the slides and sample code for this session here. This page also links to a video showing how Windows 10 Universal apps work on HoloLens!

Windows Presentation Foundation (WPF) 4.6

Here are the slides and sample code.

Thanks again for your warm welcome in Boston!! I even had some time to visit the city and had a blast in the historical places.

Happy coding
Laurent

GalaSoft Laurent Bugnion
Laurent Bugnion (GalaSoft)
Share on Facebook