1

我有以下代码无法正常工作:

 KeyValuePair<string, object> item =
        (KeyValuePair<string, object>)e.AddedItems[0];

      string str = (string)item.Key;

      switch (str)
      {
          case "?":
              this.NavigationService.Navigate(new Uri("/Shared/About/AboutPage.xaml?appName=Elements",
                UriKind.Relative));
              break;
          default:
              this.NavigationService.Navigate(new Uri("/DetailsPage.xaml?url=" +
                HttpUtility.UrlEncode((item.Value as Element).Url.LocalPath),
                UriKind.Relative));
              break;
      }

我有一个名为“item”的对象的键/值对,如果键值“?”,我想使异常触发不同的代码块。被发现。上面我使用了一个 switch 语句来尝试执行另一个代码块 if "?" 找到作为键,否则执行默认代码块。但是现在我在运行上述代码时触发了以下豁免:

// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
    if (System.Diagnostics.Debugger.IsAttached)
    {
        // An unhandled exception has occurred; break into the debugger
        System.Diagnostics.Debugger.Break();
    }
}

我想知道是否有人可以给我一个关于为什么会发生这种情况的线索。任何帮助表示赞赏,谢谢。

添加于 2013 年 12 月 27 日:

下面的代码块来自一个包含一长串网站的文件,但最后一行不是网站,而是我想要调用的程序中的一个文件。

using System;

namespace ...
{
  public class Data
  {
    public static readonly Element[] Elements = {
        new Element("Hydrogen [H] 1", new Uri("http://chemistry.about.com/od/elementfacts/a/hydrogen.htm")),
        new Element("Helium [He] 2", new Uri("http://chemistry.about.com/od/elementfacts/a/helium.htm")),

        ...

        new Element("? About", new Uri("/Shared/About/AboutPage.xaml?appName=Elements", UriKind.Relative))
    };
  }
}

最后一行new Element("? About", new Uri("/Shared/About/AboutPage.xaml?appName=Elements", UriKind.Relative))是保存程序内文件链接的行。这里的第一个代码块与 switch 语句应该确定?这行代码中的,但我不确定我做对了。第二个代码块是我测试程序时调试器抛出的。

4

0 回答 0