以保存文件为例

首先,在项目中加入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程序(打开文件,保存文件等)的更多相关文章

  1. [No0000192]Vim打开和保存文件-Vim使用技巧(7)

    使用Vim打开和保存文件是最常用的操作,介绍使用edit命令通过文件路径来打开文件,使用write命令保存文件,当文件路径不存在或用户权限不匹配时,使用write命令调用外部shell程序完成操作. ...

  2. silverlight打开和保存文件

    因为Silverlight是运行在浏览器中的客户端,所以对于程序的操作权限要求比较严格,以本篇的主题来说,一个表现就是不能够随意的进行文件打开和保存操作,如果在代码中直接使用Stream来操作文件,会 ...

  3. 12.JAVA之GUI编程打开与保存文件

    功能:java图形用户界面开发,练习打开保存文件 代码如下: import java.awt.FileDialog; import java.awt.Frame; import java.awt.Me ...

  4. Qt snippet — 打开文件&保存文件

    打开文件: void Notepad::on_actionOpen_triggered() { QString fileName = QFileDialog::getOpenFileName(this ...

  5. CFileDialog 打开文件夹文件 保存文件夹文件

    格式说明: explicit CFileDialog(    BOOL bOpenFileDialog,                         //TRUE 为打开, FALSE 为保存 L ...

  6. C#用openfiledialog文件和savefileDialog打开和保存文件

    一 打开文件 Stream myStream = null; OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog ...

  7. #用openfiledialog文件和savefileDialog打开和保存文件

    一.打开文件 Stream myStream = null;            OpenFileDialog openFileDialog1 = new OpenFileDialog();     ...

  8. VIM之打开、保存文件

    如何使用命令 在Normal mode下,输入':'字符,在GVIM界面左下可以看到如图所示的界面: 这时候可以键入命令,输入完后按下键盘上的Enter键即可执行命令. 打开文件 使用命令:e [文件 ...

  9. 【Linux】解决用vi修改文件,保存文件时,提示“readonly option is set”

    当在终端执行sudo命令时,系统提示“hadoop is not in the sudoers file”: 其实就是没有权限进行sudo,解决方法如下(这里假设用户名是cuser): 1.切换到超级 ...

随机推荐

  1. tomcat 集群配置,Session复制共享

    本配置在tomcat7上验证通过.通过此方法配置的集群,session信息将会被自动复制到各个节点. 1.配置Server.xml 在Server.xml中,找到被注释<Cluster/> ...

  2. ASP.NET-【Excel】-将Excel中的数据批量加载到SQLserver数据库

    用到了一个SqlBulkCopy的类 核心代码分析 代码我还没有测试过 string excelConnectionString = string.Format("Provider=Micr ...

  3. The Python web services developer: XML-RPC for Python

    原文地址:http://www.ibm.com/developerworks/webservices/library/ws-pyth10/index.html 摘要:概括地说,您可以将 XML-RPC ...

  4. cstring 的重载

    #include <iostream> #include <windows.h> using namespace std; +; class MyString { public ...

  5. [css3]搜索框focus时变长

    结构: <form class="demo-a">    <input placeholder="Search" type="sea ...

  6. 作用域闭包、预解释和this关键字综合题目

    var number = 2; var obj = {number : 5, fn1 : ( function() { this.number *= 2; number=number*2; var n ...

  7. [js]变量声明、函数声明、函数定义式、形参之间的执行顺序

    一.当函数声明和函数定义式(变量赋值)同名时 function ledi(){ alert('ledi1'); }; ledi(); var ledi = function (){ alert('le ...

  8. 转: CSS中overflow的用法

    Overflow可以实现隐藏超出对象内容,同时也有显示与隐藏滚动条的作用,overflow属性有四个值:visible (默认), hidden, scroll, 和auto.同样有两个overflo ...

  9. POJ 1011 Sticks dfs,剪枝 难度:2

    http://poj.org/problem?id=1011 要把所给的集合分成几个集合,每个集合相加之和ans相等,且ans最小,因为这个和ans只在[1,64*50]内,所以可以用dfs一试 首先 ...

  10. 【C++】String类实现

    //string.h #include <iostream> using namespace std; class String; istream& operator>> ...