WPF开发实战项目(一)——老化监测平台03
本文主要介绍主窗口和设置窗口之间的切换。
窗口间的切换
主窗口
主窗口.xaml
<Window x:Class="AgingMonitorPlatform.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:AgingMonitorPlatform"
mc:Ignorable="d"
Title="ViewWindow" Height="450" Width="800">
<Grid>
<Button x:Name="Setting" Content="配置" HorizontalAlignment="Left" Margin="694,32,0,0" VerticalAlignment="Top" Click="Setting_Click"/>
</Grid>
</Window>
主窗口.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;
namespace AgingMonitorPlatform
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Setting_Click(object sender, RoutedEventArgs e)
{
SettingWindow settingWindow = new SettingWindow();
settingWindow.Show();
}
}
}
设置窗口
设置窗口.xaml
<Window x:Class="AgingMonitorPlatform.SettingWindow"
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:reoGrid="clr-namespace:unvell.ReoGrid;assembly=unvell.ReoGrid"
mc:Ignorable="d"
Title="SettingWindow" Height="450" Width="680">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="60"/>
<RowDefinition Height="80"/>
<RowDefinition Height="210"/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="快速开始" Foreground="Black" FontSize="28"
HorizontalAlignment="Center" VerticalAlignment="Center"/>
<StackPanel Grid.Row="1" Orientation="Horizontal">
<StackPanel Orientation="Horizontal" Margin="57,0">
<Button x:Name="FileChoose" Content="选择文件" FontSize="16" Height="25" Click="FileChoose_Click"/>
<TextBlock x:Name="FilePath" Text="未选择文件" Margin="3,0" Foreground="Gray" FontSize="12" Height="20"/>
</StackPanel>
<Button x:Name="FileInput" Content="导入" Margin="30,0" Height="31" Width="61" FontSize="16" Click="FileInput_Click"/>
<Button x:Name="FileOutput" Content="导出" Height="32" Width="60" FontSize="16" Click="FileOutput_Click"/>
</StackPanel>
<StackPanel Grid.Row="2" Orientation="Horizontal">
<reoGrid:ReoGridControl x:Name="reoGridControl" Margin="36,0,0,0" Width="335" Height="Auto" Readonly="True"
SheetTabNewButtonVisible="False" ShowScrollEndSpacing="False" SheetTabVisible="False"/>
<Border Width="163" Margin="90,0,0,0">
<TextBlock Text="使用说明: 1.请联系开发者获取机型文件模板,按照模板填写完成后再【选择文件】进行【导入】 2.导入完成后请人为查看数据是否无误,若确定无误请点击【完成】按钮进入监控页面"
MaxWidth="160" TextWrapping="Wrap" FontSize="16"/>
</Border>
</StackPanel>
<StackPanel Grid.Row="3">
<Button x:Name="Finish" Content="完成" Height="32" Width="60" FontSize="16" Margin="36,22"
HorizontalAlignment="Right" VerticalAlignment="Center" Click="Finish_Click"/>
</StackPanel>
</Grid>
</Window>
设置窗口.xaml.cs
using Microsoft.Win32;
using System.Windows;
namespace AgingMonitorPlatform
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class SettingWindow : Window
{
public SettingWindow()
{
InitializeComponent();
}
private void FileChoose_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "c:\\desktop"; //初始的文件夹
openFileDialog.Filter = "Txt|*.csv";//在对话框中显示的文件类型
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
openFileDialog.ShowDialog(); //显示对话框
string filepath = openFileDialog.FileName; //获取选择的文件的全路径名
FilePath.Text = filepath;
}
private void FileInput_Click(object sender, RoutedEventArgs e)
{
reoGridControl.Load(FilePath.Text, unvell.ReoGrid.IO.FileFormat.CSV);
}
private void FileOutput_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Title = "窗口标题";
saveFileDialog.InitialDirectory = "c:\\desktop";
saveFileDialog.OverwritePrompt = true;
saveFileDialog.AddExtension = true;
saveFileDialog.DefaultExt = "csv";
saveFileDialog.Filter = "Txt|*.csv";
saveFileDialog.FileName = "data.csv";
if (saveFileDialog.ShowDialog() == true)
{
MessageBox.Show("保存成功");
}
else
{
MessageBox.Show("取消保存");
}
var filename = saveFileDialog.FileName; //得到保存路径及文件名
var sheet = reoGridControl.CurrentWorksheet;
sheet.ExportAsCSV(filename);
}
private void Finish_Click(object sender, RoutedEventArgs e)
{
this.Hide();
}
}
}