如何使我的 WPF MainWindow 成为单例?

发布于 2024-09-25 04:34:42 字数 816 浏览 7 评论 0原文

我想让我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤星 2024-10-02 04:34:43

非常感谢大家的快速答复。关键点是我必须从 App.xaml 中删除 StartupUri="MainWindow.xaml" 。也感谢静态构造函数的提示。我想提的另一点是,我们还可以重写 OnStartup 来启动主窗口(只是为了缩短几行):

 public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            TestApp.MainWindow.Instance.Show();
        }
    }

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):

 public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            TestApp.MainWindow.Instance.Show();
        }
    }
裂开嘴轻声笑有多痛 2024-10-02 04:34:42

不确定是否要使其成为单例,但你为什么要这样做呢?您可以简单地使用 Application.Current.MainWindow 随时随地获取应用程序中的 Application.MainWindow 属性。请参阅:http://msdn.microsoft.com/en-us/library/system.windows.application.mainwindow(v=VS.90) .aspx

Window mainWin = Application.Current.MainWindow;
mainWin.Title = "This will be set as the title of the Main Window";

使其成为单例对我来说仍然没有意义 - 这如何使其更易于访问?您始终可以在公共静态变量中保存对主窗口的引用 - 这可以在主窗口的构造函数中设置:

public partial class MainWindow : Window
{
    public static MainWindow myMainWindow; // ASSUMPTION: only one MainWindow is ever constructed otherwise this will be overwritten by latest such instance

    public MainWindow()
    {
        InitializeComponent();            
        myMainWindow = this;
    }
}

但是考虑到上面的 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.

Window mainWin = Application.Current.MainWindow;
mainWin.Title = "This will be set as the title of the Main Window";

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:

public partial class MainWindow : Window
{
    public static MainWindow myMainWindow; // ASSUMPTION: only one MainWindow is ever constructed otherwise this will be overwritten by latest such instance

    public MainWindow()
    {
        InitializeComponent();            
        myMainWindow = this;
    }
}

But then given the above Application.Current.MainWindow why bother..

み零 2024-10-02 04:34:42

要使 MainWindow 成为单例,您需要执行以下步骤:
MainWindow Instance 添加到 MainWindow 类...

public static MainWindow Instance { get; private set; }

注意:set 访问器是私有的,因此其他人无法将其设置为其他任何内容。

MainWindow 中添加一个静态构造函数,并将 MainWindow 的构造函数设置为 private,如下所示...

static MainWindow()
{
    Instance = new MainWindow();
}

private MainWindow()
{
    InitializeComponent();
}

现在删除 StartupUri="MainWindow. xaml” 来自 App.xaml 文件,以便在启动应用程序时不会启动默认窗口。在 App.xaml.cs 中捕获 App 类的 Startup 事件,如下所示:

public App()
{
    ...
    Startup += App_Startup;
    ...
}

void App_Startup(object sender, StartupEventArgs e)
{
    TestApp.MainWindow.Instance.Show();
}

To make the MainWindow a singleton, these are the steps you need to make:
Add a MainWindow Instance to MainWindow class...

public static MainWindow Instance { get; private set; }

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 of MainWindow private, like this...

static MainWindow()
{
    Instance = new MainWindow();
}

private MainWindow()
{
    InitializeComponent();
}

Now remove StartupUri="MainWindow.xaml" from your App.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:

public App()
{
    ...
    Startup += App_Startup;
    ...
}

void App_Startup(object sender, StartupEventArgs e)
{
    TestApp.MainWindow.Instance.Show();
}
甜尕妞 2024-10-02 04:34:42

App.xaml 文件中删除 StartupUri="MainWindow.xaml"。 WPF 将不再为您启动任何窗口。

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

将处理程序添加到 App.xaml.csApp 类的 Startup 事件。
在此处理程序中,调用单例实例的 Show() 方法。

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class App : Application
    {
        public App()
        {
            Startup += new StartupEventHandler(App_Startup);
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            WpfApplication1.MainWindow.Instance.Show();
        }
    }
}

注意: App 类有一个名为 MainWindow 的属性,因此在 App_Startup() 中我添加了 前缀具有命名空间的 MainWindow 类!

Remove StartupUri="MainWindow.xaml" from your App.xaml file. WPF will not launch any window for you anymore.

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

Add a handler to the Startup event of your App class in App.xaml.cs.
In this handler call the Show() method of your singleton instance.

using System;
using System.Windows;

namespace WpfApplication1
{
    public partial class App : Application
    {
        public App()
        {
            Startup += new StartupEventHandler(App_Startup);
        }

        void App_Startup(object sender, StartupEventArgs e)
        {
            WpfApplication1.MainWindow.Instance.Show();
        }
    }
}

Note: The App class has a property called MainWindow, hence in App_Startup() I prefixed the MainWindow class with the namespace!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文