Since I had a blog about accessing the files on my phone's SD card, that was the path to the file that I knew, or the file that I knew was put there, so
Direct access through code, of course, is OK. But in many cases we do not know where we want the files in the SD card, we have to slowly check the level
Search, and then select the line. Then you need to fileopenpicker.
Access to the phone SD card file Blog address: Windows Phone8.1 in the SD card file read Write method summary
For example, a self-portrait, the majority of girls to use the United States 美图秀秀 Ah, such as beauty map software to be able to upload to the various social networking sites up. When we
What are our steps to complete this requirement?
The first kind of, of course, can open the album directly, find self-portrait, choose the way to edit, link to the United States 美图秀秀 software open (this link app
The style is also very common, it is necessary to master, this blog does not introduce)
The second kind, the impatient is directly open the United States 美图秀秀, and then there is a button in the map to choose your mobile phone album in the specific picture, then jump out
Allows you to re-layer on the phone to find pictures, the final choice to self-portrait process is fileopenpicker to achieve.
Microsoft Official Link: Fileopenpicker
Well, knowing why you need this, you should know some of the common properties of Filepicker:
(many attributes are literally thought-based, not difficult to understand.) Actually see Microsoft Official link better and more detailed, very helpful)
1.ViewMode (Pickerviewmode type)------Renders the item's view mode List/thumbnail item's list/thumbnail image set
2.SuggestedStartLocation (Pickerloactionid type)---------The starting position of the file presented to US Documents/videos
/pictures/musiclibary Downloads Desktop HomeGroup
3.CommitButtonText------Select the file we want to press the confirmation button above the text
4.SettingIdentifier-------The identification of a single fileopenpicker when used with multiple fileopenpicker to differentiate
Note: The above properties are not valid in Windows Phone 8.1
5.FileTypeFilter------A collection of document types, you can actually think of it as a file format filter that filters only the files we want to see in the format.
Take the property, and with a rough idea, you can do it.
For Windows 8 and 8.1, there may be some small differences between the two, but the basic is consistent and the version of the upgrade has not changed much.
Directly on the pseudo-code:
FileOpenPicker picker = new FileOpenPicker();
/ / Set the open display mode
picker.ViewMode = PickerViewMode.List;
/ / Start to open the window displayed by the document, here is the desktop
picker.SuggestedStartLocation = PickerLocationId.Desktop;
/ / In fact, it is the role of filtering, stipulate that you want to open a .txt file
picker.FileTypeFilter.Add(".txt");
// PickMultipleFilesAsync is to select multiple files,
// PickSingleFileAsync selects a single file
StorageFile file = await picker.PickSingleFileAsync();
If (file != null)
{
/ / Return is the file name
txtTitle.Text = file.Name;
/ / Return is the file path
//txtTitle.Text = file.Path;
Var stream = await file.OpenStreamForReadAsync();
StreamReader sr = new StreamReader(stream);
/ / This is to specify encoding when your .txt text is encoded in ANSI (national standard)
//StreamReader sr = new StreamReader(stream,Encoding.GetEncoding("GB2312"));
String txt = await sr.ReadToEndAsync();
txtContent.Text = txt;
}
and the Windows Phone platform is not the same, much more trouble than Windows, WP8 and WP8.1 version of the iteration is also a few different
So where's the trouble with Windows Phone?
One is the mobile phone exists when the Fileopenpicker function, the application is suspended, when the Fileopenpicker selection is completed, the application will be restored
, so it's critical and necessary to rewrite the onactivated event and the continuefileopenpicker function at this time.
The second is that the Fileopenpicker in the Windows platform selects the properties of single or multiple files corresponding to Pickersinglefileasync () and
Pickmultiplefilesasync (), although asynchronous, just one async and one await will be done. and the Windows Phone platform is only
Picksinglefileandcontinue () and Pickmultiplefilesandcontinue (), this is in line with the 1th, handling trouble.
In a word, trouble is a problem. How to continue running the Windows Phone app after you call the file picker. The explanations and solutions of Microsoft are given below
Case, we look at the basic understanding, if still feel too messy, I can also see.
Official explanations and solutions: How to continue running the Windows Phone app (XAML) after invoking the file picker
Trouble is trouble, the problem will be solved after all. There are three big steps to solving this problem, and the reasons for each step are answered.
First, rewrite the onactivated () event in App.xaml.cs
protected override void OnActivated(IActivatedEventArgs args)
{
if(args is FileOpenPickerContinuationEventArgs)
{
Frame rootFrame = Window.Current.Content as Frame;
if(!rootFrame.Navigate(typeof(RepresentBook)))
{
throw new Exception("Failed to create target page");
}
var page = rootFrame.Content as RepresentBook;
page.FileOpenPickerEvent = (FileOpenPickerContinuationEventArgs)args;
}
Window.Current.Activate();
}
Second, instantiate the Fileopenpicker object in the. cs of the corresponding page, and set all the properties
Private void findBtn_Click(object sender, RoutedEventArgs e)
{
/ / Clear the error message
txtTip.Text = "";
/ / Instantiate the FileOpenPicker object - file picker
FileOpenPicker picker = new FileOpenPicker();
/ / Set the open display mode, this does not work on WP8.1, but still useful on Win8.1
//picker.ViewMode = PickerViewMode.Thumbnail;
/ / In fact, open the window displayed by the document, this does not work on WP8.1, but still useful on Win8.1
//picker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
/ / Specify the file format to open
picker.FileTypeFilter.Add(".txt");
/ / Use ContinuationData to record some information to ensure that the application can recover the information when the application resumes
picker.ContinuationData["Operation"] = "Text";
//The text displayed on the submit button does not work on WP8.1, it is still useful on Win8.1.
//picker.CommitButtonText = "Find a book";
/ / Set to select a single file or multiple files
picker.PickSingleFileAndContinue();
}
Finally, the returned data is processed using Fileopenpicker's page processing function continuefileopenpicker
The first thing to do is to define a property for this class of pages, which is get,set.
private FileOpenPickerContinuationEventArgs _fileOpenPickerEventArgs = null;
public FileOpenPickerContinuationEventArgs FileOpenPickerEvent
{
get { return _fileOpenPickerEventArgs; }
set
{
_fileOpenPickerEventArgs = value;
ContinueFileOpenPicker(_fileOpenPickerEventArgs);
}
}
The continuefileopenpicker function is then used to process the return data
Public void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
If ((args.ContinuationData["Operation"] as string) == "Text" && args.Files != null && args.Files.Count > 0)
{
StorageFile file = args.Files[0];
List<BookInfo> bookinfo = new List<BookInfo>();
Random random = new Random();
Int ran = random.Next(1, 6);
String imguri = "Assets/Images/" + ran + ".jpg";
Int length = localSettings.Values.Count+1;
String key = "booklist" + length.ToString();
If (!localSettings.Containers.ContainsKey(key))
{
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
Composite["bookimg"] = imguri;
Composite["bookname"] = file.Name;
Composite["bookpath"] = file.Path;
If(localSettings.Values.Count==0)
{
localSettings.Values[key] = composite;
}
Else
{
For (int i = 1; i < localSettings.Values.Count + 1; i++)
{
ApplicationDataCompositeValue composite2 = new ApplicationDataCompositeValue();
Composite2 = (ApplicationDataCompositeValue)localSettings.Values["booklist" + i.ToString()];
If (composite2["bookname"].ToString()==file.Name)
{
txtTip.Text = "This book already exists in the list!";
}
}
If(txtTip.Text=="")
{
localSettings.Values[key] = composite;
}
}
}
If(localSettings.Values.Count!=0)
{
For (int i = 1; i < localSettings.Values.Count+1;i++ )
{
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();
Composite = (ApplicationDataCompositeValue)localSettings.Values["booklist" + i.ToString()];
bookinfo.Add(new BookInfo { bookImg = composite["bookimg"].ToString(), bookName = composite["bookname"].ToString(), bookPath = composite["bookpath"].ToString() });
}
listbox.ItemsSource = bookinfo;
}
}
}
All of these are pseudo-code, we can understand the idea.
Recommended Links:
Programming Small dreams: Windows Phone 8.1 development: File selector Fileopenpicker
Win8 and Windows Phone 8.1 resource bridge: Fileopenpicker