MicrosoftDocs / winrt-api

WinRT reference content for developing Microsoft Universal Windows Platform (UWP) apps
Creative Commons Attribution 4.0 International
231 stars 500 forks source link

I tried to create a toast with ProgressBar. I tried using the method in Microsoft Learn but it didn't work. #2421

Open WillowTree1184 opened 1 year ago

WillowTree1184 commented 1 year ago

I tried to create a toast with ProgressBar. I tried using the method in Microsoft Learn but it didn't work.

Please let me know what I should do.

This is my code:

public async Task Download(MinecraftDownloadInfomation DownloadInfomation)
{
    ToastContent DownloadToastContent = new ToastContentBuilder().AddText("正在下载 " + DownloadInfomation.Version).AddVisualChild(new AdaptiveProgressBar() { Title = "下载进度", Value = new BindableProgressBarValue("ProgressValue"), ValueStringOverride = new BindableString("ProgressValueString"), Status = new BindableString("ProgressStatus") }).GetToastContent();
    ToastNotification DownloadToast = new ToastNotification(DownloadToastContent.GetXml());
    string DownloadToastTag = DownloadInfomation.Version;
    string DownloadToastGroup = "HyperMinecraftLauncher-GameDownload";
    DownloadToast.Tag = DownloadToastTag;
    DownloadToast.Group = DownloadToastGroup;
    DownloadToast.Data = new NotificationData();
    DownloadToast.Data.Values["ProgressValue"] = "0";
    DownloadToast.Data.Values["ProgressValueString"] = "下载中";
    DownloadToast.Data.Values["ProgressStatus"] = "创建文件夹";
    DownloadToast.Data.SequenceNumber = 1;
    DownloadToast.SuppressPopup = false;
    ToastNotificationManager.CreateToastNotifier().Show(DownloadToast);    //this

    try
    {
        System.IO.Directory.CreateDirectory("./.minecraft/" + DownloadInfomation.Version);
    }
    catch (Exception e)
    {
        new ToastContentBuilder().AddText("我们无法为你创建游戏文件夹").AddText("游戏下载中断").AddText(e.Message).Show();
        return;
    }

    try
    {
        HttpClient Client = new HttpClient();
        Client.DefaultRequestHeaders.Accept.Clear();
        Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("text/html"));
        Client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/xhtml+xml"));
        Client.DefaultRequestHeaders.Add("User-Agent", ".NET Foundation Repository Reporter");
        System.IO.Stream UnprocessedManifestStream = await Client.GetStreamAsync("https://piston-meta.mojang.com/v1/packages/c18d3537d1c262a5527126b317dcf6056f7d8e4d/23w44a.json");

        using (System.IO.Stream FileStream = System.IO.File.Create("./.minecraft/" + DownloadInfomation.Version + "/" + DownloadInfomation.Version + ".json"))
        {
            await UnprocessedManifestStream.CopyToAsync(FileStream);
            UnprocessedManifestStream.Close();
        }

        string UnprocessedManifest = System.IO.File.ReadAllText("./.minecraft/" + DownloadInfomation.Version + "/" + DownloadInfomation.Version + ".json");

    }
    catch (Exception e)
    {
        new ToastContentBuilder().AddText("我们无法为你下载游戏核心文件").AddText("游戏下载中断").AddText(e.Message).Show();
        return;
    }
}

When I call this function, it doesn't pop up a toast as expected.

image