在 C# Windows 应用程序中保存控件 [英] Saving Controls in C# Windows Application

查看:44
本文介绍了在 C# Windows 应用程序中保存控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,用于从视频文件创建按钮列表.

I am creating an application that creates a list of buttons from video files.

生成每个按钮以在单击时打开文件,然后我使用一个开放的 api (omdb.com) 来获取每个按钮的电影海报.但是,每次启动应用程序时执行此过程不仅会变慢,而且还会对网站造成不必要的负载.

Each button is generated to open the file on click, and then I use an open api (omdb.com) to get the movie posters for each one. However, not only is doing this process each time you launch the application slow, but it is also an unnecessary load on the website.

在后端动态生成控件后,保存控件的最有效方法是什么.

What is the most efficient way to save a control after it is dynamically generated in the backend.

到目前为止,我已经尝试使用 Properties.Settings,但没有任何运气.

So far I have tried using Properties.Settings, but without any luck.

SettingsProperty newSetting = new SettingsProperty("Movie_Button");
newSetting.Name = "Movie_Button_" + *an int that I generate*.ToString();
newSetting.Attributes.Add(Width, 200);
//Carry on setting attributes

到目前为止,这个过程还没有产生任何结果,即使只是尝试创建一个简单的字符串的 SettingsProperty 也是如此.我试过使用和不使用 Properties.Settings.Default.Save().

So far this process hasn't yielded any results, even when just trying to create a SettingsProperty that's just a simple string. And I have tried with and without Properties.Settings.Default.Save().

有没有办法保存在运行时创建的控件,以便在下次启动应用程序时继续使用?

如果没有办法做到这一点,那么有人可以帮我创建新的 SettingsProperty 来保存一个字符串,或者可能是一个我可以使用的 XML.

If there is no way to do this, then can someone please help me create the new SettingsProperty to hold a string, or possibly an XML that I could use.

推荐答案

由于我找不到保存控件的方法,我决定将图像保存在一个新文件夹中,然后创建一个包含路径的 XML图片(以及一些附加信息,如标题、年份等).

Since I couldn't find a way to save a control, I decided to instead save the images in a new folder and then create an XML containing the path to the image (and some additional information such as title, year, etc.).

为了创建 XML,我使用了 XmlDocument doc = new XmlDocument(); 方法,将图像下载到 exe 路径下的文件夹(使用 Application.ExecutablePath 到获取路径),然后 WebClient webclient = new WebClient();webclient.DownloadFile(netPath, localPath) 下载每个图像.

To create the XML I used the XmlDocument doc = new XmlDocument(); method, downloaded the images to a folder at the exe path (using Application.ExecutablePath to get the path), then WebClient webclient = new WebClient(); and webclient.DownloadFile(netPath, localPath) to download each image.

然后在表单加载时,我使用 XML 中的信息重新生成按钮.

Then on form load I regenerate the buttons using the info from the XML.

这个方法比我原来的方法要快得多,这个方法是为图像 url 发出多个 web 请求或调用 api.

This method is much faster than my original method of making multiple web requests for the image url or calls to the api.

这篇关于在 C# Windows 应用程序中保存控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆