问题
I have a launcher page(MainPage1.xaml).There is a button to select video file from device. Now I want to play that selected file on next page (MainPage2.xaml). How can I get this selcted file StorageFile file
on next page?
private async void Button_Click(object sender, RoutedEventArgs e)
{
StorageFile file;
FileOpenPicker openPicker = new FileOpenPicker();
foreach (string extension in FileExtensions.Video)
{
openPicker.FileTypeFilter.Add(extension);
}
file = await openPicker.PickSingleFileAsync();
this.Frame.Navigate(typeof(MainPage2), file);
}
回答1:
You have to override the OnNavigatedTo()
method in your page (next Page) which is expecting the StorageFile
:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter != null)
{
StorageFile file = (StorageFile)e.Parameter;
}
}
Hope this helps..
回答2:
Override OnNavigatedTo in DestinationPage
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var parameters = e.Parameter; // it will give your file reference
}
回答3:
There are two parts to this: as noted in other answers, the second argument to the Navigate method will be passed to OnNavigatedTo in the NavigationEventArgs.Parameter .
This is good for basic types, but isn't sufficient for a StorageFile. Quoting from the Frame.Navigate docs:
parameter Object The navigation parameter to pass to the target page; must have a basic type (string, char, numeric, or GUID) to support parameter serialization using GetNavigationState.
The StorageFile will work for a direct navigation, but since it isn't serializable it can get lost in the back stack, while suspending, etc.
Instead of passing the StorageFile directly stow it in a global dictionary and pass a string or GUID key that the OnNavigatedTo method can use to look up the StorageFile on the other side.
For the specific case of a StorageFile you can use the FutureAccessList. See Track recently used files and folders
来源:https://stackoverflow.com/questions/46639669/how-to-pass-seleced-file-details-to-another-page-uwp