I have a customized ContentDialog
as shown below to display a progressbar for I/O bounded tasks in WinUI3
:
public class ExtendedContentDialog: ContentDialog
{
public static readonly DependencyProperty ResultProperty = DependencyProperty.Register(nameof(Result), typeof(ContentDialogResult?), typeof(ExtendedContentDialog), new PropertyMetadata(null, ResultUpdated));
public ContentDialogResult? Result
{
get => (ContentDialogResult?)GetValue(ResultProperty);
set => SetValue(ResultProperty, value);
}
public static void ResultUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
}
public async new Task<ContentDialogResult> ShowAsync()
{
var result = await base.ShowAsync();
if (Result == null)
Result = result;
return result;
}
}
I have a NavigationView
which contains several pages. In one of these pages I have some controls which takes long to load itself. behind this loading I need to show a ContentDialog
which contains a progress bar:
public sealed partial class ProcessingDialog : ExtendedContentDialog
{
public ProcessingDialog(IEnumerable<ProcessingTask> processingTasks)
{
this.InitializeComponent();
Exceptions = new List<Exception>();
Task.Run(() =>
{
foreach (var task in processingTasks)
{
try
{
DispatcherQueue.TryEnqueue(new DispatcherQueueHandler(() =>
{
CurrentTaskTitle = task.Title;
}));
task.Action.Invoke();
}
catch (Exception ex)
{
Exceptions.Add(ex);
}
}
DispatcherQueue.TryEnqueue(new DispatcherQueueHandler(() =>
{
this.Hide();
}));
});
}
public static readonly DependencyProperty CurrentTaskTitleProperty = DependencyProperty.Register(nameof(CurrentTaskTitle), typeof(string), typeof(ProcessingDialog), new PropertyMetadata(""));
public string CurrentTaskTitle
{
get => (string)GetValue(CurrentTaskTitleProperty);
set => SetValue(CurrentTaskTitleProperty, value);
}
public IList<Exception> Exceptions { get; set; }
public DispatcherQueue DispatcherQueue { get; } = DispatcherQueue.GetForCurrentThread();
}
I’m just trying to show this ContentDialog
in the PageLoaded
event handler of the specified page. This dialog is not getting appeared but the task inside it will be executed.
To show a ContentDialog
I’m using following method:
public async Task<T> ShowDialogAsync<T>() where T : ExtendedContentDialog
{
try
{
T dialog = Activator.CreateInstance<T>();
// XamlRoot must be set in the case of a ContentDialog running in a Desktop app
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.XamlRoot = App.MainWindow.Content.XamlRoot;
//_semaphoreSlim.Wait();
await dialog.ShowAsync();
//_semaphoreSlim.Release();
return dialog;
}
catch
{
_semaphoreSlim.Release();
return null;
}
}
I have no idea about what causes this issue. Maybe I should not use DispatcherQueue
in my Actions
like this:
var loadingDialog = new ProcessingDialog(new List<ProcessingTask>
{
new ProcessingTask
{
Title = "Loading signals....",
Action = () =>
{
// I/O Bounded Staffs.
await DispatcherQueue.EnqueueAsync(() =>
{
SubjectFullName = _record.SubjectFullName;
RecordDateTime = _record.CreatedAt.ToString("yyyy/MM/dd HH:mm tt");
});
}
}
}
ShowDialogAsync(loadingDialog);
Sorry for this complexity but my issue is a little bit complex.
Tnx for any suggestion ????