Revit二次开发之WPF通过txt读取和存储TextBox的字符串【附源代码】

软件版本:VS2015 Revit2018

功能:Revit中运行程序时,在Window中的TextBox中自动显示指定txt文件中的字符串内容

缺点:会将txt文件中的字符串全部显示


程序展示:

1.程序启动



2.输入“666”,点击Button1,自动关闭窗体



扫描二维码关注公众号,回复: 1190903 查看本文章

3.再次启动程序,删除“666”,输入“777”,点击Button1,自动关闭窗体



4.再次启动窗体,发现TextBox内会同时显示两次的输入值。打开txt文件进行查看。




7.具体程序


<Window x:Class="Test.Window1"
        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:Test"
        mc:Ignorable="d"
        Title="Window1" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="55" Margin="146,108,0,0" TextWrapping="Wrap" Text="{Binding Path=SSS,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button1" Content="Button1" HorizontalAlignment="Left" Margin="198,186,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>

    </Grid>
</Window>

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;
using System.IO;//添加引用
using System.ComponentModel;

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

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            WriteFile("E:\\123.txt", textBox1.Text);
            Close();
        }
        /// <summary>
        /// 写文件
        /// </summary>
        /// <param name="Path">文件路径</param>
        /// <param name="Strings">文件内容</param>
        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                //Directory.CreateDirectory(Path);
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
                f.Dispose();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }

        private static string ReadFile(string Path)
        {
            //声明一个FileStream类的对象  
            FileStream fsRead = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] buffer = new byte[200];//声明一个字节数组,用来临时存储读取到数据,最大存储200字节  
            string a = null;
            while (true)
            {
                int r = fsRead.Read(buffer, 0, buffer.Length);//返回本次实际读取到的字节数   
                if (r == 0)//如果读取到的字节数为0,说明已到达文件结尾,则退出while循环  
                {
                    break;
                }
                string s = Encoding.UTF8.GetString(buffer, 0, r);//将字节数组转换成字符串;buffer:要转换的字节数组;0:第一个要解码的字节的索引;r:要解码的字节数   
                Console.WriteLine(s);
                a = s;
            }
            return a;
            fsRead.Close();  //关闭流  
            fsRead.Dispose(); //释放流  
            Console.ReadKey();
        }

        public class ViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private string sss = ReadFile("E:\\123.txt");
            public string SSS
            {
                get { return sss; }
                set
                {
                    sss = value;
                    NotifyPropertyChanged("管道偏移高度");
                }
            }

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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Electrical;
using System.Windows;
using System.ComponentModel;
using static Test.Window1;

namespace Test
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    class Command : IExternalCommand
    {
        UIDocument uidoc;
        Document doc;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            uidoc = commandData.Application.ActiveUIDocument;
            doc = uidoc.Document;
            Window1 w1 = new Window1();
            ViewModel v1 = new ViewModel();
            w1.DataContext = v1;

            if (!w1.ShowDialog() ?? false)
                return Result.Cancelled;

            string data = v1.SSS;

            return Result.Succeeded;
        }
    }
}


猜你喜欢

转载自blog.csdn.net/sinat_37519884/article/details/79235322
今日推荐