2014-12-05 52 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

相關問題