0
votes

In my tests, I've created a simple class like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Test
{
    public class MyCustomWindow: Window
    {

    }
}

This class is compiled into a dll. In another project, I tried to use this custom window, like this:

<Custom:MyCustomWindow x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Custom="clr-namespace:Test;assembly=Test"
    Title="MainWindow" Height="600" Width="1210" WindowState="Maximized" >

   <Grid Background="Blue">
       <Button Content="Button" HorizontalAlignment="Left" Margin="457,212,0,0" VerticalAlignment="Top" Width="75"/> 
   </Grid>

This thing compiles with no errors, and works great when the custom window is opened by the "StartupUri" in the App.xaml file (that defines the first window loaded).

However, if I set other window to load in the StartupUri, and:

MainWindow m = new MainWindow();
m.Activate();
m.Show();
this.Close();

The CustomWindow will open, but without any content, without button and without the blue grid - and even without the title.

Any workaround? And what I need to do to open a Window with the same behavior of the StartupUri?

Edit:

I've noticed that the MainWindow (or any window derived from MyCustomWindow) simply cannot have the method InitializeComponent() in the constructor, because it does not exist in the context. Strangely, when using StartupUri, the contents are loaded normally without this.

Edit 2:

I think that the problem is occurring because I can't put the InitializeComponent() method in the MyCustomWindow. This explains why the MainWindow can be loaded normally into the StartupUri: it's loading directly from the xaml file, so it's parsing the content without the need of the InitializeComponent.

I starting to think about implement the IComponentConnector interface, but I have no idea how to do this.

Edit 3:

The code-behind of the file MainWindow.xaml.cs is:

using Test;

namespace TestingCustomWindow
{
    public partial class MainWindow : MyCustomWindow
    {
        public MainWindow()
        {
              // Cannot use InitializeComponent here
        }
    }
}
2
Mhmmm, you dont open the MyCustomWindow, instead you open the MainWindow with the code: MainWindow m = new MainWindow() ... - Dannydust
Thanks for the answer. Yep, I'm opening the MainWindow, because it's the name of the C# class that xaml is referencing. If I open a "new" MyCustomWindow, I will not open the Window that have a button, right? - Guilherme
I want to open the specific MainWindow that inherit from MyCustomWindow. - Guilherme
Uh, I see. I have never used before another classname for code behind. It doesn't feel good for me... But did you try to open the the Window with MyCustomWindow m = new MyCustomWindow()? - Dannydust
pls add the new window using Visual Studio and Replace :Window with :MyCustomWindow. You will get initializecomponent - Nitin

2 Answers

1
votes

pls add the new window using Visual Studio and Replace :Window with :MyCustomWindow. You will get initializecomponent. You will hav to update window tag with your CustumWindow tag in xaml also

Adding it as answer so other can use it.

Thanks

1
votes

I think the constructor has to look like this

public class MyCustomWindow: Window
{
    InitializeComponent();
}