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.切换到超级 ...
随机推荐
- MATLAB随机森林回归模型
MATLAB随机森林回归模型: 调用matlab自带的TreeBagger.m T=textread('E:\datasets-orreview\discretized-regression\10bi ...
- HTML第二部分 CSS样式表
CSS(cascading style sheets,层叠样式表),作用是美化HTML网页. /*注释*/ 注释语法 2.1 样式表的基本概念 2.1.1样式表的分类 1.内联样式表 和HTML联 ...
- Xcode中的几个常用文件路径
在iOS开发中有时候需要知道一些文件的路径,这里总结如下: 路径查找第一步如图: 1.模拟器的路径:/Applications/Xcode.app/Contents/Developer/Platfor ...
- LOOPS(HDU 3853)
LOOPS Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total Sub ...
- Java JDK的安装以及环境变量的配置
安装并配置完Android SDK之后,本想着可以做个简单的APP应用了,只是依然提示我“请确认Java JDK是否安装”类似的报错,于是又进行了Java JDK的安装以及环境变量的配置. 1.下载地 ...
- httpClient 4.x post get方法
public static String doPost(String url, String encoding, String contentType, String sendData) throws ...
- TFS错误 TF31004:在连接带xxx.xxx.xxx.xxx上的Team Foundation Server时遇到错误。请等待几分钟,然后重试。如果问题仍然存在,请与服务器管理员联系
- HTML5表单新增属性
1.form 原来html里面,表单里的元素应该包裹在表单里,如 <form action="login.php" method="get"> &l ...
- linux 线程操作问题undefined reference to 'pthread_create'的解决办法(cmake)
问题原因: pthread 库不是 Linux 系统默认的库,连接时需要使用静态库 libpthread.a. 所以在使用pthread_create()创建线程时,需要链接该库. 1. 终端:问题解 ...
- COleDateTime类型的应用
使用COleDateTime类1) 获取当前时间. CTime time; time = CTime::GetCurrentTime();2) 获取时间元素. int y ...