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: