我遵循MVVM模式,並有一個名爲DocumentStore
的模型。這個類有一個方法如下:UWP/WinRT:如何在完成模型中的異步操作後執行UI任務?
void DocumentStore::Open_Document(StorageFile^ file) {
create_task(FileIO::ReadTextAsync(file))
.then([this, file](String^ fileContents)
{
// Take the fileContents and add them to internal data structure
});
}
我的視圖模型被彈出一個FileOpenPicker
得到一個文件,它再喂作爲參數爲Open_Document
:
create_task(picker->PickSingleFileAsync())
.then([this](StorageFile^ file)
{
m_DocStore->Open_Document(file);
// Target location to do something
}
);
我希望能夠在Open_Document
內部的任務完成後,即在處理完fileContents之後執行操作。
有沒有一種方法讓我的模型通知任何感興趣的監聽者任務已完成?
或者我的模型的Open_Document
方法實際上本身是異步的嗎?但是,我需要處理任務內的數據結構,並不會導致我的方法在不同的線程上下文中運行?
我正在C++/CX工作,但會採取任何幫助,我可以得到。
您是否考慮過將lambda傳遞給'OpenDocument',可以在異步工作完成後調用? – WiredPrairie