C# WP7如何简化此代码或任何更好的方法?
有没有办法简化这段代码?或者以任何方式创建另一个类页面,以保持主页尽可能干净。我计划有数百个文本文件,因为我是一个新手,所以我不知道最好的方法。我正在尝试用Xbox 360成就指南制作一款应用程序,但每款游戏平均获得40项成就。(这是文本文件的分配)C# WP7如何简化此代码或任何更好的方法?,c#,windows-phone-7,C#,Windows Phone 7,有没有办法简化这段代码?或者以任何方式创建另一个类页面,以保持主页尽可能干净。我计划有数百个文本文件,因为我是一个新手,所以我不知道最好的方法。我正在尝试用Xbox 360成就指南制作一款应用程序,但每款游戏平均获得40项成就。(这是文本文件的分配) 我很想把这些文本文件放在互联网的某个地方,因为用户可以根据需要检索它们。你把事情复杂化了。如果您真的想将所有数据与应用程序捆绑在一起,您可以使用或,然后在内部对其进行反序列化 这也增加了代码的灵活性,因为从长远来看,当您决定添加更改时,直接使用硬编
我很想把这些文本文件放在互联网的某个地方,因为用户可以根据需要检索它们。你把事情复杂化了。如果您真的想将所有数据与应用程序捆绑在一起,您可以使用或,然后在内部对其进行反序列化
这也增加了代码的灵活性,因为从长远来看,当您决定添加更改时,直接使用硬编码值可能会带来一些麻烦。这只是匿名结构的一个示例,您还可以使用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;
}
您应该用实际的类填充列表框(我猜它是一个列表框)
public class Tutorial
{
public string Location { get; set; }
public string Title { get; set; }
public string Prefix { get; set; }
}
使用此类,使用这些类填充列表框。最好将此列表设置为xml/json格式,或者更好地从web抓取
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
你可能想考虑把它发布到:可能会得到一些额外的建议。我希望在网络中有一个JSON文件,但是不要在哪里和如何检索到我的应用程序。你可以在任何地方托管它(有很多选择),然后你可以使用像WebClient()这样的东西来获得它。
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
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;
}