In C++/cx's windows::storage: A Fileopenpicker class is defined in the:P ickers namespace, which allows you to create a new file to open the picker and select one or more files by using the properties and functions contained within the class. The following lists the properties that the Fileopenpicker class contains:
- The ViewMode property, which sets the view mode for the file Open picker, includes list (list mode) and thumbnail (thumbnail mode) values for this property.
- The Suggestedstartlocation property, which sets the initial position of the file open picker, the value of this property includes Documentslibrary (document), Computerfolder (computer), desktop, Downloads (download), HomeGroup (homegroup), musiclibrary (music Library), pictureslibrary (Picture library), and videoslibrary (Video library).
- The Filetypefilter property gets a collection of file types that the file open picker displays.
Next, an example is used to illustrate how to create a new file with the Fileopenpicker class to open the Picker and select a picture file from the file Open picker. In Visual Staudio 2012, you create a blank application project for the Windows Store in Visual C + + and name Filepickdemo. Then open the MainPage.xaml.cpp source file, referencing the following header files and namespaces:
#include "Ppltasks.h"
using namespace Windows::storage;
Using namespace Windows::storage::P ickers;
using namespace Concurrency;
using namespace Windows::system;
In the preceding code, use the Include keyword to refer to the header file Ppltasks.h and use the using directive to reference the namespace Windows::storage, Windows::storage::P ickers, Concurrency and Windows::system, the classes defined in these header files and namespaces are used when new and set files open the picker.
Next, layout the foreground interface, open the MainPage.xaml file, and add the following code to the grid element:
<button x:name= "Filebutton" content= " Open File "fontsize=" "margin=" 100,0,0,0 "click=" Filebuttonclick "width=" "height=" ></Button>
In the code above, an "Open file" button was added to open the picker from the file to select the file. The display of the foreground interface is shown in 20-6.
Figure 20-6 "Open File" button
After you add the Open File button, next add the Click event handler Filebuttonclick to this button. Open the Mainpage.xaml.h header file and add the following code to declare the Filebuttonclick function.
Private
???? Select picture File
???? void Filebuttonclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e);
After declaring the Filebuttonclick function, add the implementation code for the Filebuttonclick function in the MainPage.xaml.cpp source file, as shown in the following code:
// Select Picture File
void Filepickdemo::mainpage::filebuttonclick (platform::object^ sender, windows::ui::xaml::routedeventargs^ e)
{
???? Create File Open Picker
???? fileopenpicker^ openpicker =ref New Fileopenpicker ();
???? set view as thumbnail
???? Openpicker->viewmode = Pickerviewmode::thumbnail;
???? Set the initial location for access to the picture library
???? Openpicker->suggestedstartlocation = Pickerlocationid::P ictureslibrary;
???? Set the file types that can be selected
???? Openpicker->filetypefilter->append (". png");
???? Openpicker->filetypefilter->append (". jpg");
???? Show File Open picker, select File
???? Create_task (Openpicker->picksinglefileasync ()). Then ([This] (storagefile^ file)
???? {
???????? Read the selected file
????});
}
In the preceding code, you first create an object Openpicker for the Fileopenpicker class and assign the enumeration member thumbnail in the Pickerviewmode enumeration to the Openpicker property of the ViewMode object. Set the view mode of the file open picker to thumbnail mode. The enumeration member Pictureslibrary in the Pickerlocationid enumeration is then assigned to the Suggestedstartlocation property of the Openpicker object, and the initial position of the file Open picker is the picture library. Then use the Filetypefilter property of the Openpicker object to get the file type collection displayed by the file Open Picker and call the Append function to add the file type ". png" and ". jpg" to this collection. Next, call the Picksinglefileasync function of the Openpicker object to pick a single file and get an object file of type Storagefile.
Run the Filepickdemo project and click the Open File button to open the Picker interface with the file shown in Figure 20-7.
Figure 20-7 File Open Picker interface
WIN10 Series: VC + + File selection