WPF 使用Application类操作多窗口方法

       今天看到WPF编程宝典第七章Application类时,发现通过将一些窗口保存在Application类中时,可以更好的提现面向对象方法,始终使用为窗口类添加自定义方法,属性,事件,不需直接向外部公开窗口的字段,避免紧耦合。

工程如下:

App.xaml.cs

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WpfApp8
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        private List<Document> documents = new List<Document>();

        public List<Document> Documents { get => documents; set => documents = value; }
    }
}

Document.xaml:

<Window x:Class="WpfApp8.Document"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp8"
        mc:Ignorable="d"
        Title="Document" Height="78" Width="209">
    A new document.
</Window>

Document.xaml.cs:

using System.Windows;
namespace WpfApp8
{
    /// <summary>
    /// Document.xaml 的交互逻辑
    /// </summary>
    public partial class Document : Window
    {
        public Document()
        {
            InitializeComponent();
        }

        public void SetContent(string content)
        {
            this.Content = content;
        }
    }
}

MainWindow.xaml:

<Window x:Class="WpfApp8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp8"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="300">
    <StackPanel>
        <Button Click="cmdCreate_Click" Margin="10,10,10,5" Name="cmdCreate">Click to Create a Document</Button>
        <Button Click="cmdUpdate_Click" Margin="10,5,10,10" Name="cmdUpdate">Click to Refresh the Documents</Button>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System;
namespace WpfApp8
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void cmdCreate_Click(object sender, RoutedEventArgs e)
        {
            Document doc = new Document();
            doc.Owner = this;
            doc.Show();
            ((App)Application.Current).Documents.Add(doc);
        }

        private void cmdUpdate_Click(object sender, RoutedEventArgs e)
        {
            foreach (Document doc in ((App)Application.Current).Documents)
            {
                doc.SetContent("Refresshed at " + DateTime.Now.ToLongDateString() + ".");
            }
        }
    }
}

最后运行效果如下,点击Click to Create a Document按钮,创建两个窗口,调用Application类中公有方法,保存在List中。

点击Click to Refresh the Documents按钮,通过遍历((App)Application.Current).Documents 的List,来修改窗口信息:

©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页