Recall 會自動儲存應用程式的快照,不過,除非您在快照時提供 UserActivity,否則使用者將無法重新啟動您的內容。 這可讓使用者 Recall 重新進入當時所看到的內容。
UserActivity 是指使用者在應用程式內處理的特定專案。 例如,當使用者正在撰寫檔時, UserActivity 可以參考使用者離開寫入的檔的特定位置。 聽音樂應用程式時, UserActivity 可能是使用者上次接聽的播放清單。 在畫布上繪製時, UserActivity 可能是用戶最後一次標記的位置。 總而言之, UserActivity 代表使用者可返回的 Windows 應用程式中的目的地,讓他們可以繼續執行其作業。
推送用戶活動
每當應用程式的主要內容變更時(例如使用者開啟不同的電子郵件、開啟不同的網頁等),您的應用程式應該記錄新的 UserActivitySession 內容,讓系統知道目前開啟的內容。
Recall 接著,會將最新的 UserActivity 與所儲存的快照產生關聯,並使用活動內的 ActivationUri,讓使用者可以重新啟動該內容。
我們建議您在所有計算機上推送用戶活動,即使是那些未執行 Recall 的計算機。
UserActivitySession _previousSession;
private async Task OnContentChangedAsync()
{
// Dispose of any previous session (which automatically logs the end of the interaction with that content)
_previousSession?.Dispose();
// Generate an identifier that uniquely maps to your new content.
string id = "doc135.txt";
// Create a new user activity that represents your new content
var activity = await UserActivityChannel.GetDefault().GetOrCreateUserActivityAsync(id);
// Populate the required properties
activity.DisplayText = "doc135.txt";
activity.ActivationUri = new Uri("my-app://docs/doc135.txt");
// Save the activity
await activity.SaveAsync();
// And start a new session tracking the engagement with this new activity
_previousSession = activity.CreateSession();
}
備註
GetOrCreateUserActivityAsync 方法一律會在最新版本的 Windows 上傳回新的活動。 已移除取得先前儲存的活動的能力,而 Windows 不再以應用程式可以擷取的方式儲存您應用程式的先前活動。
(可選):處理已請求事件
除了推送活動之外,您的應用程式還可以選擇實作 UserActivityRequested 事件,Windows 可能會觸發此事件,以確保擁有來自您應用程式的最新活動。
public void OnLaunched()
{
UserActivityRequestManager.GetForCurrentView().UserActivityRequested += UserActivityRequested;
}
private async void UserActivityRequested(
Windows.ApplicationModel.UserActivities.UserActivityRequestManager sender,
Windows.ApplicationModel.UserActivities.UserActivityRequestedEventArgs args)
{
// Start a deferral so you can use async code
var deferral = args.GetDeferral();
try
{
// Generate an identifier that uniquely maps to your current content.
string id = "doc135.txt";
// Create a user activity that represents your current content
var activity = await UserActivityChannel.GetDefault().GetOrCreateUserActivityAsync(id);
// Populate the required properties
activity.DisplayText = "doc135.txt";
activity.ActivationUri = new Uri("my-app://docs/doc135.txt");
// And return the activity to the event handler
args.Request.SetUserActivity(activity);
}
finally
{
// And complete the deferral
deferral.Complete();
}
}
相關內容
- UserActivity 類別
-
使用 UserActivity ContentInfo 將敏感度標籤提供至 Recall - 學習如何通過企業 DLP 政策來強制執行提供敏感度標籤的元資料
UserActivity.ContentInfo - 網頁瀏覽器開發人員的指引