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[0];
                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("/") + 1);
                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 > 0)
            {
                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[0].CopyAndReplaceAsync(bgFile);

                // display the selected image
                ShowImage();
            }
        }

}
时间: 2024-07-31 18:55:47

WP8.1 实现Continuation程序(打开文件,保存文件等)的相关文章

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

功能:java图形用户界面开发,练习打开保存文件 代码如下: import java.awt.FileDialog; import java.awt.Frame; import java.awt.Menu; import java.awt.MenuBar; import java.awt.MenuItem; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListen

Vim打开和保存文件-Vim使用技巧(7)

使用Vim打开和保存文件是最常用的操作,介绍使用edit命令通过文件路径来打开文件,使用write命令保存文件,当文件路径不存在或用户权限不匹配时,使用write命令调用外部shell程序完成操作. 1. edit命令打开文件 Vim命令 :edit (缩写形式为 :e) 可以通过文件的绝对路径或相对路径来打开文件. 如果想打开的文件和当前活动文件缓冲区中的文件在同一个目录里,可以使用 :edit %:h<Tab>命令. % 符号代表活动缓冲区的完整文件路径,:h 修饰符会去除路径中的文件名,

win8不通过新打开窗口进行打开和保存文件,点击即显示文本内容,点击即保存文件

前面的一篇博客讲到通过打开新窗口进行打开和保存文件,这一篇则重点介绍怎样一步到位来进行打开和保存文件(这里还以.txt文本文件为例). 我们要打开.txt文本文件,无非是想要里面的文本内容,并且把其显示到页面里罢了,那我们能不能直接点击按钮就让文件的内容自动显示到页面上............ 而不需要经过打开新窗口这一步骤呢 同样,我们点击保存按钮,能不能就直接给文件命名为自己想要的名称,而且直接保存到自己想要的位置或者文件里........ 而不经过打开新窗口定位某一个具体的文件夹下这一步骤

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

格式说明: explicit CFileDialog( BOOL bOpenFileDialog,                         //TRUE 为打开, FALSE 为保存 LPCTSTR lpszDefExt = NULL,                 // 默认文件扩展名 LPCTSTR lpszFileName = NULL,            //文件对话框中 初始的文件名称 DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVER

Linux下使用vi新建文件保存文件时遇到错误:E212: Can&#39;t open file for writing

出现E212: Can't open file for writing的问题是由于权限问题导致的,解决方法有以下思路: 1.使用root进行登录,然后再操作. 2.在使用命令时,前面加sudo. 3.如果是多级文件夹的文件时,由于这个文件夹没有创建,所以要先创建这个文件夹,再来操作这个文件. Linux下使用vi新建文件保存文件时遇到错误:E212: Can't open file for writing

MFC中CFileDialog打开和保存文件对话框(转)

首先我先写一段在VC6.0上打开/保存文件对话框的程序:        CString   FilePathName;//文件名参数定义    CFileDialog  Dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,"TXT Files(*.txt)|*.txt|All Files(*.*)|*.*");     //打开文件    if(Dlg.DoModal() == IDOK)//是否打开成功    {   

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

一.打开文件 Stream myStream = null;            OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.InitialDirectory = "d:\\";            openFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**"

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

一.打开文件 Stream myStream = null;            OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.InitialDirectory = "d:\\";            openFileDialog1.Filter = "ext files (*.txt)|*.txt|All files(*.*)|*>**"

win8中如何通过新打开的窗口进行打开或保存文件

一.硬件基础 1.硬件框图 2.LCD控制器 了解硬件最直接的办法就是看手册,在这里我只会简单介绍下LCD的硬件.具体的我会在下面结合程序讲解. a.REGBANK是LCD控制器的寄存器,含17个寄存器以及一块256*16的调色内存,用来设置各项参数. b.LCDCDMA是LCD控制器专用的DMA信道. c.TIMEGEN和LPC3600负责产生LCD屏所需要的控制时序. d.VIDPRCS需要与LCDCDMA中的数组合成特定的格式,然后从VD[23:0]发送给LCD屏幕. 3.时序理解 二.驱