Exemple #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;
            }
        }
Exemple #2
0
        public void Activate(CachedFileUpdaterActivatedEventArgs args)
        {
            // cache CachedFileUpdaterUI
            cachedFileUpdaterUI = args.CachedFileUpdaterUI;

            // Currently, the Desktop device family activates the app in the background. Other device families
            // use a background task activation. We need to handle both cases.
            //
            // We can check to see if there is UI on activation to determine which case we are in, because
            // we receive the FileUpdateRequest differently.
            //
            // If activated in the foreground (visible), then the FileUpdateRequest is passed explicitly as
            // part of the activation.
            //
            // If activated in the background (not visible), then we subscribe to the FileUpdateRequested
            // and UIRequest events. The FileUpdateRequest is received as part of the FileUpdatedRequested event,
            // and UIRequested is raised when user input is needed to resolve a conflict.

            bool isForegroundActivated = (cachedFileUpdaterUI.UIStatus == UIStatus.Visible);

            if (isForegroundActivated)
            {
                fileUpdateRequest         = cachedFileUpdaterUI.UpdateRequest;
                fileUpdateRequestDeferral = cachedFileUpdaterUI.GetDeferral();
            }
            else
            {
                cachedFileUpdaterUI.FileUpdateRequested += CachedFileUpdaterUI_FileUpdateRequested;
                cachedFileUpdaterUI.UIRequested         += CachedFileUpdaterUI_UIRequested;
            }

            switch (cachedFileUpdaterUI.UpdateTarget)
            {
            case CachedFileTarget.Local:
                scenarios = new List <Scenario> {
                    new Scenario()
                    {
                        Title = "Get latest version", ClassType = typeof(FilePickerContracts.CachedFileUpdater_Local)
                    }
                };
                break;

            case CachedFileTarget.Remote:
                scenarios = new List <Scenario> {
                    new Scenario()
                    {
                        Title = "Remote file update", ClassType = typeof(FilePickerContracts.CachedFileUpdater_Remote)
                    }
                };
                break;
            }

            if (isForegroundActivated)
            {
                Window.Current.Content = this;
                this.OnNavigatedTo(null);
                Window.Current.Activate();
            }
        }
        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);
        }
Exemple #6
0
 protected override async Task HandleInternal(CachedFileUpdaterActivatedEventArgs args)
 {
     if (args.CachedFileUpdaterUI.UIStatus == UIStatus.Visible)
     {
         // Normal Activation
         fileUpdateRequest         = args.CachedFileUpdaterUI.UpdateRequest;
         fileUpdateRequestDeferral = args.CachedFileUpdaterUI.GetDeferral();
     }
     else
     {
         // Background
         args.CachedFileUpdaterUI.FileUpdateRequested += OnFileUpdateRequested;
         args.CachedFileUpdaterUI.UIRequested         += OnUIRequested;
     }
     await Task.CompletedTask;
 }
        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;
        }
        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();
            }
        }
Exemple #10
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;
            }
        }
Exemple #11
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;
            }
        }