Dynamically adding usercontrol to MainWindow
Hello,
I want to dynamically add usercontrol (defined in seperate xaml) to MainWindow.
Something like following:
xaml1.xaml is as following:
Is there any way to achieve this ?
Thanks.
I want to dynamically add usercontrol (defined in seperate xaml) to MainWindow.
Something like following:
Code: Select all
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.
Code: Select all
<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>
Thanks.
-
-
sfernandez
Site Admin
- Posts: 3268
- Joined:
Re: Dynamically adding usercontrol to MainWindow
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?
- 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?
Re: Dynamically adding usercontrol to MainWindow
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.
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.
-
-
sfernandez
Site Admin
- Posts: 3268
- Joined:
Re: Dynamically adding usercontrol to MainWindow
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:
- 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:
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"?
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:
Code: Select all
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());
Code: Select all
<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>
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: Ahrefs [Bot] and 2 guests