WP8.1 实现Continuation程序(打开文件,保存文件等)
以保存文件为例
首先,在项目中加入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();
}
} }
WP8.1 实现Continuation程序(打开文件,保存文件等)的更多相关文章
- [No0000192]Vim打开和保存文件-Vim使用技巧(7)
使用Vim打开和保存文件是最常用的操作,介绍使用edit命令通过文件路径来打开文件,使用write命令保存文件,当文件路径不存在或用户权限不匹配时,使用write命令调用外部shell程序完成操作. ...
- silverlight打开和保存文件
因为Silverlight是运行在浏览器中的客户端,所以对于程序的操作权限要求比较严格,以本篇的主题来说,一个表现就是不能够随意的进行文件打开和保存操作,如果在代码中直接使用Stream来操作文件,会 ...
- 12.JAVA之GUI编程打开与保存文件
功能:java图形用户界面开发,练习打开保存文件 代码如下: import java.awt.FileDialog; import java.awt.Frame; import java.awt.Me ...
- Qt snippet — 打开文件&保存文件
打开文件: void Notepad::on_actionOpen_triggered() { QString fileName = QFileDialog::getOpenFileName(this ...
- CFileDialog 打开文件夹文件 保存文件夹文件
格式说明: explicit CFileDialog( BOOL bOpenFileDialog, //TRUE 为打开, FALSE 为保存 L ...
- C#用openfiledialog文件和savefileDialog打开和保存文件
一 打开文件 Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog ...
- #用openfiledialog文件和savefileDialog打开和保存文件
一.打开文件 Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); ...
- VIM之打开、保存文件
如何使用命令 在Normal mode下,输入':'字符,在GVIM界面左下可以看到如图所示的界面: 这时候可以键入命令,输入完后按下键盘上的Enter键即可执行命令. 打开文件 使用命令:e [文件 ...
- 【Linux】解决用vi修改文件,保存文件时,提示“readonly option is set”
当在终端执行sudo命令时,系统提示“hadoop is not in the sudoers file”: 其实就是没有权限进行sudo,解决方法如下(这里假设用户名是cuser): 1.切换到超级 ...
随机推荐
- github注册使用以及体会
一.个人介绍 我叫冯越,学号是1413042065,班级是网络工程143,兴趣爱好有打羽毛球,素描,读书,个人编程能力一般,行数没有计算过,大一学习C++时,实验题目一些书后的题目,大二学习数据结构时 ...
- Java JDK的安装以及环境变量的配置
安装并配置完Android SDK之后,本想着可以做个简单的APP应用了,只是依然提示我“请确认Java JDK是否安装”类似的报错,于是又进行了Java JDK的安装以及环境变量的配置. 1.下载地 ...
- Tomcat编码问题
在Tomcat7中,默认URIEncoding="iso8859-1",get请求由于url会完全出现在地址栏,所以传递中文到后台会乱码,需要改成URIEncoding=" ...
- Linux下smba服务端的搭建和客户端的使用
解决了 windows下用root登录linuxsamba后有部分目录访问无权限的问题.应该是SELinux 设置问题. 对selinux进行修改,一般为终止这项服务,操作如下: 查看SELinux状 ...
- AlarmManager手机闹钟简介
1.void set(int type , long triggerAtTime , PendingIntent operation ) : 设置在 triggerAtTime时间启动由operati ...
- ioinc
ioinc setup sassnpm installionic serve cordova plugin add cordova-plugin-crosswalk-webview 十.开发流程 1. ...
- 蓝桥杯 algo——6 安慰奶牛 (最小生成树)
问题描述 Farmer John变得非常懒,他不想再继续维护供奶牛之间供通行的道路.道路被用来连接N个牧场,牧场被连续地编号为1到N.每一个牧场都是一个奶牛的家.FJ计 划除去P条道路中尽可能多的道路 ...
- java的动态绑定和静态绑定
首先是方法的参数是父类对象,传入子类对象是否可行然后引出Parent p = new Children();这句代码不是很理解,google的过程中引出向上转型要理解向上转型又引出了动态绑定从动态绑定 ...
- android之DOM生成与解析
DOM解析不适合于进行大数据文件的操作,DOM解析适合于对文件进行修改和随机存取的操作. DOM生成 //判断一下是否存在sdcard if(!Environment.getExternalStora ...
- grub2的使用
1,添加win 启动项 edit file: /boot/grub2/grub.cfg 插入这几行: menuentry 'Windows XXX' { set root=(hd0,) chainlo ...