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.
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:
6 responses so far ↓
1 bitbonk // Mar 27, 2006 at 10:03 am
Well, the real tragedy is that c++/CLI is not (really) supported by msbuild. As a result of that we cannot use C++/CLI for codebehind XAML
2 william // Mar 27, 2006 at 7:32 pm
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.
3 Dean // May 12, 2006 at 4:32 pm
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.
4 Dean // May 12, 2006 at 10:23 pm
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.
5 Xaria // Jun 16, 2008 at 10:34 am
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.
6 Joel // Sep 3, 2010 at 7:27 am
This doesnt work to me
Leave a Comment