[英]Opening a file from Toast Notification
在我的UWP應用中,我正在下載文件並將其存儲在用戶使用FolderPicker
選擇的位置中。 下載完成后,我將顯示ToastNotification。 我正在使用這兩個名稱空間,如文檔所示。
using Microsoft.QueryStringDotNET; // for receciving arguments
using Microsoft.Toolkit.Uwp.Notifications;
我發送的吐司有兩個按鈕,1)打開文件2)關閉。 我想在用戶點擊第一個按鈕時打開下載的文件。
但是據我了解,敬酒只能將字符串參數發送給應用程序(如果出錯,請更正我)。 並且為了打開文件,需要StorageFile
對象( StorageFile
路徑不做)。
那么,有什么方法可以從吐司中實際打開下載的文件(使用前台激活還是后台激活)?
下載文件的代碼:
private async void DownloadButton_Click(object sender, RoutedEventArgs e)
{
StorageFolder selectedFolder;
try
{
selectedFolder = await ChooseFolderAsync();
}
catch
{
Toast.ShowToast("Something went wrong", ToastRow);
return;
}
Uri downloadLink = new Uri("ValidUri");
StorageFile destinationFile = await selectedFolder.CreateFileAsync(selectedAsset.name, CreationCollisionOption.GenerateUniqueName);
BackgroundDownloader downloader = new BackgroundDownloader();
downloader.SuccessToastNotification = handler.MakeToastWithButtons("Downloaded", selectedAsset.name, "Open", "Dismiss");
// downloader.SuccessToastNotification = handler.MakeToast("Downloaded", nameOfFile, string.Empty, 2);
DownloadOperation download = downloader.CreateDownload(downloadLink, destinationFile);
download.Priority = BackgroundTransferPriority.High;
download.CostPolicy = BackgroundTransferCostPolicy.Always;
var toast = handler.MakeToast("Downloading...", selectedAsset.name, selectedAsset.contentSize, 12);
toast.Group = "downloadStartedTag";
ToastNotificationManager.CreateToastNotifier().Show(toast);
Progress<DownloadOperation> progressCallback = new Progress<DownloadOperation>(handler.DownloadProgress);
try
{
await download.StartAsync().AsTask(cts.Token, progressCallback);
}
catch (Exception ex)
{
var errorCode = BackgroundTransferError.GetStatus(ex.HResult);
toast = handler.MakeToast("Download failed", selectedAsset.name, TextFormatter.CamelToHumanCase(errorCode.ToString()), 12);
toast.Group = "downloadFailedTag";
ToastNotificationManager.CreateToastNotifier().Show(toast);
return;
}
finally
{
ToastNotificationManager.History.Remove("downloadStartedTag");
}
}
創建吐司的方法:
public ToastNotification MakeToastWithButtons(string heading, string line1, string button1, string button2)
{
ToastVisual visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText() {Text = heading},
new AdaptiveText() {Text = line1},
}
}
};
ToastActionsCustom actions = new ToastActionsCustom()
{
Buttons =
{
new ToastButton("Open", new QueryString()
{
{ "action", "open" }
//maybe file path can be given here in some argument
}.ToString())
{
ActivationType = ToastActivationType.Foreground
},
new ToastButton("Dismiss", new QueryString()
{
{ "action", "dismiss" }
//more details about the file can be given here
}.ToString())
{
ActivationType = ToastActivationType.Background
}
}
};
ToastContent toastContent = new ToastContent()
{
Visual = visual,
Actions = actions,
// Arguments when the user taps body of toast
Launch = new QueryString()
{
{ "action", "nothing" }
}.ToString()
};
// And create the toast notification
var toast = new ToastNotification(toastContent.GetXml());
toast.ExpirationTime = DateTime.Now.AddDays(2);
toast.Group = "DownloadCompleteGroup";
return toast;
}
一種方法是將由后台下載創建的destinationFile
存儲在FutureAccessList
,這將為您提供一個token
您可以將其添加到Toast的有效載荷數據中:
// using Windows.Storage.AccessCache;
string token = StorageApplicationPermissions.FutureAccessList.Add(destinationFile);
// add the token to your toast payload
然后,當用戶單擊烤面包時,您可以兌換token
以將文件取回:
var file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(token);
現在,您可以使用該文件了。 請注意,令牌是“永遠”的好東西-只要文件存在於磁盤上,令牌將在您的應用重啟甚至重啟后起作用。
處理完文件后(包括下載失敗或用戶取消等),您應該從FutureAccessList
刪除令牌, FutureAccessList
使列表不填滿:
StorageApplicationPermissions.FutureAccessList.Remove(token);
這意味着您可能還希望將令牌也保留在應用程序的設置中,以防萬一用戶忽略了吐司。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.