【问题标题】:Local AppData Monitor in Metro app (StorageFileQueryResult.ContentsChanged event not firing)Metro 应用程序中的本地 AppData 监视器(StorageFileQueryResult.ContentsChanged 事件未触发)
【发布时间】:2014-05-06 20:03:52
【问题描述】:

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

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

我的问题是只监视单个文件并在其更改时使用事件处理程序。

我已尝试将此“UserSearchFilter”属性用于 QueryOptions。这实际上没有用。

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

我的 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";
}

【问题讨论】:

    标签: windows-8.1 winrt-async appdata


    【解决方案1】:

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

    添加

    queryResult->GetFilesAsync();
    

    之前

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      • 2013-07-07
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多