Metro应用程序中的本地AppData监视器(StorageFileQueryResult.ContentsChanged事件未触发)

时间:2014-05-06 20:03:53

标签: windows-8.1 winrt-async appdata

如何监视AppData文件夹中的特定文件。

我尝试过使用StorageFolderQueryResult.ContentsChanged事件来处理这个问题,但是这个事件实际上会回调文件夹中的任何更改。

我的问题是只监视一个文件,并在更改后使用eventhandler。

我试图使用这个" UserSearchFilter" QueryOptions的属性。那实际上没有用。

c有人帮忙吗?如果你能另外提供整个问题的语法,那也会很有帮助。

我的contentschanged事件未在修改"文件名"在文件夹中。

     auto fileTypeFilter = ref new Platform::Collections::Vector<Platform::String^>();
fileTypeFilter->Append("*");

auto queryOptions = ref new QueryOptions(CommonFileQuery::OrderBySearchRank, fileTypeFilter);
//use the user's input to make a query
queryOptions->UserSearchFilter = InputTextBox->Text;
auto queryResult = musicFolder->CreateFileQueryWithOptions(queryOptions);


auto localFolder = ApplicationData::Current->LocalFolder;
auto currPath = localFolder->Path;
auto fileTypeFilter = ref new Platform::Collections::Vector<Platform::String^>();
fileTypeFilter->Append(".dat");

auto queryOptions = ref new QueryOptions(CommonFileQuery::OrderByName, fileTypeFilter);
//use the user's input to make a query
queryOptions->UserSearchFilter = L"filename";
auto queryResult = localFolder->CreateFileQueryWithOptions(queryOptions);
queryResult->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^,        Platform::Object^>(this, &Scenario1::OnLocalAppDataChanged);
     //find all files that match the query
 create_task(queryResult->GetFilesAsync()).then([this, queryOptions]    (IVectorView<StorageFile^>^ files)
    {
        String^ outputText = "";
        //output how many files that match the query were found
        if ( files->Size == 0)
     {
         outputText += "No files found for '" + queryOptions->UserSearchFilter + "'";
    }
    else if (files->Size == 1)
    {
        outputText += files->Size.ToString() + " file found:\n\n";
    }
    else
    {
        outputText += files->Size.ToString() + " files found:\n\n";
    }
    //output the name of each file that matches the query
    for (unsigned int i = 0; i < files->Size; i++)
    {
        outputText += files->GetAt(i)->Name + "\n";
    }
    OutputTextBlock->Text = outputText;
});
}
void Scenario1::OnLocalAppDataChanged(Windows::Storage::Search::IStorageQueryResultBase^ sender, Platform::Object^ args)
{
Platform::String^ hello = L"hello world, I'm called back";
}

1 个答案:

答案 0 :(得分:-1)

您必须至少调用一次GetFilesAsync()方法,否则该事件将永远不会触发。

添加

queryResult->GetFilesAsync();

之前

queryResult->ContentsChanged += ref new TypedEventHandler<IStorageQueryResultBase^,...

我知道你当时并不需要这些文件,但这是应该使用ContentsChanged事件的正式方式。请查看documentation的第一段。