Maddy123
Topic Author
Posts: 11
Joined: 23 Oct 2013, 23:19

Dynamically adding usercontrol to MainWindow

03 Apr 2014, 01:04

Hello,

I want to dynamically add usercontrol (defined in seperate xaml) to MainWindow.

Something like following:
Noesis_LoadXAML(&mUIRoot, &mUIRenderer, "Gui/DC2/MainWindow.xaml");
Noesis_LoadXAML(&mPlugin1Root, &mPluginUIRenderer, "Gui/DC2/xaml1.xaml");

main_root = (Noesis::Gui::FrameworkElement*)mUIRoot;
Noesis::Gui::FrameworkElement* p1_root = (Noesis::Gui::FrameworkElement*)mPlugin1Root;

stack_ = NsStaticCast<StackPanel*> (main_root->FindName("RootGridLeftMenus"));
stack_->GetChildren()->Add(p1_root);     <--- this does not work. 
xaml1.xaml is as following:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	   xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
	   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
	   mc:Ignorable="d"
	   x:Name="UserControlP1"
	   d:DesignWidth="100" d:DesignHeight="67">

	      <Grid x:Name="LayoutRoot">
		     <Button x:Name="Button1" Content="Button" HorizontalAlignment="Center" Width="100"        
                               VerticalAlignment="Top" Height="30" d:LayoutOverrides="VerticalAlignment"/>
		     <Button x:Name="Button2" Content="Button" HorizontalAlignment="Center" Margin="0,37,0,0" 
                               Width="100" VerticalAlignment="Top" Height="30"/>
	     </Grid>
</UserControl>
Is there any way to achieve this ?

Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: Dynamically adding usercontrol to MainWindow

03 Apr 2014, 01:19

Before I can answer correctly, I should ask some questions:
- It seems you are using Ogre Bindings, am I right?
- The UserControl you want to add is implemented with a custom class? I mean, do you have your own class that inherits from UserControl overriding UserControl.Source property to set the xaml path to load on initialization?
 
Maddy123
Topic Author
Posts: 11
Joined: 23 Oct 2013, 23:19

Re: Dynamically adding usercontrol to MainWindow

03 Apr 2014, 23:41

1. Yes we are using ogre bindings.
2. Yes I do have custom class overriding usercontrol and source loading my usercontrol xaml.
But it doesnt compile properly.

class codebehind is:

class xaml1: public Noesis::Gui::UserControl
{
NS_IMPLEMENT_INLINE_REFLECTION(xaml1, UserControl)
{
NsMeta<TypeId>("Plugin1");
NsString source = "Gui/DC2/xaml1.xaml";
Ptr<UIElementData> data = NsMeta<UIElementData>(TypeOf<SelfClass>());
data->OverrideMetadata<NsString>(UserControl::SourceProperty, "Source",
FrameworkPropertyMetadata::Create(source, FrameworkOptions_None));
}
};

We are trying to achieve plug in architecture where we will add different xaml (repesenting plugin) to
MainWindow as needed. Having usercontrol to define class somehow takes away that idea as main has to
maintain it then.

Any suggestion?

Thanks.
 
User avatar
sfernandez
Site Admin
Posts: 1912
Joined: 22 Dec 2011, 19:20

Re: Dynamically adding usercontrol to MainWindow

04 Apr 2014, 01:39

Ok.

Looking at your original code, I see some mistakes.

- You have to use Noesis_LoadXAML() only to load the main window and to create the main UI Renderer. If you want to load extra xaml files that are going to be connected to the main window, you have to use Noesis::Gui::LoadXaml<T>(const NsChar* xamlFile) function defined in <NsGui/IRenderer.h> header file. The code would look like this:
Noesis_LoadXAML(&mUIRoot, &mUIRenderer, "Gui/DC2/MainWindow.xaml");

main_root = (Noesis::Gui::FrameworkElement*)mUIRoot;

Noesis::Core::Ptr<Noesis::Gui::FrameworkElement> p1_root =
    Noesis::Gui::LoadXaml<Noesis::Gui::FramemeworkElement>("Gui/DC2/xaml1.xaml");

stack_ = NsStaticCast<Noesis::Gui::StackPanel*> (main_root->FindName("RootGridLeftMenus"));
stack_->GetChildren()->Add(p1_root.GetPtr()); 
- In the xaml you have to specify the code behind class. This is done by setting the x:Class property in the root element to the TypeId specified in your own class:
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      x:Class="Plugin1"
      x:Name="UserControlP1"
      d:DesignWidth="100" d:DesignHeight="67">

         <Grid x:Name="LayoutRoot">
           <Button x:Name="Button1" Content="Button" HorizontalAlignment="Center" Width="100"        
                               VerticalAlignment="Top" Height="30" d:LayoutOverrides="VerticalAlignment"/>
           <Button x:Name="Button2" Content="Button" HorizontalAlignment="Center" Margin="0,37,0,0" 
                               Width="100" VerticalAlignment="Top" Height="30"/>
        </Grid>
</UserControl>
The code behind class you posted has no compiler errors if appropriate headers are included (<NsCore/TypeId.h>, <NsCore/ReflectionImplement.h>, <NsGui/UIElementData.h>, <NsGui/FrameworkPropertyMetadata.h>), and using namespace (Noesis::Core, Noesis::Gui) are defined.

What compiler errors are you getting?

I think that UserControls fit perfectly on the idea of a Plugin Architecture. A UserControl offers an interface to interact with the main application, but keeps its internal logic isolated from the main application logic. Plugin logic could be even implemented in a separate library if you want.

What do you mean by "main has to maintain it then"?

Who is online

Users browsing this forum: No registered users and 1 guest