WPF文本导入

WPF文本导入,界面如图
在这里插入图片描述
程序实现代码如下

<Window x:Class="FindText.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:FindText"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="22"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Menu Grid.Row="0">
            <MenuItem>
                <MenuItem.Header>File</MenuItem.Header>
                <MenuItem Click="LoadFile">
                    <MenuItem.Header>Load</MenuItem.Header>
                </MenuItem>
                <MenuItem Click="SaveFile">
                    <MenuItem.Header>Save As...</MenuItem.Header>
                </MenuItem>
                <MenuItem Click="Clear">
                    <MenuItem.Header>Clear Content</MenuItem.Header>
                </MenuItem>
                <MenuItem Click="Exit">
                    <MenuItem.Header>Exit</MenuItem.Header>
                </MenuItem>
            </MenuItem>
        </Menu>
        <FlowDocumentReader
      Name="FlowDocRdr"
      Grid.Row="1"/>
    </Grid>
</Window>

后台代码


using System;
using System.IO;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Markup;
using Microsoft.Win32;

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

        private void LoadFile(object sender, RoutedEventArgs args)
        {
            var openFile = new OpenFileDialog
            {
                Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*",
                InitialDirectory = "Content"
            };

            if (openFile.ShowDialog().HasValue)
            {
                var xamlFile = openFile.OpenFile() as FileStream;
                if (xamlFile == null) return;
                FlowDocument content = null;
                try
                {
                    content = XamlReader.Load(xamlFile) as FlowDocument;
                    if (content == null)
                        throw (new XamlParseException("The specified file could not be loaded as a FlowDocument."));
                }
                catch (XamlParseException e)
                {
                    var error = "There was a problem parsing the specified file:\n\n";
                    error += openFile.FileName;
                    error += "\n\nException details:\n\n";
                    error += e.Message;
                    MessageBox.Show(error);
                    return;
                }
                catch (Exception e)
                {
                    var error = "There was a problem loading the specified file:\n\n";
                    error += openFile.FileName;
                    error += "\n\nException details:\n\n";
                    error += e.Message;
                    MessageBox.Show(error);
                    return;
                }

                // At this point, there is a non-null FlowDocument loaded into content.  
                FlowDocRdr.Document = content;
            }
        }

        private void SaveFile(object sender, RoutedEventArgs args)
        {
            var saveFile = new SaveFileDialog();
            FileStream xamlFile = null;
            saveFile.Filter = "FlowDocument Files (*.xaml)|*.xaml|All Files (*.*)|*.*";
            if (saveFile.ShowDialog().HasValue)
            {
                try
                {
                    xamlFile = saveFile.OpenFile() as FileStream;
                }
                catch (Exception e)
                {
                    var error = "There was a opening the file:\n\n";
                    error += saveFile.FileName;
                    error += "\n\nException details:\n\n";
                    error += e.Message;
                    MessageBox.Show(error);
                    return;
                }
                if (xamlFile == null) return;
                XamlWriter.Save(FlowDocRdr.Document, xamlFile);
                xamlFile.Close();
            }
        }

        private void Clear(object sender, RoutedEventArgs args)
        {
            FlowDocRdr.Document = null;
        }

        private void Exit(object sender, RoutedEventArgs args)
        {
            Close();
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

向着光-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值