ObservableCollection更改后,ListView无法更新

时间:2019-06-11 07:51:31

标签: c# wpf mvvm mvvm-light observablecollection

我已经尝试了几个小时,以使带有ObservableCollection的ListView正常工作。但是,没有运气。我还在这里和那里通读了一些帖子,尝试匹配,但仍然没有好处。请给我指出哪里出了问题。

基本上,我想做的是将逻辑从VM拆分到Helper类。并且此类中的逻辑将更新数据,但VM无法识别它。

我的问题是在复制文件功能中,作业状态不会更改视图数据。我尝试了Messenger.Default.Send(来自帮助程序)和Messenger注册(在VM中)来接受更改,但还是没有运气。

我正在使用MVVM Light,WPF,C#。

这是我的Model代码。

public class MyFile : ViewModelBase
{
    public string fullFileName { get; set; }
    public string fileName { get; set; }


    private string _jobStatus;
    public string jobStatus
    {
        get { return _jobStatus; }
        set { Set(ref _jobStatus, value); }
    }
}

这是我的帮助程序代码。

class FileHelper
{
    public List<MyFile> GetFileName(string dir)
    {
        List<MyFile> lstFiles = new List<MyFile>();

        foreach (var file in (new DirectoryInfo(dir).GetFiles()))
        {
            if (file.Name.ToLower().Contains("xls") && !file.Name.Contains("~$"))
                lstFiles.Add(new MyFile() { fullFileName = file.FullName, fileName = file.Name, jobStatus = "-" });
        }

        return lstFiles;
    }

    public bool CopyFiles(string destDir, List<MyFile> lstFiles)
    {
        try
        {
            int counter = 0;

            foreach (MyFile f in lstFiles)
            {
                f.jobStatus = "Copying";
                File.Copy(f.fullFileName, Path.Combine(destDir, f.fileName),true);
                f.jobStatus = "Finished";
                counter += 1;
                Console.WriteLine("M: " + DateTime.Now.ToString("hh:mm:ss") + "    " + counter);
                Messenger.Default.Send(counter, "MODEL");
            }

            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
            return false;
        }
    }
}

这是我的VM代码。

public class MainViewModel : ViewModelBase
{
    public ICommand CmdJob { get; private set; }



    private ObservableCollection<MyFile> fileList;
    public ObservableCollection<MyFile> FileList
    {
        get { return fileList; }
        set { Set(ref fileList, value); }
    }


    private string counter;
    public string Counter
    {
        get { return counter; }
        set { Set(ref counter, value); }
    }


    public MainViewModel()
    {
        Messenger.Default.Register<int>(this, "MODEL", UpdateCounter);

        CmdJob = new RelayCommand<object>(Action_Job);
        Counter = "0";
    }

    private void UpdateCounter(int bgCounter)
    {
        Counter = bgCounter.ToString();
        RaisePropertyChanged("FileList");
        Console.WriteLine("VM: " + DateTime.Now.ToString("hh:mm:ss") + "    " + Counter);
    }

    private void Action_Job(object tag)
    {
        if (tag == null || string.IsNullOrEmpty(tag.ToString()))
            return;

        switch (tag.ToString())
        {
            case "GET": GetFile();  break;
            case "COPY": CopyFile(); break;
        }
    }


    private void GetFile()
    {
        Counter = "0";
        List<MyFile> myFs = new FileHelper().GetFileName(@"C:\Test\Original\");
        FileList = new ObservableCollection<MyFile>(myFs);
    }

    private void CopyFile()
    {
        if (new FileHelper().CopyFiles(@"C:\Test\Destination\", fileList.ToList()))
            Messenger.Default.Send("Files copying finished", "VM");
        else
            Messenger.Default.Send("Files copying failed", "VM");
    }
}

这是我的XAML。

<ListView Grid.Row="0" Grid.Column="0" ItemsSource="{Binding FileList}" Margin="5,5,0,5" HorizontalAlignment="Left" VerticalAlignment="Stretch" ScrollViewer.VerticalScrollBarVisibility="Visible">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="File Name" Width="170" DisplayMemberBinding="{Binding fileName}" />
                <GridViewColumn Header="Status" Width="170" DisplayMemberBinding="{Binding jobStatus}" >
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

    <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical">
        <Button Content="Get file list" Tag="GET" Command="{Binding CmdJob}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Width="80" Height="25" Margin="0,50,0,50"/>
        <Button Content="Copy file" Tag="COPY" Command="{Binding CmdJob}" CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=Tag}"  Width="80" Height="25" />
        <Label Content="{Binding Counter}" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" Height="25" FontWeight="Bold" Foreground="Red" Margin="0,50,0,0"/>
    </StackPanel> 

我通读了一些帖子,它说要“更改”,ObservableCollection不会反映对View的更改。因此,我遵循这些帖子解决方案(以在Model类中使用Notify change),但对我不起作用。

对我来说,我的单个文件很大,因此我可以看到VM上没有更新。
如果使用较小的文件进行测试,您将看不到差异。

我尝试使用Messenger方法,该方法也不会在View上更新,但是我的VM可以毫无问题地接受传入消息。

2 个答案:

答案 0 :(得分:1)

不能同时更新UI和在同一线程上复制文件。

您应该在后台线程上执行复制,或者使用异步API,例如:

public async Task<bool> CopyFiles(string destDir, List<MyFile> lstFiles)
{
    try
    {
        int counter = 0;

        foreach (MyFile f in lstFiles)
        {
            f.jobStatus = "Copying";
            using (Stream source = File.Open(f.fullFileName))
            using (Stream destination = System.IO.File.Create(Path.Combine(destDir, f.fileName)))
                await source.CopyToAsync(destination);
            f.jobStatus = "Finished";
            counter += 1;
            Console.WriteLine("M: " + DateTime.Now.ToString("hh:mm:ss") + "    " + counter);
            Messenger.Default.Send(counter, "MODEL");
        }

        return true;
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: " + e.Message);
        return false;
    }
}

请注意,您必须修改方法的签名,才能使用.NET Framework 4.5和C#5中引入的async / await关键字。

您还应该等待异步方法"all the way"

private Task CopyFile()
{
    var fh = new FileHelper();
    if (await fh.CopyFiles(@"C:\Test\Destination\", fileList.ToList()))
        Messenger.Default.Send("Files copying finished", "VM");
    else
        Messenger.Default.Send("Files copying failed", "VM");
}

private async void Action_Job(object tag)
{
    if (tag == null || string.IsNullOrEmpty(tag.ToString()))
        return;

    switch (tag.ToString())
    {
        case "GET": GetFile();  break;
        case "COPY": await CopyFile(); break;
    }
}

答案 1 :(得分:0)

多亏了Clemens和mm8,我设法将其更改为异步并等待UI更新。
我认为我的实现方式仍然是合理的(与mm8相比)。

    private async void CopyFile()
    {
        var fh = new FileHelper();
        bool result = await fh.CopyFilesAsync(@"C:\Test\Destination\", fileList.ToList());

        if (result)
            Messenger.Default.Send("Files copying finished", "VM");
        else
            Messenger.Default.Send("Files copying failed", "VM");
    }

    public async Task<bool> CopyFilesAsync(string destDir, List<MyFile> lstFiles)
    {
        try
        {
            int counter = 0;

            await Task.Run(() =>
            {
                foreach (MyFile f in lstFiles)
                {
                    f.jobStatus = "Copying";
                    Thread.Sleep(500);
                    File.Copy(f.fullFileName, Path.Combine(destDir, f.fileName), true);
                    f.jobStatus = "Finished";
                    counter += 1;
                    Messenger.Default.Send(counter, "MODEL");
                    Thread.Sleep(500);
                }
            });

            return true;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: " + e.Message);
            return false;
        }
    }