Click to See Complete Forum and Search --> : Put the XAML in C#, derive a class in C++/CLI


sabeeshcs
April 11th, 2008, 04:52 AM
Hi,

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"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="basewindow" Height="400" Width="400" ResizeMode="NoResize">
<Grid>
<Canvas>
<ListBox Canvas.Left="10" Canvas.Top="10" Width="180" Height="350"
Name="listbox" x:FieldModifier="protected" />
<TextBox Canvas.Left="200" Canvas.Top="10" Width="180" Height="25"
Name="textbox" x:FieldModifier="protected" />
<Button Canvas.Left="200" Canvas.Top="45" Width="80" Height="25"
Name="addbutton" x:FieldModifier="protected">Add</Button>

</Canvas>

</Grid>
</Window>


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



My second project is like this

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

AppMainWindow.h
--------------
#pragma once
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Controls;

ref class AppMainWindow : wpflib::basewindow
{

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

void onAddButtonClick(Object^ sender, RoutedEventArgs^ e)
{
listbox->Items->Add(textbox->Text);
textbox->Text="";
textbox->Focus();
}

};

AppMainWindow.cpp
--------------

#include "AppMainWindow.h"

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

}



AppMainWindow.h
---------------
ref class FirstAppDerived : Application
{
//public:
//FirstAppDerived(void);
protected:
virtual void OnStartup(StartupEventArgs^ e) override
{
Application::OnStartup(e);
AppMainWindow^ mainwnd = gcnew AppMainWindow();
mainwnd->Show();
}
};

App.cpp
---------
#include "FirstAppDerived.h"

using namespace System;
using namespace System::Windows;
[STAThread]

int main( array<String^>^ 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.

Thankyou
Sabeesh