有没有办法简化这段代码?或者以任何方式创建另一个类页面页面以尽可能保持主页面清洁。我打算有数百个文本文件,因为我是新手,我不是最好的方法来解决这个问题。我正在尝试使用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";
}
}
我希望在互联网上的某个地方有这个文本文件,因为用户可以根据需要检索它们。
答案 0 :(得分:0)
你太复杂了。如果您确实希望将所有数据与应用程序捆绑在一起,则可以serialize it to XML或JSON然后在内部对其进行反序列化。
这也为您的代码增加了灵活性,因为当您决定添加更改时,直接使用像您这样的硬编码值可能会在长期内引起一些麻烦。
答案 1 :(得分: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;
}
答案 2 :(得分: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;
}