打开字符串 Unicode 编码时出现错误
本文关键字:错误 编码 字符串 Unicode | 更新日期: 2023-09-27 18:33:26
所以,我正在制作一个从西班牙语词典中读取的程序。它抓住一个随机的单词。我需要为字符串打开 Unicode,因为会有像"é"这样的字符,这显然需要对字符串进行不同的编码。当我为string[]
打开 Unicode 时,它会给我以下错误消息:
System.Windows.Markup.XamlParseException was unhandled
Message='The invocation of the constructor on type 'WordADay.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'.
Source=PresentationFramework
LineNumber=4
LinePosition=9
StackTrace:
at System.Windows.Markup.XamlReader.RewrapException(Exception e, IXamlLineInfo lineInfo, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
at System.Windows.Application.LoadBamlStreamWithSyncInfo(Stream stream, ParserContext pc)
at System.Windows.Application.LoadComponent(Uri resourceLocator, Boolean bSkipJournaledProperties)
at System.Windows.Application.DoStartup()
at System.Windows.Application.<.ctor>b__1(Object unused)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Threading.DispatcherOperation.Invoke()
at System.Windows.Threading.Dispatcher.ProcessQueue()
at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.Run()
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at System.Windows.Application.Run()
at WordADay.App.Main() in C:'Documents and Settings'admin'My Documents'WordADay'WordADay'obj'x86'Debug'App.g.cs:line 0
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.IndexOutOfRangeException
Message=Index was outside the bounds of the array.
Source=WordADay
StackTrace:
at WordADay.MainWindow.newWord() in C:'Documents and Settings'admin'My Documents'WordADay'WordADay'MainWindow.xaml.cs:line 132
at WordADay.MainWindow..ctor() in C:'Documents and Settings'admin'My Documents'WordADay'WordADay'MainWindow.xaml.cs:line 33
InnerException:
顺便说一句,这对我来说完全是古怪的,我无法理解。只是问,当来自此代码时这意味着什么?
#region Setup
Random word = new Random();
string[] lines = File.ReadAllLines(dictionarypath, Encoding.Unicode);
int randomword = word.Next(1, lines.Count());
string[] excludedlines;
if (!File.Exists(path))
{
File.Create(path);
}
excludedlines = File.ReadAllLines(path);
string chosenWord = lines[randomword];
#endregion
#region Logic
if (excludedlines.Count() == 58110)
{
File.WriteAllText(path, "");
}
if (excludedlines.Contains(chosenWord))
{
while (excludedlines.Contains(chosenWord))
{
randomword = word.Next(58110);
chosenWord = lines[randomword];
}
File.AppendAllText(path, chosenWord + Environment.NewLine);
excludedlines = File.ReadAllLines(path);
label1.Content = chosenWord;
}
else
{
File.AppendAllText(path, chosenWord + Environment.NewLine);
excludedlines = File.ReadAllLines(path);
label1.Content = chosenWord;
}
#endregion
以下两行很容易引发索引超出范围的异常:
randomword = word.Next(58110);
chosenWord = lines[randomword];
当然,以下内容更有意义:
randomword = word.Next(lines.Length);
chosenWord = lines[randomword];
另外,以下行:
int randomword = word.Next(1, lines.Count());
应该是
int randomword = word.Next(lines.Length);
在您的版本中,lines[0]
的单词永远不会随机选择,这看起来是错误的。