WPF - 项目样例

WPF - 项目样例

 1. 创建项目:

参考:https://www.cnblogs.com/1285026182YUAN/p/18462396

 

2. 修改App.xaml

<Application x:Class="ModelFileMigrate.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:ModelFileMigrate">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary>
                    <local:Bootstrapper x:Key="bootstrapper"/>
                </ResourceDictionary>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

 

3. 根目录下 创建Bootstrapper.cs文件

using Caliburn.Micro;
using ModelFileMigrate.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ModelFileMigrate
{
    public class Bootstrapper : BootstrapperBase
    {
        public Bootstrapper()
        {
            Initialize();
        }

        protected override async void OnStartup(object sender, StartupEventArgs e)
        {
            await DisplayRootViewForAsync<ShellViewModel>();
        }
    }
}

 

 

4. 创建 views文件夹 ,创建 ShellView.xaml 文件

<Window x:Class="ModelFileMigrate.Views.ShellView"
        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:ModelFileMigrate.Views"
        xmlns:cal="http://www.caliburnproject.org"
        mc:Ignorable="d"
        Title="模型迁移" Height="620" Width="800">
    <StackPanel >
        <StackPanel Orientation="Horizontal">
            <Label Margin="10" Width="80">目标路径:</Label>
            <TextBox Margin="10" Width="620" Text="{Binding Path=PathTarget}"></TextBox>
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <Label Margin="10" Width="80">源路径:</Label>
            <TextBox Margin="10" Width="620" Text="{Binding Path=PathSource}"></TextBox>
        </StackPanel>

        <StackPanel Orientation="Horizontal">
            <Label Margin="10" Width="80">临时路径:</Label>
            <TextBox Margin="10" Width="620" Text="{Binding Path=PathTemp}"></TextBox>
        </StackPanel>

        <StackPanel Orientation="Horizontal">

            <StackPanel Orientation="Horizontal" Width="260">
                <Label Margin="10" Width="100" Foreground="red" FontSize="16">总数量:</Label>
                <TextBox Margin="10" Width="80" Foreground="red" FontSize="16" Background="White" BorderBrush="White" Text="{Binding Path=ConAll}" IsReadOnly="True" ></TextBox>
            </StackPanel>

            <StackPanel Orientation="Horizontal" Width="260">
                <Label Margin="10" Width="80" Foreground="Green" FontSize="16">移动数量:</Label>
                <TextBox Margin="10" Width="80" Foreground="Green" FontSize="16" Background="White" BorderBrush="White"  Text="{Binding Path=ConWait}" IsReadOnly="True" ></TextBox>

            </StackPanel>
            <StackPanel Orientation="Horizontal" Width="260">
                <Label Margin="10" Width="80" Foreground="red" FontSize="16">剩余数量:</Label>
                <TextBox Margin="10" Width="80" Foreground="Green" FontSize="16" Background="White" BorderBrush="White"  Text="{Binding Path=ConComplete}" IsReadOnly="True" ></TextBox>

            </StackPanel>
        </StackPanel>

        <StackPanel Orientation="Horizontal">

            <Button x:Name="btn_start" Margin="10" Height="29" Width="72" Background="#FF02F102" 
                    cal:Message.Attach="[Event Click]=[Action MoveStart()]" IsEnabled="{Binding ButtonEnabled}">Start</Button>
            <Button x:Name="btn_stop"   Margin="10"  Height="29" Width="72" Background="#FFFFDE00"
                    cal:Message.Attach="[Event Click]=[Action MovePause()]" >Pause</Button>


        </StackPanel>

        <StackPanel Orientation="Horizontal" >
            <ListView  Margin="10" Height="320" Width="750"   ItemsSource="{Binding LvDataList}" Grid.Row="1" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding}" />
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

        </StackPanel>

    </StackPanel>

</Window>

 

 

5. 创建 ViewModels 文件夹,创建 ShellViewModel.cs 文件

using Caliburn.Micro;
using ModelFileMigrate.Auxiliary;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ModelFileMigrate.ViewModels
{
    public class ShellViewModel : Screen, INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }



        private double _conAll = 0;
        public double ConAll
        {
            get { return _conAll; }
            set { _conAll = value; NotifyOfPropertyChange(); }
        }


        private double _conWait = 0;
        public double ConWait
        {
            get { return _conWait; }
            set { _conWait = value; NotifyOfPropertyChange(); }
        }


        private double _conComplete = 0;
        public double ConComplete
        {
            get { return _conComplete; }
            set { _conComplete = value; NotifyOfPropertyChange(); }
        }

        private string _pathTarget = @"D:\\aab";
        public string PathTarget
        {
            get { return _pathTarget; }
            set { _pathTarget = value; NotifyOfPropertyChange(); }
        }

        private string _pathSource = @"D:\\aaa";
        public string PathSource
        {
            get { return _pathSource; }
            set { _pathSource = value; NotifyOfPropertyChange(); }
        }

        private string _pathTemp= @"D:\\aac";
        public string PathTemp
        {
            get { return _pathTemp; }
            set { _pathTemp = value; NotifyOfPropertyChange(); }
        }

        private ObservableCollection<string> _lvDataList = new ObservableCollection<string>();
        public ObservableCollection<string> LvDataList
        {
            get { return _lvDataList; }
            set { _lvDataList = value; OnPropertyChanged(); }
        }


        private bool _buttonEnabled = true;
        public bool ButtonEnabled
        {
            get { return _buttonEnabled; }
            set
            {
                _buttonEnabled = value;
                OnPropertyChanged(nameof(ButtonEnabled));
            }
        }

        public void MoveStart()
        {
            ButtonEnabled = false; // 按钮被点击后设置为不可用

            var InsertLvInfo = (string str) => { LvDataList.Insert(0, str); };
            var SetConAll = (int val) => { ConAll = val; };
            var SetConWait = (int val) => { ConWait = val; };
            var SetConComplete = (int val) => { ConComplete = val; };          
            new FileOperation().MoveStart(PathSource,PathTarget,PathTemp, InsertLvInfo, SetConAll,SetConWait,SetConComplete);             
        }
       
       public void MovePause() { }
    }
}

 

 

 6. 创建 文件夹 Auxiliary,创建 文件 FileOperation.cs 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ModelFileMigrate.Auxiliary
{
    public class FileOperation
    {

        public void MoveStart(string dirPathSource, string dirPathTarget, string dirPathTemp, Action<string> SetInfo, Action<int> SetConAll, Action<int> SetConWait, Action<int> SetConComplete)
        {
            SetInfo("开始获取目标文件夹文件列表:" + dirPathTarget);

            var ccc = new DirectoryInfo(dirPathTarget);

            var targetFileAll = new DirectoryInfo(dirPathTarget).GetFiles();
            targetFileNameList = targetFileAll.Select(t => t.Name).ToList();
            SetInfo("获取到文件数量:" + targetFileNameList.Count);

             

            SetInfo("开始获取文件夹文件:" + dirPathSource);
            var fileArr = new DirectoryInfo(dirPathSource).GetFiles(); 
            SetInfo("获取到文件数量:" + fileArr.Length);



            Task.Run(() =>
            {
                for (int i = 0; i < 100; i++)
                {
                    Application.Current.Dispatcher.Invoke(() =>
                    {
                        SetInfo("AAAAAAAAA" + i);
                    });
                    Thread.Sleep(100);
                }
            });

        }


        private List<string> targetFileNameList { get; set; }

    }
}

 

 

 7. 运行

 

 

 

 

 

 

 

 

 

 

end.

 

posted @ 2024-10-14 18:30  无心々菜  阅读(54)  评论(0编辑  收藏  举报