2014-12-05 64 views
0

所以,我正在制作一个从西班牙字典中读取的程序。它抓住一个随机单词。我需要打开一个字符串的Unicode,因为会有像“é”这样的字符,显然需要以不同的方式对字符串进行编码。当我打开的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 
+0

你为什么不尝试一下呢? – pquest 2014-12-05 20:04:56

+0

@pquest会做。 – 2014-12-05 20:05:19

+0

@pquest踩踏虽然没有奏效。 – 2014-12-05 20:07:01

回答

1

下面两行可以很容易地抛出一个索引超出范围例外的未来时:

 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]这个词永远不会被随机选择的,它看起来是错误的。

+0

这仍然不适用于Unicode,我的程序不会给出错误,但不会启动。 – 2014-12-05 21:12:01

+0

如果这是真的,那么你应该在Visual Studio中进行调试,[抛出异常时中断](http://msdn.microsoft.com/zh-cn/library/d14azbfh.aspx)。然后找出问题出现的确切位置,并对其进行调试。如果您仍不确定发生了什么,您可以在此处发布详情。 – dbc 2014-12-05 21:29:45

相关问题