我正在开发一个程序,它可以包含你添加到程序中的所有程序和游戏,所以你可以把它放在一个地方。该程序还具有搜索功能。
问题是,程序在第一次运行时,或者在重新启动计算机之后,需要一段时间才能加载。我很确定这是因为我使用Icon.ExtractAssociatedIcon从可执行文件中获取图标。
因此,如果XML文件中有很多内容(我将数据保存在XML文件中),那么加载程序需要一段时间,因为我提取了可执行图标。
我想出了一种方法让我更容易:
public static BitmapImage GetExeIcon(string path)
{
try
{
using (MemoryStream memory = new MemoryStream())
{
System.Drawing.Icon.ExtractAssociatedIcon(path).ToBitmap().Save(memory, System.Drawing.Imaging.ImageFormat.Png);
memory.Position = 0;
BitmapImage bitmapImg = new BitmapImage();
bitmapImg.BeginInit();
bitmapImg.StreamSource = memory;
bitmapImg.CacheOption = BitmapCacheOption.OnLoad;
bitmapImg.EndInit();
return bitmapImg;
}
}
catch (Exception)
{
MessageBox.Show("Are you sure it is a valid exe path?");
}
return null;
}
我承认,在使用模块中有一些东西我不明白,但我尽可能地去理解它。
负荷法:
public void Load()
{
if(File.Exists("ApplicationSaves\\apps.xml"))
{
using (XmlReader xmlReader = XmlReader.Create("ApplicationSaves\\apps.xml"))
{
string name = string.Empty;
string desc = string.Empty;
ApplicationItem.AppType type = ApplicationItem.AppType.Game;
string exePath = string.Empty;
while (xmlReader.Read())
{
switch (xmlReader.Name)
{
case "Name":
name = xmlReader.ReadElementContentAsString();
break;
case "Description":
desc = xmlReader.ReadElementContentAsString();
break;
case "Type":
type = (xmlReader.ReadElementContentAsString() == "Game" ? ApplicationItem.AppType.Game : ApplicationItem.AppType.Program);
break;
case "ExePath": //This is the last data it needs
exePath = xmlReader.ReadElementContentAsString();
GlobalManager.applicationList.Add(new ApplicationItem() { Name = name, Description = desc, ExePath = exePath, Type = type, Icon = GlobalManager.GetExeIcon(exePath) });
break;
}
}
}
}
}
您可以看到,我在结尾处使用了GlobalManager.GetExeIcon(exePath),在"ExePath“一词中,所以它对它添加的所有项都使用了ExePath。
最后一张图片,您可以看到我使用的图标是什么:-)您有什么建议,我如何减少加载时间?我知道我可以将提取出来的图标保存到一个文件夹中,如果它们存在的话,可以从那里加载它们,我想这会减少一些加载。但如果你知道更好的方法,我会很感激的:)
PS。对不起,如果读的太多了,我总是试着说得很具体,但是有很多的文字。如果你需要更多的信息,请告诉我。
你不需要读下面的内容,但可能有人读了它有一些建议:
我不会说我是C#新手(我大约两年前就开始了),也不是一般的编程。两年的C#和我仍然不知道如何/何时/为什么使用结构,接口和其他很酷的东西。
我一直在长时间休息,有时是因为我不知道该做什么,而失去动力是因为几个月后我觉得自己什么也没学到(有时我会因为觉得自己不够好或学习不够而失去动力)。
发布于 2015-09-18 14:22:28
另外,它看起来像是一个很酷的小应用程序。我不介意做你的测试员。完成后更新这个答案。
发布于 2015-09-18 14:13:54
要回答这个问题,您可能会考虑缓存图标,如果这是问题的话。另外,在你的屏幕截图中,你只有8个图标显示,所以你可能可以在后台加载其余的图标。如果您想变得更时尚,您可以异步加载所有东西,就像Visual的nuget包管理器一样。
其他注释: xml加载代码看起来有点奇怪--您不应该依赖于属性的顺序。我认为?是一个好的开始。
https://stackoverflow.com/questions/32654167
复制相似问题