Create an instance of a C # class from within XAML

I have a working application written in C #, and now I want to expand this application to allow the user to switch between viewing the application and viewing the built-in web browser (inside the same application window).

I also have a separate working web browser, which was also written in C #.

I just added functionality to the original application to enable tabbed displays where the original application will appear on the first tab and the built-in web browser on the second tab.

Tabbed mappings for the application were created using XAML markup in Visual Studio. Now I want to add a web browser instance that was also written in C # to the second tab that I created in the XAML markup.

It will be something like:

 <TabControl> <TabItem Header="Browser"> <StackPanel> <!-- Call/ instantiate the browser here --> </StackPanel> </TabItem> </TabControl> 

But I have no idea how I call / instantiate the browser from the XAML markup ...

The browser was created using C #:

 namespace Agent { public partial class Browser : Form { public Browser() { ... } } } 

Can someone explain to me how to create an instance of the Browser inside the XAML markup?

Edit

Ok, so I edited my XAML markup as recommended in the answer that was suggested - now I have:

 <Window ... xmlns:Agent="clr-namespace:Agent" ...> <Grid> ... <TabControl> <TabItem Header="R"> <StackPanel> ... </StackPanel> </TabItem> <TabItem Header="Browser"> <Agent:Browser x:Name="Browser" /> </TabItem> </TabControl> </Grid> </Window> 

I also updated my Browser.cs class, so now it extends UserControl , not Form :

 public partial class Browser : UserControl{ 

However, I get a compilation error in the line:

 <Agent:Browser x:Name="Browser" /> 

which reads:

The name "Browser" does not exist in the namespace "clr-namespace: Agent".

But it’s clear that the Browser exists in Agent , as shown in the code that I included here ... In fact, when I entered the line <Agent:Browser x:Name="Browser /> , when I typed : Browser was one of the options, appearing in the autocomplete menu ...

What am I doing wrong here? Why doesn't the compiler think that the Browser exists inside the Agent ?

+5
source share
2 answers

The key to instantiating any object in XAML is to make sure the namespace is declared. You can provide any XML prefix and assign it to the CLR ( ref ) namespace, and it will act as a using statement. For instance:

 <TabControl xmlns:agent="clr-namespace:Agent"> <TabItem Header="Browser"> <StackPanel> <agent:Browser/> </StackPanel> </TabItem> </TabControl> 

NOTE. Your object must extend the UIElement (or one of its children) in order for it to work in the XAML tree. If your control is a WinForms control, you need to either find the equivalent XAML control or wrap it in WindowsFormsHost ( ref ).


WPF vs. Winforms

The goal of this section is to help you know which platform code belongs to the namespace, as well as some trade-offs. I used both and can tell from experience that each of them has good points and ... not very good points.

  • WinForms classes live in the System.Windows.Forms and are accessible by reference to the System.Windows.Forms.dll assembly.
  • WPF classes live in the System.Windows and System.Windows.Controls and are accessible by reference to a set of DLLs
  • WinForms rendering is immediate. This means that you are working with raster images, and you yourself are responsible for cleaning and redrawing things (usually you can just call Invalidate ()). If you are manipulating large images, WinForms is easier to work with.
  • WPF rendering is declarative. This means that more work is being uploaded to your GPU, and you're just telling how to draw things. You can also use GPU shaders for special effects. WPF has a prettier out of the box look, but it has a reputation for making light things difficult but impossible things.
  • WinForms is easier to learn, but outdated.
  • WPF is built around data binding, allowing the user interface to automatically update in response to property values. It can also be completely restyled, although this is a pretty serious event.

If you are just starting out, I would go and bite a bullet to start a heavier learning curve for WPF. This will provide a basic understanding of what is being ported to other platforms, such as Windows Store apps, etc.

+5
source

First, you need to place this tag inside your UserControl opening tag as follows:

 <UserControl x:Class="View.testControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:Agent="clr-namespace:Agent"> 

Then you can use it as follows:

 <TabControl> <TabItem Header="R"> <StackPanel> ... </StackPanel> </TabItem> <TabItem Header="Browser"> <Agent:Browser x:Name="Browser" /> </TabItem> </TabControl> 

EDIT
From what you told me in the comments, you need to create Custom Control in your WPF project. For this you need:

  • Right click on your project;
  • Select Add New Item;
  • From the installed (which is in the left column) select WPF ;
  • From the list in the middle column, select Custom Control ;

Now you can create this control in your XAML with the xmlns attribute.
Here is a great example from msdn on how to create custom controls

0
source

All Articles