WidgetManager.UpdateWidget(WidgetUpdateRequestOptions) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
為小工具主機提供小工具的更新內容。
public:
virtual void UpdateWidget(WidgetUpdateRequestOptions ^ widgetUpdateRequestOptions) = UpdateWidget;
void UpdateWidget(WidgetUpdateRequestOptions const& widgetUpdateRequestOptions);
public void UpdateWidget(WidgetUpdateRequestOptions widgetUpdateRequestOptions);
function updateWidget(widgetUpdateRequestOptions)
Public Sub UpdateWidget (widgetUpdateRequestOptions As WidgetUpdateRequestOptions)
參數
- widgetUpdateRequestOptions
- WidgetUpdateRequestOptions
WidgetUpdateRequestOptions物件,其中包含要用來更新小工具的內容。
實作
範例
下列程式碼範例示範 UpdateWidget的一般用法。 WidgetManager.GetWidgetInfos() 會呼叫 來取得與小工具提供者相關聯之使用中小工具的物件清單 WidgetInfo 。 針對每個小工具,會產生視覺化範本和資料範本,並傳遞至WidgetUpdateRequestOptions物件中的UpdateWidget呼叫。
using namespace winrt;
using namespace Microsoft::Windows::Widgets;
using namespace Microsoft::Windows::Widgets::Providers;
class WidgetManagerOperations
{
void InitializeWidgets(hstring myWidgetId)
{
WidgetManager widgetManager = WidgetManager::GetDefault();
com_array<WidgetInfo> widgetInfos = widgetManager.GetWidgetInfos();
for (const auto& widgetInfo : widgetInfos)
{
if (widgetInfo.WidgetContext().IsActive())
{
WidgetUpdateRequestOptions options{myWidgetId};
options.Template(LR"({
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{
"type": "TextBlock",
"text": "${greeting}"
}
]
})");
options.Data(LR"({
"greeting": "Hello"
})");
widgetManager.UpdateWidget(options);
}
}
}
}
備註
不會更新 WidgetUpdateRequestOptions類別中未設定的任何變數。 例如,如果您只想要更新小工具的資料,但您想要保留現有的視覺化範本和自訂狀態,則只要 WidgetUpdateRequestOptions.Data 設定 屬性,小工具的資料就會更新,但不會修改範本和自訂狀態。
UpdateWidget 也可用來清除任何預存值,方法是設定要清除值的空字串。 例如,如果您想要清除小工具的預存自訂狀態,您可以將 屬性設定 WidgetUpdateRequestOptions.CustomState 為空字串,這會在呼叫 UpdateWidget 之後清除儲存的自訂狀態。
呼叫此函式來更新已刪除的小工具、未與您的應用程式相關聯的小工具,或使用不存在的小工具識別碼,將會導致忽略更新。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應