如何使用 C# 在 WPF 中创建 2 个独立的窗口

问题描述 投票:0回答:2

我想在xaml中创建2个单独的窗口,并且我想从代码部分单独控制它们。你知道该怎么做吗?如果您能提供一些代码示例,我将不胜感激。

从现在起谢谢你...

c# wpf windows
2个回答
11
投票

在项目中添加第二个

Window
(第一个是 MainWindow.xaml)(右键单击项目 -> 添加 -> 窗口)。我们称之为 BobbyWindow。

在MainWindow.xaml.cs的构造函数中,调用:

BobbyWindow bWin = new BobbyWindow();
bwin.Owner = this;
bWin.Show(); 

瞧。

编辑:反映评论的附加信息

这之间的主要区别:

public MainWindow() 
{ 
   InitializeComponent(); 
   Window1 bWin = new Window1(); 
   bWin.Owner = this; 
   bWin.Show(); 
}

还有:

Window1 bWin = new Window1(); 

public MainWindow() 
    { 
       InitializeComponent(); 

       bWin.Owner = this; 
       bWin.Show(); 
    }

第一种情况中,bWin是MainWindow()构造函数的局部变量,这意味着它只存在于MainWindow()的括号内。
第二种情况中,bWin是类的本地,这意味着它只能从MainWindowclass

的边界内访问

0
投票

不行!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!! 不会工作!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!

:( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :( :(!!!!!!!! !

© www.soinside.com 2019 - 2024. All rights reserved.