Example #1
0
        void CachedFileUpdaterUI_FileUpdateRequested(CachedFileUpdaterUI sender, FileUpdateRequestedEventArgs args)
        {
            fileUpdateRequest         = args.Request;
            fileUpdateRequestDeferral = fileUpdateRequest.GetDeferral();
            switch (cachedFileUpdaterUI.UIStatus)
            {
            case UIStatus.Hidden:
                if (fileUpdateRequest.File.Name.Contains("ConflictingFile"))
                {
                    fileUpdateRequest.Status = FileUpdateStatus.UserInputNeeded;
                }
                else
                {
                    fileUpdateRequest.Status = FileUpdateStatus.Complete;
                }
                fileUpdateRequestDeferral.Complete();
                break;

            case UIStatus.Visible:
                break;

            case UIStatus.Unavailable:
                fileUpdateRequest.Status = FileUpdateStatus.Failed;
                fileUpdateRequestDeferral.Complete();
                break;
            }
        }
        async void _cachedFileUpdaterUI_FileUpdateRequested(CachedFileUpdaterUI sender, FileUpdateRequestedEventArgs args)
        {
            _fileUpdateRequest = args.Request;
            FileUpdateRequestDeferral fileUpdateRequestDeferral = _fileUpdateRequest.GetDeferral();

            if (_cachedFileUpdaterUI.UpdateTarget == CachedFileTarget.Local) // 由 CachedFileUpdater 更新 CachedFile(CachedFileTarget.Local 方式)
            {
                // 开始激活 CachedFileUpdater 时,是 UIStatus.Hidden 状态的
                if (_cachedFileUpdaterUI.UIStatus == UIStatus.Hidden)
                {
                    // 下面针对两种方式分别写示例

                    // 方式一:直接更新文件,并设置为 FileUpdateStatus.Complete 状态,最后完成
                    if (DateTime.Now.Second % 2 == 0)
                    {
                        await FileIO.AppendTextAsync(_fileUpdateRequest.File, Environment.NewLine + "由 CachedFileUpdater 更新:" + DateTime.Now.ToString());

                        _fileUpdateRequest.Status = FileUpdateStatus.Complete;
                        fileUpdateRequestDeferral.Complete();
                    }
                    // 方式二:设置为 FileUpdateStatus.UserInputNeeded 状态,并完成,之后会再次触发这个事件,并且变为 UIStatus.Visible 状态,弹出本页界面,
                    // 这样的话可以在用户做一些操作之后再更新文件(参见下面的 btnUpdate_Click 部分)
                    else
                    {
                        _fileUpdateRequest.Status = FileUpdateStatus.UserInputNeeded;
                        fileUpdateRequestDeferral.Complete();
                    }
                }
            }
            else if (_cachedFileUpdaterUI.UpdateTarget == CachedFileTarget.Remote) // 由 app 更新 CachedFile(CachedFileTarget.Remote 方式)
            {
                // 这里可以拿到 app 更新后的文件的结果
                string textContent = await FileIO.ReadTextAsync(_fileUpdateRequest.File);

                // 但是这里不能修改这个文件,否则会报错
                // await FileIO.AppendTextAsync(_fileUpdateRequest.File, Environment.NewLine + "由 CachedFileUpdater 更新:" + DateTime.Now.ToString());

                // CachedFileUpdater 返回给 app 一个 FileUpdateStatus 状态
                _fileUpdateRequest.Status = FileUpdateStatus.Complete;
                fileUpdateRequestDeferral.Complete();
            }
        }
Example #3
0
        void CachedFileUpdaterUI_FileUpdateRequested(CachedFileUpdaterUI sender, FileUpdateRequestedEventArgs args)
        {
            fileUpdateRequest         = args.Request;
            fileUpdateRequestDeferral = fileUpdateRequest.GetDeferral();
            switch (cachedFileUpdaterUI.UIStatus)
            {
            case UIStatus.Hidden:
                fileUpdateRequest.Status = FileUpdateStatus.UserInputNeeded;
                fileUpdateRequestDeferral.Complete();
                break;

            case UIStatus.Visible:
                break;

            case UIStatus.Unavailable:
                fileUpdateRequest.Status = FileUpdateStatus.Failed;
                fileUpdateRequestDeferral.Complete();
                break;
            }
        }
        private void FileIsCurrentButton_Click(object sender, RoutedEventArgs e)
        {
            FileUpdateRequest         fileUpdateRequest         = CachedFileUpdaterPage.Current.fileUpdateRequest;
            FileUpdateRequestDeferral fileUpdateRequestDeferral = CachedFileUpdaterPage.Current.fileUpdateRequestDeferral;

            OutputFileAsync(fileUpdateRequest.File);

            fileUpdateRequest.Status = FileUpdateStatus.Complete;
            fileUpdateRequestDeferral.Complete();

            UpdateUI(CachedFileUpdaterPage.Current.cachedFileUpdaterUI.UIStatus);
        }
        private void OverwriteButton_Click(object sender, RoutedEventArgs e)
        {
            FileUpdateRequest         fileUpdateRequest         = CachedFileUpdaterPage.Current.fileUpdateRequest;
            FileUpdateRequestDeferral fileUpdateRequestDeferral = CachedFileUpdaterPage.Current.fileUpdateRequestDeferral;

            // update the remote version of file...
            // Printing the file content
            this.OutputFileAsync(fileUpdateRequest.File);

            fileUpdateRequest.Status = FileUpdateStatus.Complete;
            fileUpdateRequestDeferral.Complete();

            UpdateUI(CachedFileUpdaterPage.Current.cachedFileUpdaterUI.UIStatus);
        }
        private async void ProvideUpdatedVersionButton_Click(object sender, RoutedEventArgs e)
        {
            FileUpdateRequest         fileUpdateRequest         = CachedFileUpdaterPage.Current.fileUpdateRequest;
            FileUpdateRequestDeferral fileUpdateRequestDeferral = CachedFileUpdaterPage.Current.fileUpdateRequestDeferral;

            await FileIO.AppendTextAsync(fileUpdateRequest.File, String.Format(@"{0}New content added @ {1}", System.Environment.NewLine, DateTime.Now.ToString()));

            OutputFileAsync(fileUpdateRequest.File);

            fileUpdateRequest.Status = FileUpdateStatus.Complete;
            fileUpdateRequestDeferral.Complete();

            UpdateUI(CachedFileUpdaterPage.Current.cachedFileUpdaterUI.UIStatus);
        }
Example #7
0
        private void OnFileUpdateRequested(CachedFileUpdaterUI sender, FileUpdateRequestedEventArgs args)
        {
            switch (sender.UIStatus)
            {
            case UIStatus.Hidden:
                fileUpdateRequest         = args.Request;
                fileUpdateRequestDeferral = fileUpdateRequest.GetDeferral();
                // process cached file
                fileUpdateRequestDeferral.Complete();
                break;

            case UIStatus.Visible:
                // use the class fields

                fileUpdateRequestDeferral.Complete();
                break;

            case UIStatus.Unavailable:
                fileUpdateRequest.Status = FileUpdateStatus.Failed;
                fileUpdateRequestDeferral.Complete();
                break;
            }
        }
        private async void RenameButton_Click(object sender, RoutedEventArgs e)
        {
            FileUpdateRequest         fileUpdateRequest         = CachedFileUpdaterPage.Current.fileUpdateRequest;
            FileUpdateRequestDeferral fileUpdateRequestDeferral = CachedFileUpdaterPage.Current.fileUpdateRequestDeferral;

            StorageFile file = await fileUpdateRequest.File.CopyAsync(ApplicationData.Current.LocalFolder, fileUpdateRequest.File.Name, NameCollisionOption.GenerateUniqueName);

            CachedFileUpdater.SetUpdateInformation(file, "CachedFile", ReadActivationMode.NotNeeded, WriteActivationMode.AfterWrite, CachedFileOptions.RequireUpdateOnAccess);
            fileUpdateRequest.UpdateLocalFile(file);

            this.OutputFileAsync(file);

            fileUpdateRequest.Status = FileUpdateStatus.CompleteAndRenamed;
            fileUpdateRequestDeferral.Complete();

            UpdateUI(CachedFileUpdaterPage.Current.cachedFileUpdaterUI.UIStatus);
        }
        private async void btnUpdate_Click(object sender, RoutedEventArgs e)
        {
            FileUpdateRequestDeferral fileUpdateRequestDeferral = _fileUpdateRequest.GetDeferral();

            // 由 CachedFileUpdater 更新 CachedFile,然后返回给 app 一个 FileUpdateStatus 状态
            await FileIO.AppendTextAsync(_fileUpdateRequest.File, Environment.NewLine + "由 CachedFileUpdater 更新:" + DateTime.Now.ToString());

            string fileContent = await FileIO.ReadTextAsync(_fileUpdateRequest.File);

            lblMsg.Text  = "文件名: " + _fileUpdateRequest.File.Name;
            lblMsg.Text += Environment.NewLine;
            lblMsg.Text += "文件内容: " + fileContent;

            _fileUpdateRequest.Status = FileUpdateStatus.Complete;
            fileUpdateRequestDeferral.Complete();

            btnUpdate.IsEnabled = false;
        }