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.切换到超级 ...
随机推荐
- Objective-C:Category
Category可以在不需要创建子类或是修改原始类的基础上,动态为已经存在的类添加新的行为(方法),,这样可以保证类的原始设计规模较小,功能增加时再逐步扩展:实现了类的相关方法的模块化,把不同的类方法 ...
- BZOJ4006 [JLOI2015]管道连接
裸的状压DP 令$f_S$表示包含颜色集合S的最小斯坦纳生成森林的值,于是有: $$f_S=\min\{f_S,f_s+f_{S-s}|s\subset S\}$$ 然后嘛...还是裸的斯坦纳树搞搞. ...
- 管理工具MongoVUE使用
连接数据库 管理数据库 查询 1,查询所有 2,查询命令窗口
- 把Angular中的$http变成jQuery.ajax()一样,可以让后台(php)轻松接收到参数
最近接到一个手机项目,我决定用ionic + php + mysql来实现.ionic是一个前端框架,主要用于手机端,它融合了html5.css3.angularJS于一体,用起来很顺手. 开始构建项 ...
- 浏览器渲染原理--reflow
Web页面运行在各种各样的浏览器当中,浏览器载入.渲染页面的速度直接影响着用户体验简单地说,页面渲染就是浏览器将html代码根据CSS定义的规则显示在浏览器窗口中的这个过程.先来大致了解一下浏览器都是 ...
- ZOJ 3279
线段树单点更新 //============================================================================ // Name : E.c ...
- CSS 垂直居中。
1,display: table; display: table-cell <div style="border:solid red 1px ;height:200px;width:2 ...
- iOS解决两个静态库的冲突 duplicate symbol
http://blog.163.com/023_dns/blog/static/118727366201391544630380/ 场景: 解决TencentOpenAPI.framework与Zba ...
- Windows平台下的读写锁
Windows平台下的读写锁简单介绍Windows平台下的读写锁以及实现.背景介绍Windows在Vista 和 Server2008以后才开始提供读写锁API,即SRW系列函数(Initialize ...
- Android Studio 配置JPush
1.在JPush官方下载 JPush SDK(jpush-android-arm-2.1.0.zip),我下载的是2.1.0: 2.解压下载好的压缩包(jpush-android-arm-2.1.0. ...