WPF(3)----多窗口的实现

比起MFC,WPF下的多窗口实现更有层次感。下面的例子中,在主窗口下点击SubWindow菜单键实现子窗口的跳出。

1:工程右键[add] -->[Window], 本次添加的名称为SubWindow。完成之后,工程目录如下图所示:



2:主窗口代码如下:

MainWindow.xaml代码:

<Window x:Class="WpfMultiWindows.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="394" Width="537" Background="#FF030944">
    <Grid>
        <Menu Margin="0,0,0,335" HorizontalAlignment="Left" Width="529">
            <MenuItem Header="_SubWindow">
                <MenuItem Header="_New SubWindow" Click="Button_Click_SubWindow"/>
            </MenuItem>
        </Menu>
    </Grid>


</Window>

MainWindow.xaml.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfMultiWindows
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        static int SubWindowsNum = 0;
        private void Button_Click_SubWindow(object sender, RoutedEventArgs e)
        {
            SubWindowsNum++;
            SubWindow sw = new SubWindow();
            sw._Value2 = "I'm number " + SubWindowsNum.ToString() + " SubWindow!" ;
            sw.Show();
            return;
        }

    }

}

3:子窗口代码如下:

SubWindow.xaml代码:

<Window x:Class="WpfMultiWindows.SubWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="SubWindowDebug" Height="200.952" Width="269.96" Background="#FF0C7272">
    <Grid Name="Grid1">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="50*"/>
            <ColumnDefinition Width="23*"/>
        </Grid.ColumnDefinitions>
        <TextBox HorizontalAlignment="Left" Height="32" Margin="47,64,0,0" TextWrapping="Wrap" Text="{Binding Path=TextBoxValue}" VerticalAlignment="Top" Width="180" Grid.ColumnSpan="2"/>
    </Grid>
</Window>

SubWindow.xaml.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;


using System.ComponentModel;

namespace WpfMultiWindows
{
    /// <summary>
    /// Interaction logic for SubWindowDebug.xaml
    /// </summary>
    public partial class SubWindow : Window, INotifyPropertyChanged
    {
       public int SubWindowNum;

       public event PropertyChangedEventHandler PropertyChanged;

        protected void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
               PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public string _Value2;

        public string TextBoxValue
        {
            get { return _Value2; }
            set
            {
                if (value != _Value2)
                {
                    _Value2 = value;
                    NotifyPropertyChanged("TextBoxValue");
                }
            }
        }


        public SubWindow()
        {
            InitializeComponent();
            Grid1.DataContext = this;
        }

    }
}

运行程序,每点击一次[SubWindow]--->[New SubWindow]之后,都会弹出一个子窗口,并根据顺序编号。


  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
WPF 01-BootstrapperShell是一种用于启动和初始化WPF应用程序的框架。它是指示WPF应用程序在启动时应执行的代码的入口点。通常情况下,我们可以在App.xaml.cs文件中找到它。 BootstrapperShell提供了一种将应用程序的各个部分组织在一起的方式,以便在启动时执行特定的操作。这些操作可以包括设置应用程序的默认样式、添加全局资源、注册服务和创建主窗口等。通过将所有这些相关的代码集中到一个地方,我们可以更好地管控应用程序的启动过程。 通常情况下,BootstrapperShell会执行以下几个步骤: 1. 创建应用程序的主窗口:这个步骤通常在App.xaml.cs文件的OnStartup方法中完成。我们可以在这里创建一个MainWindow实例,并将其设置为应用程序的主窗口。 2. 设置应用程序的默认样式:WPF应用程序通常使用样式来定义应用程序中各个控件的外观和行为。在BootstrapperShell中,我们可以通过添加资源字典来设置应用程序的默认样式。 3. 注册服务和初始化其他组件:在应用程序启动时,我们可能需要注册一些服务或初始化其他组件,以便在应用程序中的其他地方使用。在BootstrapperShell中,我们可以执行这些操作。 4. 处理未捕获的异常:在应用程序中可能会发生未捕获的异常,我们可以通过在BootstrapperShell中实现Application.DispatcherUnhandledException事件处理程序来捕获和处理这些异常。 总而言之,WPF 01-BootstrapperShell是一种用于组织和管理WPF应用程序启动过程的框架。它提供了一个入口点来集中所有与应用程序启动相关的代码和操作,从而更好地控制应用程序的行为和外观。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值