I have a helper method that shows an ContentDialog with an input box that accepts string data. My issue is that it takes about 1 second after OK is clicked before the caller gets the string back (and it's very noticeable). I theorized that maybe it was the dialog fading out default animation/transition to end before the dialog returned.
In the code below, there's about a 1 second delay between when OK is clicked on the dialog and when the "return textBox.Text" fires.
/// <summary>
/// Shows an simple input box to get a string value in return.
/// </summary>
/// <param name="title">The title to show at the top of the dialog.</param>
/// <param name="message">The message shown directly above the input box.</param>
/// <param name="defaultValue">A value to prepopulate in the input box if any.</param>
/// <returns></returns>
public static async Task<string> ShowInput(string message, string defaultValue, string title)
{
var dialog = new ContentDialog
{
Title = title
};
var panel = new StackPanel();
panel.Children.Add(new TextBlock { Text = message, TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap });
var textBox = new TextBox();
textBox.Text = defaultValue;
textBox.SelectAll();
textBox.KeyUp += (o, e) =>
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
dialog.Hide();
}
e.Handled = true;
};
panel.Children.Add(textBox);
dialog.Content = panel;
dialog.PrimaryButtonText = "OK";
await dialog.ShowAsync();
return textBox.Text;
}
My questions are:
Am I missing something I should be setting or is this delay after clicking OK on the ContentDialog out the out of the box behavior?
If it is caused by a transition can I disable it?
Is there anyway I can speed up the time between when OK is clicked and the await returns?
I am running against 1809 Build 17763.379.
Thank you in advance.
The following solution is not really a performance fix, since there is nothing wrong with the code you have posted.
The basic idea is - since this process takes a while based on the device you can just show a spinner / loading screen just before calling your ShowInput() method and hiding it after you are done using the value/text that is returned by the ShowInput().
For example you can try something like this :
showLoader();
string x = await ShowInput("message","","title");
...
//some more code
...
hideLoader();
Where showLoader/hideLoader will show or hide a lightweight loader screen something like this :
This will make sure user waits until the code has completed execution. (This might be overkill if you are doing something very trivial with the text input)
As Pratyay mentioned in his comment, the amount of time will vary by device.
But as far as the await keyword goes, I believe what you are experiencing is intended behavior.
The await operator is applied to a task in an asynchronous method to insert a suspension point in the execution of the method until the awaited task completes. The task represents ongoing work.
Source # Microsoft Doc
This means that your ShowInput function will return its Task<string> object as soon as it reaches the await keyword. Then after dialog.ShowAsync(); returns it will continue the ShowInput function asynchronously and place the results into the Task object for you to retrieve.
So although your ShowInput function should return almost immediately. You will notice a delay between dialog.ShowAsync(); and return textBox.Text;.
The other thing to keep in mind is that when a window closes, there is usually a bit of processing (disposing of resources, etc.) before the window loop finishes. The way your code is written, you will have to wait until all of that finishes before you get your result.
Is there anyway I can speed up the time between when OK is clicked and the await returns?
I figure the quickest way to get your response back is to not await the ContentDialog, but rather await a signal that occurs right when the content is available.
public static Task<string> ShowInput(string message, string defaultValue, string title)
{
var dialog = new ContentDialog { Title = title };
var panel = new StackPanel();
panel.Children.Add(new TextBlock { Text = message, TextWrapping = Windows.UI.Xaml.TextWrapping.Wrap });
var textBox = new TextBox() { Text = defaultValue };
textBox.SelectAll();
var signal = new TaskCompletionSource<string>();
textBox.KeyUp += (o, e) =>
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
dialog.Hide();
signal.SetResult(textBox.Text);
}
e.Handled = true;
};
dialog.PrimaryButtonClick += (o, e) =>
{
dialog.Hide();
signal.SetResult(textBox.Text);
};
panel.Children.Add(textBox);
dialog.Content = panel;
dialog.PrimaryButtonText = "OK";
dialog.ShowAsync();
return signal.Task;
}
Edit: Doing it this way no longer needs the extra await, as the Task being created has the final result in it.
Related
I have a Web Forms app where I need to show a progress spinner when button is clicked, and hide it when a task finishes executing. I have tried a bunch of different ways to do this. I can only get either the hide or show functionality to work properly but not both. For the code below, the spinner isn't shown until after the page reloads (I tested this by removing the hide spinner part). So basically, the hiding part occurs and works as expected but the show part does not.
As a side note, the page is in a re-load state for the duration of this method. Thanks for any help!
protected async void btnPullQBData_Click(object sender, EventArgs e)
{
loadingContainer.Visible = true;
string type = "";
string response = "";
response = await SyncData();
loadingContainer.Visible = false;
}
public Task<string> SyncData()
{
return Task.Run(() =>
{
QBService service = new QBService(User.Identity.GetUserId());
string response = service.RefreshDBFromQB();
return response;
});
}
I have a method which I am using to enable a user to input time. I have set it up that once a button is pressed it opens a dialog. The user can then select the time, once the user selects the time it is meant to populate a textview however I have found that once the dialog opens the textview is assigned its default value of null. It doesn't wait.
When i have ran tests using Toast messages I can see that the underlying code does work however its the order which is messing me about.
Currently I have attempting to implement the await feature with no avail.
I have attached my code. thanks for any help or advice. I am reasonably new to c# and xamarin by the way.
<<within on create>>
StartTimePickerBTN.Click += async delegate
{
OnCreateDialog().Show();
StartTimeTV.Text = (await UpdateTime()).ToString();
};
private void TimePickerCallback(object sender, TimePickerDialog.TimeSetEventArgs e)
{
hour = e.HourOfDay;
minute = e.Minute;
}
public async Task<string> UpdateTime()
{
string time = string.Format("{0}:{1}", hour, minute.ToString().PadLeft(2, '0'));
return time;
}
You are not awaiting the call to UpdateTime, instead you are calling .ToString() on the Task that is returned, instead of the string.
Your event handler should do
StartTimeTV.Text = (await UpdateTime()).ToString();
I managed to resolve my issue however i'm sure its not the best way but i will post my code below.
The plus is now the TextView is populated at the correct stage and waits until user selection.
Within OnCreate, Please note the bool is a class wide variable
StartTimePickerBTN.Click += delegate
{
SelectStartOrEndTime = true;
OnCreateDialog().Show();
};
EndTimePickerBTN.Click += delegate
{
SelectStartOrEndTime = false;
OnCreateDialog().Show();
};
protected Dialog OnCreateDialog()
{
return new TimePickerDialog(this, TimePickerCallback, hour, minute, false);
}
private void TimePickerCallback(object sender, TimePickerDialog.TimeSetEventArgs e)
{
hour = e.HourOfDay;
minute = e.Minute;
UpdateTime();
}
private void UpdateTime()
{
if (SelectStartOrEndTime == true)
{
StartTimeTV.Text = string.Format("{0}:{1}", hour, minute.ToString().PadLeft(2, '0'));
}
else
{
EndTimeTV.Text = string.Format("{0}:{1}", hour, minute.ToString().PadLeft(2, '0'));
}
}
I am using Windows.UI.Xaml.Controls.ContentDialog to show a confirmation. And based on the response from the first dialog I would (or would not) show another dialog. But, when I am trying to open the second content dialog it throws : "Only a single ContentDialog can be open at any time." error. Even though in the UI, first dialog would be closed but somehow I am still not able to open the second dialog. Any idea?
I have created some code to handle this type of conundrum in my Apps:
public static class ContentDialogMaker
{
public static async void CreateContentDialog(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
public static async Task CreateContentDialogAsync(ContentDialog Dialog, bool awaitPreviousDialog) { await CreateDialog(Dialog, awaitPreviousDialog); }
static async Task CreateDialog(ContentDialog Dialog, bool awaitPreviousDialog)
{
if (ActiveDialog != null)
{
if (awaitPreviousDialog)
{
await DialogAwaiter.Task;
DialogAwaiter = new TaskCompletionSource<bool>();
}
else ActiveDialog.Hide();
}
ActiveDialog = Dialog;
ActiveDialog.Closed += ActiveDialog_Closed;
await ActiveDialog.ShowAsync();
ActiveDialog.Closed -= ActiveDialog_Closed;
}
public static ContentDialog ActiveDialog;
static TaskCompletionSource<bool> DialogAwaiter = new TaskCompletionSource<bool>();
private static void ActiveDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) { DialogAwaiter.SetResult(true); }
}
To use these Methods, you need to create the ContentDialog and its content in a variable, then pass the variable, and bool into the Method.
Use CreateContentDialogAsync(), if you require a callback in your app code, say if you have a button in your Dialog, and you want wait for a button press, and then get the value from the form in code after the dialog.
Use CreateContentDialog(), if you don't need to wait for the Dialog to complete in your UI Code.
Use awaitPreviousDialog to wait for the previous dialog to complete before showing the next Dialog, or set false, to remove the previous Dialog, then show the next Dialog, say, if you want to show an Error Box, or the next Dialog is more important.
Example:
await ContentDialogMaker.CreateContentDialogAsync(new ContentDialog
{
Title = "Warning",
Content = new TextBlock
{
Text = "Roaming Appdata Quota has been reached, if you are seeing this please let me know via feedback and bug reporting, this means that any further changes to data will not be synced across devices.",
TextWrapping = TextWrapping.Wrap
},
PrimaryButtonText = "OK"
}, awaitPreviousDialog: true);
William Bradley's approach above is good. Just to polish it up a bit, here is an extension method to submit and await the showing of a content dialog; the dialog will be shown after all the other content dialogs that have already been submitted. Note: by the time the user clicks through earlier backlogged dialogs you may no longer want to show the dialog that you have submitted; to indicate this you may pass a predicate that will be tested after the other dialogs have been dismissed.
static public class ContentDialogExtensions
{
static public async Task<ContentDialogResult> EnqueueAndShowIfAsync( this ContentDialog contentDialog, Func<bool> predicate = null)
{
TaskCompletionSource<Null> currentDialogCompletion = new TaskCompletionSource<Null>();
TaskCompletionSource<Null> previousDialogCompletion = null;
// No locking needed since we are always on the UI thread.
if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
previousDialogCompletion = ContentDialogExtensions.PreviousDialogCompletion;
ContentDialogExtensions.PreviousDialogCompletion = currentDialogCompletion;
if (previousDialogCompletion != null) {
await previousDialogCompletion.Task;
}
var whichButtonWasPressed = ContentDialogResult.None;
if (predicate == null || predicate()) {
whichButtonWasPressed = await contentDialog.ShowAsync();
}
currentDialogCompletion.SetResult(null);
return whichButtonWasPressed;
}
static private TaskCompletionSource<Null> PreviousDialogCompletion = null;
}
Another way might be to use a SemaphoreSlim(1,1).
"Only a single ContentDialog can be open at a time"
This statement is not entirely true. You can only ShowAsync one ContentDialog at a time. All you need to do is hide the current ContentDialog before opening another one. Then, after the "await ShowAsync" of the second ContentDailog, your simply call "var T = this.ShowAync()" to unhide it. Example:
public sealed partial class MyDialog2 : ContentDialog
{
...
}
public sealed partial class MyDialog1 : ContentDialog
{
...
private async void Button1_Click(object sender, RoutedEventArgs e)
{
// Hide MyDialog1
this.Hide();
// Show MyDialog2 from MyDialog1
var C = new MyDialog2();
await C.ShowAsync();
// Unhide MyDialog1
var T = ShowAsync();
}
}
I know this is slightly old, but one simpler solution instead of going through all this pain is to just register a callback for the ContentDialog_Closed event. By this point you can be sure the previous dialog has been closed, and can open your next dialog. :)
Only a single ContentDialog can be open at any time.
That is a fact. (I was really surprised, but just for a moment)
You can't have more than one at any time and it is more like guideline from Microsoft, because it's really messy to have multiple dialogs on top of each other filled with content.
Try to change your UX to display only one sophisticated ContentDialog and for all other messages use MessageDialog - it supports multiple buttons(only two for phones, but more on desktop) for user response but without Checkboxes or similar "smart"-content stuff.
In my case MessageDialogs were really helpful, but in some areas I used chained ContentDialogs but for that you must await the first one, and open second right after without any exceptions. In your case it seems like ContentDialog was not fully closed when you tried to open next one.
Hope it helps!
I like this answer https://stackoverflow.com/a/47986634/942855, this will allow us ot handle binding all events.
So extended it a little to check the multiple calls to show dialog.
private int _dialogDisplayCount;
private async void Logout_OnClick(object sender, RoutedEventArgs e)
{
try
{
_dialogDisplayCount++;
ContentDialog noWifiDialog = new ContentDialog
{
Title = "Logout",
Content = "Are you sure, you want to Logout?",
PrimaryButtonText = "Yes",
CloseButtonText = "No"
};
noWifiDialog.PrimaryButtonClick += ContentDialog_PrimaryButtonClick;
//await noWifiDialog.ShowAsync();
await noWifiDialog.EnqueueAndShowIfAsync(() => _dialogDisplayCount);
}
catch (Exception exception)
{
_rootPage.NotifyUser(exception.ToString(), NotifyType.DebugErrorMessage);
}
finally
{
_dialogDisplayCount = 0;
}
}
modified predicate
public class Null { private Null() { } }
public static class ContentDialogExtensions
{
public static async Task<ContentDialogResult> EnqueueAndShowIfAsync(this ContentDialog contentDialog, Func<int> predicate = null)
{
TaskCompletionSource<Null> currentDialogCompletion = new TaskCompletionSource<Null>();
// No locking needed since we are always on the UI thread.
if (!CoreApplication.MainView.CoreWindow.Dispatcher.HasThreadAccess) { throw new NotSupportedException("Can only show dialog from UI thread."); }
var previousDialogCompletion = _previousDialogCompletion;
_previousDialogCompletion = currentDialogCompletion;
if (previousDialogCompletion != null)
{
await previousDialogCompletion.Task;
}
var whichButtonWasPressed = ContentDialogResult.None;
if (predicate == null || predicate() <=1)
{
whichButtonWasPressed = await contentDialog.ShowAsync();
}
currentDialogCompletion.SetResult(null);
return whichButtonWasPressed;
}
private static TaskCompletionSource<Null> _previousDialogCompletion;
}
I have some code that when I call my CustomMessageBox it displays the box with a user prompt for an amount of my object to add, once that is done I have it added to a list of objects. Once added, it then Displays a MessageBox.Show to just let the user know it was added.
My problem is that when I run the code it executes all the code, bypasses the display of the Custom message box, then displays the MessageBox.Show, and THEN displays the CMB.Show. I ran the code through the debugger and followed the trail and it hits the CMB.Show before the MessageBox.Show, but is displayed once the code is done. Sorry, I am still learning and might not be telling the problem well, please let me know if there is anything I can further explain upon.
Some code:
private int BasicLand(Card basicLand)
{
var countBox = new TextBox
{
Name = "count",
Width = 100,
};
var cmbCount = new CustomMessageBox
{
Caption = "Blah",
Content = countBox,
RightButtonContent = "ok",
};
cmbCount.Dismissed += (s1, e1) =>
{
switch (e1.Result)
{
case CustomMessageBoxResult.RightButton:
if (int.TryParse(countBox.Text, out tempInt) && Convert.ToInt32(countBox.Text) > 0)
{
countReturn = Convert.ToInt32(tempInt);
break;
}
else
{
//Some code for error....
}
}
};
cmbCount.Show();
return countReturn;
}
Then the other part that triggers first but is last in the code block.
MessageBox.Show("Object was added to List!");
I tried adding the ShowDialog to the custom box but it came up broken in VS. BasicLand is called within another method and when the object is added to the list it will display the MessageBox.Show.
The problem with your code is, it does not take into account that any user interaction is asynchronous. When you call Show() it will actually show the messagebox, but it will not block your currently running thread, the other statements after the call to Show() will be executed immediately and thus your method returns a returnvalue that has not been provided by the user but is just the default. To fix this you have to write your code in continuations.
private void PromtUserForFeeblefezerAmount(Action<int> continueFeeblefzing, Action cancel)
{
var messagebox = CreateFeeblefezerPromt();
messagebox.Dismissed += (sender, args) =>
{
if ( args.Result == CustomMessageBoxResult.RightButton )
continueFeeblefzing( GetFeeblefezerAmount(messagebox) );
else
cancel();
};
messagebox.Show();
}
If you call the ShowAsync command on a MessageDialog object when another MessageDialog object has already been displayed to the user but not dismissed (i.e. you show a popup when another one is already up), an UnauthorizedAccessException is thrown. This can make things difficult when you have multiple threads attempting to alert the user at the same time.
My current (stopgap) solution is merely to surround the ShowAsync call with a try/catch block and swallow the exception. This undesirably leads to the user missing out on subsequent notifications. The only other way around this that I can think of is to manually implement some sort of popup queue. This seems like an inordinate amount of work, however, considering other frameworks (like Windows Phone) do not have this issue and will merely display the popups one after another as the user dismisses them.
Is there another way to solve this problem?
You can easy do it with this extension method:
public static class MessageDialogShower
{
private static SemaphoreSlim _semaphore;
static MessageDialogShower()
{
_semaphore = new SemaphoreSlim(1);
}
public static async Task<IUICommand> ShowDialogSafely(this MessageDialog dialog)
{
await _semaphore.WaitAsync();
var result = await dialog.ShowAsync();
_semaphore.Release();
return result;
}
}
There are many ways to approach it and the choice might depend on your skills, requirements and preferences.
My personal choice is to avoid using dialog boxes altogether since they are bad for user experience (evil). There are then alternative solutions like displaying a separate screen/page with the UI requiring user to provide some input when it really is required or displaying a non-modal popup somewhere on the side/edge/corner if the user input is optional and hiding it after a moment or some other sort of notification that doesn't break user flow.
If you disagree or don't have the time, resources or skills to implement an alternative - you can create some sort of a wrapper around MessageDialog.ShowAsync() call to either queue or ignore new requests while a dialog is already shown.
This class has extension methods to allow to either ignore a new show request when another dialog is already displayed or queue up the requests:
/// <summary>
/// MessageDialog extension methods
/// </summary>
public static class MessageDialogExtensions
{
private static TaskCompletionSource<MessageDialog> _currentDialogShowRequest;
/// <summary>
/// Begins an asynchronous operation showing a dialog.
/// If another dialog is already shown using
/// ShowAsyncQueue or ShowAsyncIfPossible method - it will wait
/// for that previous dialog to be dismissed before showing the new one.
/// </summary>
/// <param name="dialog">The dialog.</param>
/// <returns></returns>
/// <exception cref="System.InvalidOperationException">This method can only be invoked from UI thread.</exception>
public static async Task ShowAsyncQueue(this MessageDialog dialog)
{
if (!Window.Current.Dispatcher.HasThreadAccess)
{
throw new InvalidOperationException("This method can only be invoked from UI thread.");
}
while (_currentDialogShowRequest != null)
{
await _currentDialogShowRequest.Task;
}
var request = _currentDialogShowRequest = new TaskCompletionSource<MessageDialog>();
await dialog.ShowAsync();
_currentDialogShowRequest = null;
request.SetResult(dialog);
}
/// <summary>
/// Begins an asynchronous operation showing a dialog.
/// If another dialog is already shown using
/// ShowAsyncQueue or ShowAsyncIfPossible method - it will wait
/// return immediately and the new dialog won't be displayed.
/// </summary>
/// <param name="dialog">The dialog.</param>
/// <returns></returns>
/// <exception cref="System.InvalidOperationException">This method can only be invoked from UI thread.</exception>
public static async Task ShowAsyncIfPossible(this MessageDialog dialog)
{
if (!Window.Current.Dispatcher.HasThreadAccess)
{
throw new InvalidOperationException("This method can only be invoked from UI thread.");
}
while (_currentDialogShowRequest != null)
{
return;
}
var request = _currentDialogShowRequest = new TaskCompletionSource<MessageDialog>();
await dialog.ShowAsync();
_currentDialogShowRequest = null;
request.SetResult(dialog);
}
}
Test
// This should obviously be displayed
var dialog = new MessageDialog("await ShowAsync", "Dialog 1");
await dialog.ShowAsync();
// This should be displayed because we awaited the previous request to return
dialog = new MessageDialog("await ShowAsync", "Dialog 2");
await dialog.ShowAsync();
// All other requests below are invoked without awaiting
// the preceding ones to complete (dialogs being closed)
// This will show because there is no dialog shown at this time
dialog = new MessageDialog("ShowAsyncIfPossible", "Dialog 3");
dialog.ShowAsyncIfPossible();
// This will not show because there is a dialog shown at this time
dialog = new MessageDialog("ShowAsyncIfPossible", "Dialog 4");
dialog.ShowAsyncIfPossible();
// This will show after Dialog 3 is dismissed
dialog = new MessageDialog("ShowAsyncQueue", "Dialog 5");
dialog.ShowAsyncQueue();
// This will not show because there is a dialog shown at this time (Dialog 3)
dialog = new MessageDialog("ShowAsyncIfPossible", "Dialog 6");
dialog.ShowAsyncIfPossible();
// This will show after Dialog 5 is dismissed
dialog = new MessageDialog("ShowAsyncQueue", "Dialog 7");
dialog.ShowAsyncQueue();
// This will show after Dialog 7 is dismissed
dialog = new MessageDialog("ShowAsyncQueue", "Dialog 8");
dialog.ShowAsyncQueue();