internal static async Task RunOnCoreDispatcherIfPossible(Func <Task> action, bool runAnyway = true) { CoreDispatcher dispatcher = null; try { if (CustomDispatcher == null) { dispatcher = CoreApplication.MainView?.CoreWindow?.Dispatcher; } else { dispatcher = CustomDispatcher; } } catch { } if (dispatcher != null) { await dispatcher.RunTaskAsync(async() => { await action(); }); } else if (runAnyway) { await action(); } }
/// <summary> /// Executes the action on the UI thread asynchronously. /// </summary> /// <param name="action">The action to execute.</param> /// <returns></returns> public virtual Task OnUIThreadAsync(Func <Task> action) { ValidateDispatcher(); #if WINDOWS_UWP return(dispatcher.RunTaskAsync(action)); #else return(dispatcher.InvokeAsync(action).Task.Unwrap()); #endif }
private static async Task <string> GetPinFromUserAsync(CoreDispatcher dispatcher) { return(await dispatcher.RunTaskAsync(async() => { var pinBox = new TextBox(); var dialog = new ContentDialog() { Title = "Enter Pin", PrimaryButtonText = "OK", Content = pinBox }; await dialog.ShowAsync(); return pinBox.Text; })); }
private static async Task ShowPinToUserAsync(CoreDispatcher dispatcher, string strPin) { await dispatcher.RunTaskAsync(async() => { var messageDialog = new MessageDialog($"Enter this PIN on the remote device: {strPin}"); // Add commands messageDialog.Commands.Add(new UICommand("OK", null, 0)); // Set the command that will be invoked by default messageDialog.DefaultCommandIndex = 0; // Set the command that will be invoked if the user cancels messageDialog.CancelCommandIndex = 0; // Show the Pin await messageDialog.ShowAsync(); }); }
/// <summary> /// Loads songs from a specified folder into the library. <seealso cref="LoadCommand"/> /// </summary> public async void Load() { FolderPicker picker = new FolderPicker() { SuggestedStartLocation = PickerLocationId.MusicLibrary }; picker.FileTypeFilter.Add(".mp3"); StorageFolder folder = await picker.PickSingleFolderAsync(); if (folder != null) { var filelist = await Macalifa.Common.DirectoryWalker.GetFiles(folder.Path); foreach (var x in filelist) { StorageFile file = await StorageFile.GetFileFromPathAsync(x); Path = file.Path; using (var stream = await Dispatcher.RunTaskAsync(GetFileAsStream)) { if (stream != null) { var path = file.Path; if (TracksCollection.Elements.All(t => t.Path != path)) { var m = await CreateMediafile(stream); TracksCollection.AddItem(m); //TracksCollection.Elements.Add(m); //ByStringGroupedCollection.AddItem(Mediafile); db.Insert(m); } } } } } }