如何使我的 WPF MainWindow 成为单例?
我想让我的 MainWindow 成为单例,因为我想让从应用程序中的所有其他窗口访问它变得更容易。但我无法让它运行。这就是我所做的。
像往常一样,我将 MainWindow 承包商设为私有,并创建了一个 public static MainWindow Instance
属性来返回静态实例。当我在没有任何其他更改的情况下运行它时,出现“无可用源”错误。我在互联网上搜索了一个相关主题 http ://www.netframeworkdev.com/windows-presentation-foundation-wpf/xamlc-singleton-class-80578.shtml。但是,我无法按照那里的建议使其工作。有些人建议将 MainWindow.xaml 中的更改为
<Window x:Class="TestApp.MainWindow"
“
<Window x:Class="TestApp.MainWindow.Instance"
看起来合乎逻辑”。然而,当我这样做时,我遇到了大量的编译错误(第一个错误说名称空间 TestApp 已经包含“MainWindow”的定义。)
我在互联网上找到了许多关于如何制作单实例应用程序的文章。我不是在找这个。我只想让我的主窗口成为单例。我已经在 WinForm 应用程序中做过很多次了。
I want to make my MainWindow a singleton because I want to make accessing it from all other windows in my app easier. But I couldn't make it run. Here is what I did.
As usual, I made the MainWindow contractor private, and created a public static MainWindow Instance
property to return a static instance. When I just run it without any other changes, I got "No Source Available" error. I googled the Internet and found one related topic at http://www.netframeworkdev.com/windows-presentation-foundation-wpf/xamlc-singleton-class-80578.shtml. However, I couldn't make it work as suggested there. Some suggest to make a change in MainWindow.xaml from
<Window x:Class="TestApp.MainWindow"
to
<Window x:Class="TestApp.MainWindow.Instance"
Which looks logical. However, when I did this, I got tons of compiling errors (first one says the namespace TestApp already contains a definition of 'MainWindow'.)
I found many articles on the Internet about how to make single instance app. I'm not looking for this. I just want to make my MainWindow a singleton. I have done it in WinForm apps many times.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
非常感谢大家的快速答复。关键点是我必须从 App.xaml 中删除
StartupUri="MainWindow.xaml"
。也感谢静态构造函数的提示。我想提的另一点是,我们还可以重写 OnStartup 来启动主窗口(只是为了缩短几行):Thank you all very much for the quick answsers. The key point is that I have to remove
StartupUri="MainWindow.xaml"
from App.xaml. Thanks also for the tip of static contructor. Another point I want to mention is that we can also override OnStartup to startup the main window (just to make a few lines shorter):不确定是否要使其成为单例,但你为什么要这样做呢?您可以简单地使用 Application.Current.MainWindow 随时随地获取应用程序中的 Application.MainWindow 属性。请参阅:http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow(v=VS.90) .aspx。
使其成为单例对我来说仍然没有意义 - 这如何使其更易于访问?您始终可以在公共静态变量中保存对主窗口的引用 - 这可以在主窗口的构造函数中设置:
但是考虑到上面的 Application.Current.MainWindow 为什么还要麻烦..
Not sure about making it singleton, but why would you want to? You can simple use Application.Current.MainWindow to get the Application.MainWindow property anytime from anywhere in your application.. See: http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow(v=VS.90).aspx.
Making it singleton still does not make sense to me - how does that make it more accessible? You can always save reference to your main window in a public static variable - this could be set in the constructor of your main Window:
But then given the above Application.Current.MainWindow why bother..
要使
MainWindow
成为单例,您需要执行以下步骤:将
MainWindow
Instance
添加到MainWindow
类...注意:set 访问器是私有的,因此其他人无法将其设置为其他任何内容。
在
MainWindow
中添加一个静态构造函数,并将MainWindow
的构造函数设置为private
,如下所示...现在删除
StartupUri="MainWindow. xaml”
来自App.xaml
文件,以便在启动应用程序时不会启动默认窗口。在 App.xaml.cs 中捕获 App 类的 Startup 事件,如下所示:To make the
MainWindow
a singleton, these are the steps you need to make:Add a
MainWindow
Instance
toMainWindow
class...Note: set accessor is private so that nobody else can set it to anything else.
Add a static constructor in
MainWindow
and make the constructor ofMainWindow
private
, like this...Now remove
StartupUri="MainWindow.xaml"
from yourApp.xaml
file so that no default window is launched when you start the application. Catch the Startup event of your App class in App.xaml.cs like this:从 App.xaml 文件中删除
StartupUri="MainWindow.xaml"
。 WPF 将不再为您启动任何窗口。将处理程序添加到 App.xaml.cs 中
App
类的Startup
事件。在此处理程序中,调用单例实例的
Show()
方法。注意:
App
类有一个名为MainWindow
的属性,因此在App_Startup()
中我添加了前缀具有命名空间的 MainWindow
类!Remove
StartupUri="MainWindow.xaml"
from your App.xaml file. WPF will not launch any window for you anymore.Add a handler to the
Startup
event of yourApp
class in App.xaml.cs.In this handler call the
Show()
method of your singleton instance.Note: The
App
class has a property calledMainWindow
, hence inApp_Startup()
I prefixed theMainWindow
class with the namespace!