Thursday, July 31, 2014

MVVm views without C# code behind file

The MVVm is a great technique for separating the concerns. If we take ASP.Net MVC we can see it is built for using model view controller model. So by default wont allow us to write code behind for the views (We can write inside the cshtml though). But WPF as the technology was not built to use WPF to there is no in-built way to avoid the code behind of views. 

If the development team is so technology focused, it doesn't matter as they won't break the rules of MVVm even if there is code behind file. But since companies are offshoring to get cheap labor, enforcing quality is little tricky. As always cheap things comes with less quality. Most of the time people's priority is to get things done in quick way ie by writing everything in code behind instead of going through viewmodel. 

The challenge here is how to bring quality with these less skilled labor? One of the way to enforce viewmodel  usage is to delete the code behind of views which is nothing but the user controls.

If we just remove the usercontrol.xaml.cs, it will fail as the Initialize method is missing. So how to overcome is?

Add the Initialize method in the view itself. Yes we can write C# code in XAML. So the view user control will look as follows.

<UserControl x:Class="Joymons.MVVmDemo.Views.MainView"
             d:DesignHeight="300" d:DesignWidth="300">
        <![CDATA[ public MainView() { InitializeComponent(); }]]>
            <TabControl ItemsSource="{Binding ChildVMs}" SelectedIndex="0">
                        <TextBlock Text="{Binding Header}" />

I too started using VS 2013. That is why a black background in the code :)


  1. This comment has been removed by the author.

  2. Great post! I am see the programming coding and step by step execute the outputs. It's helpful for me. Also great blog here with all of the valuable information you have.
    Reasons to Learn WPF

  3. Hey, I enjoyed this blog. I love the way you talked about how the income of game developers can be increased with the help of innovative skills. Today, several freelancing platforms can help developers earn good income from the comfort of their homes. Eiliana is a platform where developers can find work from global clients and boost their income.

  4. Hii
    Thank you for the Sharing this information. I thoroughly enjoyed reading your blog. The way you discussed enhancing the income of game developers through innovative skills was particularly insightful. Here is sharing some Netsuite Functional Training journey information may be its helpful to you.
    Netsuite Functional Training