WdSaveDatas.xaml.cs 4.27 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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 FLY.Thick.Base.IService;
using FObjBase;
using System.ComponentModel;
using System.Threading;

潘栩锋's avatar
潘栩锋 committed
18
namespace FLY.Thick.Base.UI
潘栩锋's avatar
潘栩锋 committed
19 20 21 22
{
    /// <summary>
    /// Window_SaveDatas.xaml 的交互逻辑
    /// </summary>
潘栩锋's avatar
潘栩锋 committed
23
    public partial class WdSaveDatas : FLY.ControlLibrary.WindowBigClose
潘栩锋's avatar
潘栩锋 committed
24 25
    {
        ISaveManager mManager;
潘栩锋's avatar
潘栩锋 committed
26
        public WdSaveDatas()
潘栩锋's avatar
潘栩锋 committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
        {
            InitializeComponent();
        }
        public void Init(ISaveManager manager)
        {
            mManager = manager;
            this.DataContext = mManager;
        }


        private void button_open_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.SaveFileDialog open = new System.Windows.Forms.SaveFileDialog();
            open.Filter = "csv文件|*.csv";
            open.Title = "保存数据";
            open.FileName = System.IO.Path.GetFileName(mManager.SavePath);

            if (open.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                mManager.SavePath = open.FileName;
            }
        }

        private void button_save_Click(object sender, RoutedEventArgs e)
        {
            FLY.ControlLibrary.Window_Tip.Show("数据保存", "等待。。。。", TimeSpan.FromSeconds(2));
            mManager.Save(SaveReturn, null);
            this.Close();
        }
56
        private void SaveReturn(object asyncContext, object retData)
潘栩锋's avatar
潘栩锋 committed
57
        {
58
            Dispatcher.BeginInvoke(new AsyncCBHandler(_SaveReturn), asyncContext, retData);
潘栩锋's avatar
潘栩锋 committed
59
        }
60
        private void _SaveReturn(object asyncContext, object retData)
潘栩锋's avatar
潘栩锋 committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
        {

            bool ret = (bool)retData;
            if (ret)
            {
                FLY.ControlLibrary.Window_Tip.Show("数据保存", "成功输出到" + mManager.SavePath, TimeSpan.FromSeconds(2));
            }
            else
            {
                FLY.ControlLibrary.Window_Tip.Show("数据保存", "保存失败", TimeSpan.FromSeconds(2));
            }
        }
    }

    public interface ISaveManager
    {
        string SavePath { get; set; }

        /// <summary>
80
        /// 成功或失败,会调用 asyncDelegate
潘栩锋's avatar
潘栩锋 committed
81
        /// </summary>
82 83 84
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        void Save(AsyncCBHandler asyncDelegate, object asyncContext);
潘栩锋's avatar
潘栩锋 committed
85 86 87 88
    }

    public abstract class SaveManager : ISaveManager, INotifyPropertyChanged
    {
89 90

        public string SavePath { get; set; }
潘栩锋's avatar
潘栩锋 committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104


        public void PreSave()
        {
            string directory = System.IO.Path.GetDirectoryName(SavePath);
            if (string.IsNullOrEmpty(directory))
            {
                directory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            }

            SavePath = directory + @"\" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".csv";
        }


105 106
        AsyncCBHandler asyncDelegate;
        object asyncContext;
潘栩锋's avatar
潘栩锋 committed
107 108
        protected void Return(bool state)
        {
109
            asyncDelegate?.Invoke(asyncContext, state);
潘栩锋's avatar
潘栩锋 committed
110 111 112 113
        }
        /// <summary>
        /// 成功或失败,会调用 AsyncDelegate
        /// </summary>
114 115 116
        /// <param name="asyncDelegate"></param>
        /// <param name="asyncContext"></param>
        public void Save(AsyncCBHandler asyncDelegate, object asyncContext)
潘栩锋's avatar
潘栩锋 committed
117 118
        {
            //需要异步!!!!
119 120
            this.asyncDelegate = asyncDelegate;
            this.asyncContext = asyncContext;
潘栩锋's avatar
潘栩锋 committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
            //先复制数据出来
            //再开一个线程输出到csv
            if (!Backup())
            {
                Return(false);
            }
            else
            {
                Thread t = new Thread(Output);
                t.Start();
            }
        }
        /// <summary>
        /// 备份数据
        /// </summary>
        protected abstract bool Backup();
        protected abstract void Output();

        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    }
}