【问题标题】:How to maintain a download list (code-behind and UI)?How to maintain a download list (code-behind and UI)?
【发布时间】:2022-12-01 12:08:37
【问题描述】:

I am currently building an app in which the main goal is to download files from their download links, pretty easy, isn't it?

The app downloads the files in the background, and has a dedicated page to display the current Downloads operations. However, I have no idea on how to store it, and came with a few ideas:

  • Have a JSON file, which store a list of what's downloading and their current state (including the bytes downloaded), and the file is rewritten each time there is some progress. This seems awful to me, since it involves a lot of read/write operations in addition to the file being downloaded.
  • Store the list of Download Operations in a List directly in App class, so I can retrieve them at any time just using (Application.Current as App).Downloads.

I do not really know what would be the best way to maintain my download list, and being able to display it to the end-user, and/or accessing it from my other pages. I would also like to keep the operation saved somewhere so the user is able to resume the operation later.

【问题讨论】:

    标签: c# xaml winui


    【解决方案1】:

    Assuming you don't intend toexitthe app and then re-open it and have progress still visible, your App class idea isn't as awful as you might think, though I wouldn't use the App class specifically.

    What you're looking for is called a singleton. It's a design pattern and there are a lot of good articles about it. (Wiki to get you started if you're not familiar with it).

    Typically I'd suggest creating a class and registering it as a singleton with your dependency injection container (further reading here if you're not familiar with that). If you are familiar with DI or feel adventurous, I would place your download progress logic into a DownloadManager class registered as a singleton.

    If you don't have a DI container set up though, that's okay you can still do a makeshift singleton class using statics. A common pattern for that looks like this:

    public class MySingleton
    {
        private static MySingleton _instance;
        public static MySingleton Instance
        {
            get
            {
                _instance ??= new MySingleton();
                return _instance;
            }
        }
    }
    

    This allows you to access an instance of the MySingleton class by calling MySingleton.Instance whenever you want it.

    What happens is the first time the Instance getter is called, the ??= checks if _instance is null or not, if it's null, it constructs a new instance of MySingleton, otherwise it simply returns the value already set.

    This ensures that only one instance of MySingleton ever gets created for the life of your application.

    You can create a singleton DownloadManager where you handle storing all the values of your active downloads which can be fetched and updated at any time simply by accessing DownloadManager.Instance.

    Technically speaking, this isn't much different from the App solution you mentioned, but it doesn't pollute your App class with download code and allows you to keep your download manager code self-contained where you can easily modify it and test it in isolation.

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-12-01
      • 2022-12-19
      • 2022-12-02
      • 2022-05-17
      • 2022-12-28
      • 2022-12-01
      • 2022-12-02
      • 2022-12-02
      相关资源
      最近更新 更多