如何在xamarin.forms中下载文件?

发布于 2025-02-12 10:54:01 字数 546 浏览 0 评论 0原文

大家好!我陷入了一个问题。 我们认为,由于某些原因,我们需要为游戏制作自己的启动器,该游戏将检查其版本并下载。我们的团队中没有Xamarin开发人员,所以我被要求自己尝试。卡在下载第二天。 我安装了Nuget插件下载Manager,试图使用它并且没有成功。一些印度人的视频也没有帮助我。 我可以做游戏,但完全吸引了移动开发。我应该怎么办?

我尝试过:

  1. 使用EventHandler制作IDownloader(URL,文件夹),这无助于
  2. 使用这些代码使用插件。将其链接到按钮。这也没有帮助我。您的帮助将挽救我的生命
void DownloadFile() 
{
    var downloadManager = CrossDownloadManager.Current;
    var file = downloadManager.CreateDownloadFile("someURIGodSaveMe");
    downloadManager.Start(file);
}

Good day everyone! I am stuck on a problem.
We decided that for some reasons we need to make our own launcher for a game which will check its version and download .apk of the game to install. We dont have a xamarin developer in our team, so I was asked to try do it myself. Stuck on downloading for the 2nd day.
I installed NuGet plugin DownloadManager, tried to use it and did not succeed. Some indian guy video's didn't help me either.
I can make a game, but totally suck in mobile development. What should I do?

I tried:

  1. Making IDownloader(url, folder) with EventHandler, that did not help
  2. Using plugin with these code. Linked it to a button. This did not help me too. Your help will save my life
void DownloadFile() 
{
    var downloadManager = CrossDownloadManager.Current;
    var file = downloadManager.CreateDownloadFile("someURIGodSaveMe");
    downloadManager.Start(file);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2025-02-19 10:54:01

您不需要任何特别的东西就可以在xamarin.forms中下载文件,良好的旧httpclient可以完成工作。

此代码将文件从URL下载到应用程序内部存储。有关emoverition.specialfolder.personal in xamarin.forms 在这里,有关emoveruct.pecialfolder.personal的更多信息。

private async Task DownloadApkAsync()
{
    var downloadedFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "myApp_v1.2.3.apk");

    var success = await DownloadFileAsync("https://www.myapp.com/apks/v1.2.3.apk", downloadedFilePath);

    if (success)
    {
        Console.WriteLine($"File downloaded to: {downloadedFilePath}");
    }
    else
    {
        Console.WriteLine("Download failed");
    }
}

private async Task<bool> DownloadFileAsync(string fileUrl, string downloadedFilePath)
{
    try
    {
        using var client = new HttpClient();

        var downloadStream = await client.GetStreamAsync(fileUrl);

        using var fileStream = File.Create(downloadedFilePath);

        await downloadStream.CopyToAsync(fileStream);

        return true;
    }
    catch (Exception ex)
    {
        //TODO handle exception
        return false;
    }
}

我还发现这个非常不错-en.html“ rel =“ nofollow noreferrer”>用于下载大文件的教程xamarin.forms 如果您想花哨(虽然没有尝试过)。它支持下载进度栏和取消。

You don't need anything special to download files in Xamarin.Forms, the good old HttpClient does the job.

This code downloads a file from a URL to the apps internal storage. More about Environment.SpecialFolder.Personal in Xamarin.Forms here.

private async Task DownloadApkAsync()
{
    var downloadedFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "myApp_v1.2.3.apk");

    var success = await DownloadFileAsync("https://www.myapp.com/apks/v1.2.3.apk", downloadedFilePath);

    if (success)
    {
        Console.WriteLine(
quot;File downloaded to: {downloadedFilePath}");
    }
    else
    {
        Console.WriteLine("Download failed");
    }
}

private async Task<bool> DownloadFileAsync(string fileUrl, string downloadedFilePath)
{
    try
    {
        using var client = new HttpClient();

        var downloadStream = await client.GetStreamAsync(fileUrl);

        using var fileStream = File.Create(downloadedFilePath);

        await downloadStream.CopyToAsync(fileStream);

        return true;
    }
    catch (Exception ex)
    {
        //TODO handle exception
        return false;
    }
}

I also found this very nice tutorial for downloading large files in Xamarin.Forms if you want to get fancy (haven't tried it though). It supports download progress bar and cancellation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文