0

有没有办法简化这段代码?或以任何方式创建另一个类页面以保持主页尽可能干净。我计划拥有数百个文本文件,因为我是新手,所以我不是解决这个问题的最佳方法。我正在尝试制作一个带有 Xbox 360 成就指南的应用程序,但每场游戏平均大约有 40 个成就。(这是分配的文本文件)。

if (selectedIndex == "0")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/StoryRelated.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = "Story Complete [MEDIUM]\n" + Environment.NewLine + sr.ReadToEnd();
        Title.Text = "Feel The Payne";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "1")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/StoryRelated.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = "Story Complete [HARD]\n" + Environment.NewLine + sr.ReadToEnd();
        Title.Text = "Serious Payne";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "2")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/StoryRelated.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = "Story Complete [OLD SCHOOL]\n" + Environment.NewLine + sr.ReadToEnd();
        Title.Text = "Maximum Payne";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "3")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/TextFile1.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "Payne In The Ass";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "4")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/StoryRelated.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "Part I Complete";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "5")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/StoryRelated.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "Part II Complete";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "6")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/StoryRelated.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "Part III Complete";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "7")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/TextFile2.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "A New York Minute";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "8")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/TextFile3.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "The Shadows Rushed Me";
        AppBarMenuDisable.IsEnabled = false;
    }
}
else if (selectedIndex == "9")
{
    var Tutorial = Application.GetResourceStream(new Uri("Resources/Games/MaxPayne3/TextFile4.txt", UriKind.Relative));

    using (Stream Text = Tutorial.Stream)
    {
        StreamReader sr = new StreamReader(Text);
        Guide.Text = sr.ReadToEnd();
        Title.Text = "Out The Window";
        YouTubeLink.URL = "http://www.youtube.com/watch?v=lRg6ygA-M_Y";
    }
}

我希望将这些文本文件放在 Internet 的某个位置,因为用户可以根据需要检索它们。

4

3 回答 3

0

这只是一个匿名结构的例子,你也可以用 xml + linq 扩展它

var actions = new[]
{
 new {
  index = 0, 
  uri = "Resources/Games/MaxPayne3/StoryRelated.txt", 
  guide = "Story Complete [MEDIUM]\n\n", 
  title = "Feel The Payne", 
  appBar = false, 
  youtube = ""
 },

 new {
  index = 7, 
  uri = "Resources/Games/MaxPayne3/TextFile2.txt", 
  guide = "", 
  title = "A New York Minute", 
  appBar = false, 
  youtube = ""
 },

 new {
  index = 9, 
  uri = "Resources/Games/MaxPayne3/TextFile4.txt", 
  guide = "", 
  title = "Out The Window", 
  appBar = true, 
  youtube = "http://www.youtube.com/watch?v=lRg6ygA-M_Y"
 },
};

var actionQuery = actions.Where(a => a.index == selectedIndex).ToArray();

if (actionQuery.Length == 0) throw new Exception("Index not found: " + selectedIndex);
if (actionQuery.Length > 1) throw new Exception("Duplicate entries found: " + selectedIndex);

var action = actionQuery[0];

var Tutorial = Application.GetResourceStream(new Uri(action.uri, UriKind.Relative));

using (Stream Text = Tutorial.Stream)
{
 StreamReader sr = new StreamReader(Text);
 Guide.Text = action.guide + sr.ReadToEnd();
 Title.Text = action.title;
 AppBarMenuDisable.IsEnabled = action.appBar;
 if (action.youtube != "") YouTubeLink.URL = action.youtube;
}
于 2012-07-10T21:25:13.743 回答
0

你应该用实际的类填充 ListBox(我猜它是一个 ListBox)

public class Tutorial
{
    public string Location { get; set; }
    public string Title { get; set; }
    public string Prefix { get; set; }
}

使用此类,用这些类填充 ListBox。最好将此列表采用 xml/json 格式,或者更好地从网络上获取。

List<Tutorial> tutorials = new List<Tutorial()
{
    new Tutorial 
    { 
        Location = "Resources/Games/MaxPayne3/StoryRelated.txt",
        Title = "Feel the Payne",
        Prefix = "Story Complete [MEDIUM]\n"
    }
    // add more
};
ListBox.ItemsSource = tutorials; // better to do binding to this property 

然后在 selectionchanged 事件中

Tutorial tutorial = ListBox.SelectedItem as Tutorial;
if(tutorial == null) return;

var item = Application.GetResourceStream(new Uri(tutorial.Location, UriKind.Relative));

using (Stream Text = Tutorial.Stream)
{
    StreamReader sr = new StreamReader(Text);
    Guide.Text = tutorial.Prefix + sr.ReadToEnd();
    Title.Text = tutorial.Tile;
    AppBarMenuDisable.IsEnabled = false;
}
于 2012-07-10T21:30:13.527 回答
0

你把事情复杂化了。如果您真的想将所有数据与应用程序捆绑在一起,您可以将其序列化为 XMLJSON,然后在内部对其进行反序列化。

这也为您的代码增加了灵活性,因为从长远来看,当您决定添加更改时,像您一样直接使用硬编码值可能会导致一些麻烦。

于 2012-07-10T21:18:34.357 回答