I know there are many threads on the subject, and I have try to implement (translate) this one into C# from VB.
Table fileContainer = {string FileName, binary File}
Here is my try:
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();
}
}
}
});
}
But the code fails on this line:
FileContainer fc = this.FileContainers.AddNew();
With this error:
IVisualCollection<T>.AddNew() should not be called from UI Thread.
I'm a bit confused. I thought the:
Dispatchers.Main.BeginInvoke(() =>
prevented that from happening. Or am I doing it the wrong way?
Another thing I have notice is that the VB code uses the:
filenLen-1
but I get out of bounds trying to do that. They also don't cast it to an int
but the .Read
doesn't take a long
as an argument.