public BaseApiResult SetupPlayer(WallpaperType wpType, string customDownloadUrl) { string url = customDownloadUrl; if (string.IsNullOrEmpty(url)) { url = WallpaperApi.PlayerUrls.FirstOrDefault(m => m.Type == wpType).DownloadUrl; } void WallpaperManager_SetupPlayerProgressChangedEvent(object sender, SetupPlayerProgressChangedArgs e) { _lastSetupPlayerRaiseLimiter.Execute(async() => { try { System.Diagnostics.Debug.WriteLine($"{e.ProgressPercentage} {e.ActionType}"); //向所有客户端推送,刷新后也能显示 var client = _hubEventEmitter.AllClient(); await client.SendAsync("SetupPlayerProgressChanged", e); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex); } }, 1000); } _lastSetupPlayerRaiseLimiter = new RaiseLimiter(); WallpaperApi.SetupPlayerProgressChangedEvent -= WallpaperManager_SetupPlayerProgressChangedEvent; WallpaperApi.SetupPlayerProgressChangedEvent += WallpaperManager_SetupPlayerProgressChangedEvent; var result = WallpaperApi.SetupPlayer(wpType, url, (async _ => { //设置完成 await _lastSetupPlayerRaiseLimiter.WaitExit(); WallpaperApi.SetupPlayerProgressChangedEvent -= WallpaperManager_SetupPlayerProgressChangedEvent; })); if (result.Ok) { //开始成功 //_lastSetupPlayerRaiseLimiter = new RaiseLimiter(); //WallpaperApi.SetupPlayerProgressChangedEvent += WallpaperManager_SetupPlayerProgressChangedEvent; //_lastConnectionId = Context.ConnectionId; } return(result); }
private async void BtnSelect_Click(object sender, RoutedEventArgs e) { //System.Diagnostics.Debug.WriteLine("before ShowWallpaper " + GetActiveWindowTitle()); using (var openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "WallpaperSamples"; openFileDialog.Filter = "All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var displayScreen = monitorsVM.Where(m => m.Checked).Select(m => m.DeviceName).ToArray(); BtnApply_Click(null, null); var showResult = await WallpaperApi.ShowWallpaper(openFileDialog.FileName, displayScreen); var wp = showResult.Data; if (!showResult.Ok) { if (showResult.Error == ErrorType.NoPlayer) { var r = System.Windows.MessageBox.Show($"{showResult.Error} {showResult.Message}, Whether to download the player?", "", MessageBoxButton.OKCancel); if (r == MessageBoxResult.OK) { popup.Visibility = Visibility.Visible; txtPopup.Text = "downloading..."; var url = WallpaperApi.PlayerUrls.FirstOrDefault(m => m.Type == wp.RunningData.Type).DownloadUrl; void WallpaperManager_SetupPlayerProgressChangedEvent(object sender, SetupPlayerProgressChangedArgs e) { Dispatcher.BeginInvoke(new Action(async() => { txtPopup.Text = $"{(e.ActionType == SetupPlayerProgressChangedArgs.Type.Unpacking ? "unpacking" : "downloading")} ... {(int)(e.ProgressPercentage * 100)}%"; if (e.AllCompleted) { WallpaperApi.SetupPlayerProgressChangedEvent -= WallpaperManager_SetupPlayerProgressChangedEvent; popup.Visibility = Visibility.Collapsed; if (e.Result.Ok) { showResult = await WallpaperApi.ShowWallpaper(wp, displayScreen); } else { System.Windows.Forms.MessageBox.Show($"Message:{e.Result.Message},Error:{e.Result.Error}"); } } })); } WallpaperApi.SetupPlayerProgressChangedEvent -= WallpaperManager_SetupPlayerProgressChangedEvent; WallpaperApi.SetupPlayerProgressChangedEvent += WallpaperManager_SetupPlayerProgressChangedEvent; var setupResult = WallpaperApi.SetupPlayer(wp.RunningData.Type.Value, url); } } else { System.Windows.MessageBox.Show($"{showResult.Error} {showResult.Message} "); } } } } //System.Diagnostics.Debug.WriteLine("after ShowWallpaper" + GetActiveWindowTitle()); //IntPtr progman = User32Wrapper.FindWindow("Progman", null); //User32Wrapper.SetForegroundWindow(window); //change focus from the started window//application. //User32Wrapper.SetFocus(window); Activate(); }