Dynamically load a Xaml file from a C++ app

While it’s an absolute tragedy that C++ does not directly support compiled Xaml, you can use Xaml dynamically from a C++ Avalon app using XamlReader::Load. Let me show you how to do that, using a simple example. You can create your Xaml file, either using a text editor or using a temporary dummy C# or VB project.

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    >
  <Canvas Background="#FF008000">
    <TextBox HorizontalAlignment="Left"
             Width="240" Height="30"
             Name="mTextBox" />
    <Button  HorizontalAlignment="Left"
             Width="75" Height="23"
             Name="mButton"
             Canvas.Top="45">Set Title</Button>
  </Canvas>
</Window>

Now here’s the C++ code that will load this Xaml, show the window, and also hook events to the member controls. The code is mostly self-explanatory if you understand basic .NET event handling.

ref struct EventHelper
{
    static void OnBtnClick(Object^ sender, RoutedEventArgs^ e)
    {
        Window^ mainwnd = Application::Current->MainWindow;
        TextBox^ txtbox = (TextBox^)mainwnd->FindName("mTextBox");
        mainwnd->Title = txtbox->Text;
    }
};

[STAThread]
int main(array<System::String^>^args)
{
    Stream^ st = File::OpenRead(
        "C:\\SomePath\\Window1.xaml");
    Window^ mainwnd = (Window^)XamlReader::Load(st);
    mainwnd->Height = 400;
    mainwnd->Width = 600;
    mainwnd->Title = "Dynamically load Xaml";
    //FindName will find the element with the specified identifier
    Button^ btn = (Button^)mainwnd->FindName("mButton");
    btn->Click += gcnew RoutedEventHandler(&EventHelper::OnBtnClick);
    st->Close();
    return (gcnew Application())->Run(mainwnd);
}

This Avalon stuff is pretty powerful I can tell you. Expect more entries as I figure out more stuff :nerd:

Advertisements

6 thoughts on “Dynamically load a Xaml file from a C++ app

  1. XAML and C++ won’t be fully supported until the next version of VS.
    At least that is my understanding. That is a tradegy. I am trying to
    avoid C# completely on our development effort.

  2. This might be a newbee question, but I was attempting to create the dynamic load of xaml
    with VC 2005 Express. I have all the Winfx SDK loaded – but seems like the RoutedEventArgs
    are not being found via the System namespace in a blank CLR app. What blank app should one
    start with? How do you resolve errors I get above?

    This (xaml loading C++) could be a powerful tool in future development. Thanks for your expertise.

  3. Nish,

    I was able to get your demo loaded and working after putting in the correct references from
    the WinFx assemblies.

    I was also able to load one of the Expression (Fabrikam) demo’s and control some of the
    controls on it after some mods to the xaml.

    Is there a reference at to what xaml will load without parsing errors?

    Also, is there a mapping somewhere to what you can control via “FindName”?

    Thanks.

  4. Hey Nish,
    I cam across your post, while looking up for solution to use the Xaml code with VC++. I managed to run a simple avalon application following ur previous post, but I am unable to put this one to action. Could you please throw some more light on this..
    Hoping you would read this.

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