I'm developing a C#/Xaml UWP text editing app that uses the RichEditBox control for editing text files from. However, I've noticed when I load larger files (~1mb and above, perhaps even less) that the control struggles in a couple of key areas: 1) it takes a while to load the contents of the file and 2) once it finally has, scrolling is very jerky and input into the file is neither smooth nor responsive.
The closest answer I've come across is this (what it describes is exactly the problem I'm having) but it doesn't appear to be applicable to a UWP app.
EDIT:
When I open a file, the noteworthy code to share is:
_currentFile.Content = await readFile(file);
_currentFile._richeditbox.Document.SetText(TextSetOptions.None, _currentFile.Content);
This is the readFile() function, which was helped via https://social.msdn.microsoft.com/Forums/sqlserver/en-US/0f3cd056-a2e3-411b-8e8a-d2109255359a/uwpc-reading-ansi-text-file?forum=wpdevelop:
private async Task<string> readFile(StorageFile file)
{
string charSet = null;
try
{
await Task.Run(() =>
{
try
{
using (FileStream fs = System.IO.File.OpenRead(file.Path))
{
Ude.CharsetDetector cdet = new Ude.CharsetDetector();
cdet.Feed(fs);
cdet.DataEnd();
charSet = cdet.Charset;
}
}
catch (Exception ex)
{
}
});
}
catch (Exception e)
{
}
Classes.File._lastEncoding = charSet;
IBuffer buffer = await FileIO.ReadBufferAsync(file);
DataReader reader = DataReader.FromBuffer(buffer);
byte[] fileContent = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(fileContent);
string content = "";
if (charSet == "windows-1252")
{
content = Encoding.GetEncoding("iso-8859-1").GetString(fileContent, 0, fileContent.Length);
}
else if (charSet == "UTF-16LE")
{
content = Encoding.Unicode.GetString(fileContent, 0, fileContent.Length);
}
else if (charSet == "UTF-16BE")
{
content = Encoding.BigEndianUnicode.GetString(fileContent, 0, fileContent.Length);
}
else
{
content = Encoding.UTF8.GetString(fileContent, 0, fileContent.Length);
}
return content;
}
When debugging, there's absolutely no delay when the readFile() function is called; it gets executed very quickly.
I've tried loading a 1.14mb text file. It takes some time to load, and when it does although the scrollbar height indicates all of it has loaded, it actually doesn't display any text from line 2098 onwards (there are 3,771 lines in total); it stops at this line consistently even on subsequent reloads.
See picture:
As you can also see the last line that is visible gets mashed up with the line above it.
For reference, the file I'm having the problems with can be downloaded from here (but to be clear it's a problem with all text files of a similar size, and possibly much less even): http://mtrostyle.net/appytext/testfile.txt.