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
}
}
}