c# set main window after startup window

c# set main window after startup window

如果我启动我的应用程序,我会启动一个带有 3 个按钮的自定义 window。这是 app.xaml 代码:

<Application x:Class="EbayManager.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Startup="ApplicationStart">
</Application>

这是app.xaml.cs代码:

    private void ApplicationStart(object sender, StartupEventArgs e)
    {
        Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;

        ChooseAccountWindow chooseAccountWindow = new ChooseAccountWindow();
        chooseAccountWindow.ShowDialog();

        MainWindow mainWindow = new MainWindow(chooseAccountWindow.Result);
        mainWindow.Show();
    }

ChooseAccountWindow 有 3 个按钮,这里是 CustomAccountWindow.xaml.cs 代码:

public partial class ChooseAccountWindow : MetroWindow
{

    public string Result { get; set; }

    public ChooseAccountWindow()
    {
        InitializeComponent();
    }

    private void btnDastaschentuch2013_Click(object sender, RoutedEventArgs e)
    {
        this.Result = "dastaschentuch2013";
        this.Close();
    }

    private void btnSkeptar_de_Click(object sender, RoutedEventArgs e)
    {
        this.Result = "skeptar_de";
        this.Close();
    }

    private void btnAsdf_de_Click(object sender, RoutedEventArgs e)
    {
        this.Result = "asdf_de";
        this.Close();
    }


}

ChooseAccountWindow.xaml 应该是第二个 window,主要 window 应该是 MainWindow.xaml。按下我想要的按钮后,第二个 window ChooseAccountWindow.xaml 将关闭并且应该显示主 window 。那是正确的方法吗?因为这两个命令:

MainWindow mainWindow = new MainWindow(chooseAccountWindow.Result);
mainWindow.Show();

打开主window,但是没有显示标题,我无法设置window的高度和宽度。

这是MainWindow.xaml代码:

<Controls:MetroWindow x:Class="EbayManager.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
    Title="MainWindow" Height="3500" Width="525" GlowBrush="{StaticResource AccentColorBrush}">
<Grid>
</Grid>

这是MainWindow.xaml.cs

的代码
namespace EbayManager
{
public partial class MainWindow : MetroWindow
{
    private string p;

    public MainWindow()
    {
        InitializeComponent();
    }

    public MainWindow(string p)
    {
        // TODO: Complete member initialization
        this.p = p;
        MessageBox.Show(p);
    }
}

}

也许你们中有人可以帮助我。

你评论说标题不显示,你不能设置高度和宽度 window 告诉我这个问题与你在原始 post。该问题很可能与 MainWindow 的 WindowStyle 或与 MainWindow 相关的其他属性有关。

问题是您的 MainWindow(string p) 构造函数没有调用 InitializeComponent()。更改为:

public MainWindow(string p): this()
{
    // TODO: Complete member initialization
    this.p = p;
    MessageBox.Show(p);
}

或者这样:

public MainWindow(string p)
{
    InitializeComponent();

    // TODO: Complete member initialization
    this.p = p;
    MessageBox.Show(p);
}

编辑:

下面是一个示例,显示当您在 MainWindow(string p) 构造函数上指定 this() 时会发生什么:

class Program
{
    static void Main(string[] args)
    {
        var t2 = new Test2("blah");
    }
}

class Test1
{
    public Test1()
    {
        Debug.WriteLine("I'm in Test1.ctor()");
    }
}

class Test2: Test1
{
    public Test2()
    {
        Debug.WriteLine("I'm in Test2.ctor()");

        Initialize();
    }

    public Test2(string blah) : this()
    {
        Debug.WriteLine("I'm in Test2.ctor(string blah)");
    }

    private void Initialize()
    {
        Debug.WriteLine("I'm in Test2.Initialize()");

    }
}

输出为:

I'm in Test1.ctor() // Base constructors called first
I'm in Test2.ctor() // Specified constructor `this()` called
I'm in Test2.Initialize() // This is in the `Test2()` constructor so it executes
I'm in Test2.ctor(string blah) // Finally the code in `Test2(string blah)` is executed