PgInstallVm.cs 3.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 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
using GalaSoft.MvvmLight.Command;
using Install.Core;
using Install.Core.Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;

namespace Install.View
{
    public class PgInstallVm : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public ObservableCollection<ListItem> Items { get; } = new ObservableCollection<ListItem>();

        public string InstallPath { get; set; }


        public Dictionary<string, BitmapSource> Icons { get; private set; }

        public RelayCommand NextCmd { get; private set; }
        public RelayCommand SelectPathCmd { get; private set; }
        public RelayCommand DownloadCmd { get; private set; }

        InstallWizard installWizard;
        public InstallWizard InstallWizard => installWizard;

        public PgInstallVm() 
        {
            NextCmd = new RelayCommand(Next);
            SelectPathCmd = new RelayCommand(SelectPath);
            DownloadCmd = new RelayCommand(Download);
        }
        private void Download()
        {
            //打开 下载进度界面
            WdDownload w = new WdDownload();
            w.Init();
            w.ShowDialog();

        }


        public void Init() 
        {
潘栩锋's avatar
潘栩锋 committed
53
            installWizard = App.Resolve<InstallWizard>();
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105

            Icons = installWizard.Icons;

            foreach (var installpack in installWizard.ToInstall) 
            {
                Items.Add(new ListItem() { Item = installpack, IsSelected = installpack.IsDefaultSelected });
            }

            InstallPath = installWizard.InstallPacks.DefaultInstallPath;
        }

        private void SelectPath()
        {
            var dbd = new System.Windows.Forms.FolderBrowserDialog();
            dbd.SelectedPath = InstallPath;

            if (dbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                InstallPath = dbd.SelectedPath;
            }
        }

        private async void Next()
        {
            //筛选出选择的软件
            var installPacks = from item in Items where item.IsSelected select item.Item as InstallPack;

            //检测有没安装被选
            if (installPacks.Count() == 0)
            {
                //没有任何一个被选择
                System.Windows.MessageBox.Show("没有任何一个被选择", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            //检测路径
            if (!System.IO.Directory.Exists(InstallPath))
            {
                try
                {
                    System.IO.Directory.CreateDirectory(InstallPath);

                }
                catch
                {
                    System.Windows.MessageBox.Show(InstallPath + " 路径不能创建,重新选择", "异常", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }

            PgProgress p = new PgProgress();
            p.Init();
潘栩锋's avatar
潘栩锋 committed
106
            App.Resolve<NavigationService>().Navigate(p);
107 108 109 110 111
            await installWizard.Install(InstallPath, installPacks);
        }
    }
    
}