3

我知道这个主题有很多线程,我尝试将这个线程从 VB 实现(翻译)为 C#。

Table fileContainer = {string FileName, binary File}

这是我的尝试:

partial void FileContainersAddAndEditNew_Execute()
{
    Dispatchers.Main.BeginInvoke(() =>
    {

        OpenFileDialog openDialog = new OpenFileDialog();

        if (openDialog.ShowDialog() == true)
        {
            using (System.IO.FileStream fileData = openDialog.File.OpenRead())
            {
                long fileLen = fileData.Length;

                if (fileLen > 0)
                {
                    Byte[] fileBArray = new Byte[fileLen];

                    fileData.Read(fileBArray, 0, fileLen);
                    fileData.Close();

                    FileContainer fc = this.FileContainers.AddNew();

                    fc.File = fileBArray;
                    fc.FileName = openDialog.File.Extension.ToString().ToLower();

                }
            }
        }

    });
}

但是代码在这一行失败了:

 FileContainer fc = this.FileContainers.AddNew();

出现此错误:

IVisualCollection<T>.AddNew() should not be called from UI Thread.

我有点困惑。我认为:

 Dispatchers.Main.BeginInvoke(() =>

阻止了这种情况的发生。还是我做错了?

我注意到的另一件事是 VB 代码使用:

filenLen-1

但我试图做到这一点越界。他们也不会将其转换为 anint.Read不会将 along作为参数。

4

2 回答 2

4

openFileDialog.ShowDialog() does not return a bool value and cannot be used in an if statement like that. openFileDialog.ShowDialog() will open the dialog. As far as I know the execution pauses until you close the dialog and there's no reason to check if it's open or not.

filenLen-1

using (System.IO.FileStream fileData = openDialog.File.OpenRead());

long fileLen = fileData.Length;

should be

filenLen--;

using (System.IO.FileStream fileData = System.IO.File.OpenRead(openDialog.FileName))

int fileLen = int.Parse(fileData.Length.ToString());

fileData.Read(fileBArray, 0, fileLen); needs fileLen to be an integer. fileData returns a long for a reason though and this might cause problems.

As for the invoke question I'd have to know exactly what you're trying to do to be able to help you. Perhaps we can avoid invoking.

于 2013-04-12T10:27:18.333 回答
3

我在这里发布我更新的代码,以免为未来的读者弄乱我的原始代码。这应该有效;

    partial void FileContainersAddAndEditNew_Execute()
    {

        var supportedFiles = "*.*";
        Dispatchers.Main.BeginInvoke(() =>
        {
            OpenFileDialog openDialog = new OpenFileDialog();
            openDialog.Filter = "Supported files|" + supportedFiles;

            if (openDialog.ShowDialog() == true)
            {
                using (System.IO.FileStream fileData = openDialog.File.OpenRead())
                {
                    long fileLen = fileData.Length;

                    if (fileLen > 0)
                    {
                        Byte[] fileBArray = new Byte[fileLen--];

                        fileData.Read(fileBArray, 0, (int)fileLen);
                        fileData.Close();
                        var filename = openDialog.File.ToString().ToLower();

                        this.FileContainers.Details.Dispatcher.BeginInvoke(() =>
                        {
                            FileContainer fc = this.FileContainers.AddNew();
                            fc.File = fileBArray;
                            fc.FileName = filename;
                        });

                    }
                }
            }
        }); 

    }
于 2013-04-12T11:35:56.617 回答