kennykerr / modern

Modern C++ for the Windows Runtime
https://moderncpp.com/
MIT License
295 stars 36 forks source link

filesystem #11

Closed romka2411 closed 9 years ago

romka2411 commented 9 years ago

Now I`m working on app that read files and parse it. I try port my code into your CoreApp sample. First of all, app.cpp: code after line "CoreApplication::Run(make());" unreachable. I set after it "auto a=0" and set breakpoint - app never reaches this point. Second - I try to use "Windows::Storage::Pickers::FileOpenPicker". Usually it needs to set "ViewMode" prop. So following code: auto picker = Windows::Storage::Pickers::FileOpenPicker(); picker.FileTypeFilter.ViewMode = Windows::Storage::Pickers::PickerViewMode::List; generates error c2228 (which is currently missing on msdn). It said "left of ViewMode must be a class, struct...". Also I unable set FileTypeFilter prop - member of it "Append" is missing. Assume that this prop is Foundation::Vector<String^>

2015-08-28

kennykerr commented 9 years ago

There are quite a few problems with your example. Firstly, CoreApplication::Run almost never returns. It is meant to block until the app comes to a close. Even then it rarely returns as the OS prefers to kill the process abruptly.

Next, you need to keep in mind that Modern is a standard C++ language projection and standard C++ doesn’t have properties. That means that WinRT properties are projected as methods. This is why you’re getting the C2228 compiler error.

To get your example to work you need to create the FileOpenPicker once your app’s CoreWindow has been activated. You could take my CoreApp sample and change it as follows:

struct View : IFrameworkViewT<View>
{
    void SetWindow(CoreWindow const & window)
    {
        window.KeyUp([](CoreWindow const &, KeyEventArgs const &)
        {
            using namespace Windows::Storage;
            using namespace Windows::Storage::Pickers;

            FileOpenPicker picker;
            picker.FileTypeFilter().Append(L".txt");
            picker.ViewMode(PickerViewMode::List);

            picker.PickSingleFileAsync().Completed([](auto const & sender, AsyncStatus)
            {
                if (StorageFile file = sender.GetResults())
                {
                    MODERN_TRACE(L"%ls\n", file.Name().Buffer());
                }      
                else
                {
                    MODERN_TRACE(L"Canceled\n");
                }
            });
        });
    }
};

You can also enable the compiler’s support for resumable functions (with the /await compiler option) and then you can take advantage of the await keyword for a more natural way of waiting on the result of PickSingleFileAsync as follows:

FileOpenPicker picker;
picker.FileTypeFilter().Append(L".txt");
picker.ViewMode(PickerViewMode::List);

StorageFile file = await picker.PickSingleFileAsync();

if (file)
{
    MODERN_TRACE(L"%ls\n", file.Name().Buffer());
}
else
{
    MODERN_TRACE(L"Canceled\n");
}

Hope that helps.

romka2411 commented 9 years ago

Thanks, it works