Avalon with C++/CLI – what are your options?

As of today (Feb CTP), Visual C++ projects cannot have XAML in them as C# and VB projects can. Of course, this does not mean that you cannot target Avalon using C++, and if you have read my previous blog entries, you’d have seen a couple of ways how that can be done. To put things in the right perspective, let’s see what options we have and their pros and cons.

  • 100% procedural code – This would allow you to develop Avalon apps with just VC++ and no other tool. Disadvantages would be a lack of UI-Logic separation, unsuitability for designing complex UIs, and increased lines of code. Advantages are you can use C++, and needn’t depend on VC# or any other tool.
  • C++ code dynamically loading XAML – You still stay within VC++ and are not dependent on other tools. You also achieve UI-Logic separation, because the UI stuff is in the XAML. But your apps would have loosely distributed XAML file dependencies, and any XAML corruption would mean trouble. And while you have reduced LOC, you still need to write code to wire the XAML elements with control variables.
  • Put the XAML in C#, derive a class in C++/CLI – This is a pretty decent option right now. There is full UI-Logic separation, and you can use Cider to design your XAML. The elements are wired to your code by the compiler and LOC is further reduced. Disadvantage is the dependency on a temporary C# project to create the XAML, and the need to put that in a DLL.

Of course, in future, if direct support for XAML is added to VC++ (as I believe it will), then that’d be our best option. But until then, we are not out of the game, which is a good thing I say 🙂


8 thoughts on “Avalon with C++/CLI – what are your options?

  1. Hello Nish,
    for me it shows an ongoing trend at Microsoft. The cool wizzbang stuff gets added and
    is supported for C# and VB.NET from the beginning and added to MC++ as an afterthought.

    The priorities for MS seem to be as follows:
    1: VB.NET
    2: c#
    3: MC++ if time and resources allow it

  2. you forgot the last two…

    The priorities for MS seem to be as follows:
    4: Release first service pack for Visual Studio 2003 (3+ years layer), if time and resources allow it.
    5: Put out press release saying Vista will be delayed. Again. 😉

  3. I don’t think VB.NET takes priority over C#, honestly. Look at the LINQ preview: C# first. Look at Microsoft Research: C# first.

    MS developer priorities are, in my utterly foolish opinion:

    1. .NET (in specific, C#, then VB.NET, then C++/CLI)
    2. native code (in specific, MFC, ATL, then Win32)

  4. I will comment on one thing. C++/CLI is gaining ground at MS. Look at the Phoenix compiler research project (which will be used as the basis for all future compiler technologies at MS). The samples are all in C# and C++/CLI! No VB.NET, ha!

  5. “Put the XAML in C#, derive a class in C++/CLI” does not seem to be an option with production WPF. Does anyone have a sample doing this?


  6. Hi Nish
    I am trying to create a simple application in C++/CLI using WPF, using Deriving from a class in a C# DLL in VS-2008. Fro that I create a project in C# and compile it and the output is a .dll file.
    The step is like this
    File->new->Project->C#->WPF Custom Control Library
    Delete the default .xaml file and add a new one and code is like this,

    Window x:Class=”wpflib.basewindow”
    Title=”basewindow” Height=”400″ Width=”400″ ResizeMode=”NoResize”>


    And build the project, and it create a .dll file.

    I want to create a new C++/CLI project and give a referance to the first project. How can I do it?

    My second project is like this

    Open CLR Empty project
    Set SubSystem to /SUBSYSTEM:WINDOWS
    Set the entry point to main
    Add these referance
    And create a class “AppMainWindow”
    Code :

    #pragma once
    using namespace System;
    using namespace System::Windows;
    using namespace System::Windows::Controls;

    ref class AppMainWindow : wpflib::basewindow

    // void onAddButtonClick(Object^ sender, RoutedEventArgs^ e);

    void onAddButtonClick(Object^ sender, RoutedEventArgs^ e)



    #include “AppMainWindow.h”

    addbutton->Click += gcnew RoutedEventHandler( this, &AppMainWindow::onAddButtonClick);


    ref class FirstAppDerived : Application
    virtual void OnStartup(StartupEventArgs^ e) override
    AppMainWindow^ mainwnd = gcnew AppMainWindow();

    #include “FirstAppDerived.h”

    using namespace System;
    using namespace System::Windows;

    int main( array^ args)
    return ( gcnew FirstAppDerived())->Run();


    And successfully build the project. But at runtime it display an error like this

    An unhandled exception of type ‘System.NullReferenceException’ occurred in PresentationCore.dll

    Additional information: Object reference not set to an instance of an object.

    How can I find this error. Please help me.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s