Wpf MainWindow封装多个窗口-让StartupUri显示多个窗口

发布于 2025-01-07 11:00:00 字数 922 浏览 0 评论 0原文

目的是创建一个包含多个 WPF 窗口的对象,该对象代表一个应始终一起显示的单元,每个 Window 保留其各自的职责 - 比如说选择布局在screen X ...

现在因为 WPF 是单窗口架构,我想将这个 Windows 列表包装在单个 Window 对象中,以便可以设置它例如,作为 StartupUri。

  • 我知道我可以在 Application 启动中手动 Show() 每个 Window,但这不是重点 - 我需要一组粘着的窗口在一起,我可以轻松地在代码库中的任何地方调用它们,因为我知道我有一个类来处理链接。
  • 对这里的多文档接口 MDI 不感兴趣,

我不知道如何实现这样的适配器,我尝试重写 BeginInit,但根本没有被调用。

如何停止窗口初始化

public class MultiWindow : Window
{
    private readonly IList<Window> _windows = new List<Window>();

    public void Register(Window window)
    {
        _windows.Add(window);
    }

    public override void BeginInit()
    {
        foreach (var w in _windows)
            w.Show();
        // base.BeginInit();
    }
}

我不太了解Window生命周期,有什么办法可以取消初始化并显示已注册窗口的列表吗?

The aim is to create an object that contains a number of WPF windows, which would represent a unit that should always be shown together, each Window retaining its own individual responsibilities - say choosing to be laid out on a screen X ...

Now because WPF is a single-window architecture, I would like to wrap this list of Windows in a single Window object, so that it could be set as StartupUri, for instance.

  • I know I could manually Show() each Window in Application startup, but that is not the point - I need a collection of windows that stick together and which I can easily call upon anywhere in my codebase, knowing I have one class that has taken care of the linkage.
  • Not interested in Multiple Document Interface MDI here

I do not know how to implement such an adapter, I tried to override BeginInit, but that did not get called at all.

How to stop Window initialization

public class MultiWindow : Window
{
    private readonly IList<Window> _windows = new List<Window>();

    public void Register(Window window)
    {
        _windows.Add(window);
    }

    public override void BeginInit()
    {
        foreach (var w in _windows)
            w.Show();
        // base.BeginInit();
    }
}

I do not understand Window lifecycle much, is there anyhow I could cancel the initialization and show the list of registered windows instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

耶耶耶 2025-01-14 11:00:00

我相信startupuri将允许您在应用程序启动时打开一个窗口
如果您想在应用程序启动时打开窗口,可以这样做。

有App.xaml文件,请转到App.xaml.cs文件。

请尝试...按照

  1. 删除 App.xaml 文件中的 StartupUri="MainWindow.xaml" 操作。
  2. App类继承自Application。您可以重写 OnStartup() 来自定义应用程序启动时间。

     公共分部类 App :应用程序
        {
          私有只读 IList _windows = new List<窗口>();
    
          protected override void OnStartup(StartupEventArgs e)
          {
            基础.OnStartup(e);        
    
            // 在此处添加您要打开的窗口。
            foreach(_windows 中的 var w)
            {
              w.Show();
            }
        }
    

我不确定这是否是您想要的。我希望这有帮助。

I believe that the startupuri will allow you to open a windows when the application start up
If you want to open windows when the application start up, you can do this way.

There is App.xaml file, please go to App.xaml.cs file.

please try...following

  1. Remove StartupUri="MainWindow.xaml" in your App.xaml file.
  2. App class inherited from Application. You can override OnStartup() to customize application stating up time.

        public partial class App : Application
        {
          private readonly IList<Window> _windows = new List<Window>();
    
          protected override void OnStartup(StartupEventArgs e)
          {
            base.OnStartup(e);        
    
            // Add windows you want to open here.
            foreach (var w in _windows)
            {
              w.Show();
            }
        }
    

I am not sure that this is what you want. I hope this help.

终止放荡 2025-01-14 11:00:00

我找到了一种方法来做到这一点,即为普通的单个 Window 对象和实际上的 Window 对象创建所有窗口的子类 BaseWindow包含多个窗口。

  • 现在,当您调用 .Show() 时,它将显示一个或多个窗口,具体取决于子类中封装的内容。

所有 Window 对象都应继承自 BaseWindow

public class BaseWindow : Window
{
    
    // Hide default implementation and call BaseShow instead
    public new void Show()
    {
        BaseShow();
    }

    protected virtual void BaseShow()
    {
        base.Show();
    }

    // ... abstract away any additional Window methods and properties required 
}

重写 BaseShow()

public class MultiWindow : BaseWindow
{        
    private readonly IList<Window> _windows = new List<Window>();

    public void Register(Window window)
    {
        _windows.Add(window);
    }

    protected override void BaseShow()
    {
        foreach (var w in _windows)
           w.Show();
    }       
}

在应用程序启动时设置 MultiWindow

 // First Window object instantiated becomes WPF MainWindow     
 var multiWindow = new MultiWindow();
 multiWindow.Register(new WindowA());
 multiWindow.Register(new WindowB());

使用 MultiWindow,但通过 BaseWindow 调用重写成员

// Cast down to BaseWindow in order to call the overriden member
var baseWindow = (BaseWindow)Application.Current.MainWindow;
// Show WindowA and WindowB
baseWindow.Show();

PS 我希望 Microsoft 在整个 .NET 中使用接口,使用 IWindow 接口而不是具体的 Window 类提供更多的功能,在这种情况下,也易于实施。

I found a way to do it, which is by making all my windows subclass BaseWindow, for both normal single Window objects and for Window objects actually containing multiple windows.

  • Now when you call .Show() it will either show one window or multiple, depending on what is encapsulated in the subclass.

All Window objects should inherit from BaseWindow

public class BaseWindow : Window
{
    
    // Hide default implementation and call BaseShow instead
    public new void Show()
    {
        BaseShow();
    }

    protected virtual void BaseShow()
    {
        base.Show();
    }

    // ... abstract away any additional Window methods and properties required 
}

Override BaseShow()

public class MultiWindow : BaseWindow
{        
    private readonly IList<Window> _windows = new List<Window>();

    public void Register(Window window)
    {
        _windows.Add(window);
    }

    protected override void BaseShow()
    {
        foreach (var w in _windows)
           w.Show();
    }       
}

Set up MultiWindow in application startup

 // First Window object instantiated becomes WPF MainWindow     
 var multiWindow = new MultiWindow();
 multiWindow.Register(new WindowA());
 multiWindow.Register(new WindowB());

Use MultiWindow, but call overriden members through BaseWindow

// Cast down to BaseWindow in order to call the overriden member
var baseWindow = (BaseWindow)Application.Current.MainWindow;
// Show WindowA and WindowB
baseWindow.Show();

P.S. I wish Microsoft employed interfaces throughout .NET, working with IWindow interface rather than the concrete Window class would provide much more power and in this case, also ease-of-implementation.

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