首页 技术 正文
技术 2022年11月22日
0 收藏 630 点赞 3,562 浏览 3888 个字

以保存文件为例

首先,在项目中加入ContinuationManager.cs类,以及SuspensionManager.cs类。

其次,在App.xaml.cs中,完成如下步骤:

1. 添加ContinuationManager类的实例作为属性。

public ContinuationManager ContinuationManager { get; private set; }

2. 加入如下的方法

        // for continuable
private Frame CreateRootFrame()
{
Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (rootFrame == null)
{
// Create a Frame to act as the navigation context and navigate to the first page
rootFrame = new Frame(); // Set the default language
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[];
rootFrame.NavigationFailed += OnNavigationFailed; // Place the frame in the current Window
Window.Current.Content = rootFrame;
} return rootFrame;
} private async Task RestoreStatusAsync(ApplicationExecutionState previousExecutionState)
{
// Do not repeat app initialization when the Window already has content,
// just ensure that the window is active
if (previousExecutionState == ApplicationExecutionState.Terminated)
{
// Restore the saved session state only when appropriate
try
{
await SuspensionManager.RestoreAsync();
}
catch (SuspensionManagerException)
{
//Something went wrong restoring state.
//Assume there is no state and continue
}
}
} void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
} protected async override void OnActivated(IActivatedEventArgs e)
{
base.OnActivated(e);
ContinuationManager = new ContinuationManager(); Frame rootFrame = CreateRootFrame(); await RestoreStatusAsync(e.PreviousExecutionState); if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage));
} var continuationEventArgs = e as IContinuationActivatedEventArgs;
if (continuationEventArgs != null)
{
// Call ContinuationManager to handle continuation activation
ContinuationManager.Continue(continuationEventArgs); }
Window.Current.Activate();
}

最后, 实现保存文件的操作, 在保存文件的页面,使该页面可以实现 IFileSavePickerContinuable接口。

public sealed partial class MainPage: Page, IFileSavePickerContinuable
{
string OriginalPictureUrl = string.Empty;
private void DownloadAppBarButton_Click(object sender, RoutedEventArgs e)
{
var item = FlipViewControl.SelectedItem as ViewSource;
if (null != item)
{
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
savePicker.FileTypeChoices.Add("JPG File", new ObservableCollection<string>() { ".jpg" });
savePicker.DefaultFileExtension = ".jpg";
savePicker.SuggestedFileName = item.ViewImageUrl.Substring(item.ViewImageUrl.LastIndexOf("/") + );
savePicker.PickSaveFileAndContinue();
OriginalPictureUrl = item.ViewImageUrl;
}
} public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
{
if (null != args.File)
{
StorageFile saveFile = args.File;
var uri = new Uri(OriginalPictureUrl);
var fileName = saveFile.DisplayName;
var thumbnail = RandomAccessStreamReference.CreateFromUri(uri); var remoteFile = await StorageFile.CreateStreamedFileFromUriAsync(fileName, uri, thumbnail);
await remoteFile.CopyAndReplaceAsync(saveFile); var toast = new ToastPrompt { Message = "download completed." };
toast.Show();
}
}}

另外,若要实现打开文件的操作,页面需要实现 IFileOpenPickerContinuable 接口。

public sealed partial class SettingPage : Page, IFileOpenPickerContinuable
{
private void PickPhoto()
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.Thumbnail;
openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
openPicker.FileTypeFilter.Add(".jpg");
openPicker.FileTypeFilter.Add(".jpeg");
openPicker.FileTypeFilter.Add(".png");
openPicker.PickSingleFileAndContinue();
} public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
{
if (args.Files.Count > )
{
StorageFile bgFile = null;
string id = Guid.NewGuid().ToString();
string bgFileName = string.Format(@"{0}.jpg", id);
bgFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(bgFileName, CreationCollisionOption.OpenIfExists);
App.BgImg = bgFileName;
bool isSaved = await new data().SetBackground(App.BgImg); App.isBGChanged = true;
await args.Files[].CopyAndReplaceAsync(bgFile); // display the selected image
ShowImage();
}
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,077
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,813
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,896