ITNOA
Salut,
J’ai un UWP projet et je veux qu’un message passe entre ViewModel
et View
avec CommunityToolkit MVVM Toolkit.
Comme vous pouvez le voir dans mon ViewModel
nous avons quelque chose comme ci-dessous
private async void CallbackClick()
{
while (true)
{
CallbackDto callback = callbackQueue.Pop();
if (callback is null)
break;
callbackQueue.Push(new CallbackDto(callback.Number, DateTimeOffset.UtcNow.ToUnixTimeSeconds()));
string normalizedAddress = callback.Number;
BSN.LinphoneSDK.Call outgoingCall = await LinphoneManager.Instance.NewOutgoingCall($"{normalizedAddress}");
await outgoingCall.WhenEnded();
// TODO: It is mandatory for backing to Dialer from InCall, but it is very bugous and must fix it
await Task.Delay(1000);
Task cancellationTokenTask = WeakReferenceMessenger.Default.Send();
CancellationToken cancellationToken = await cancellationTokenTask;
if (cancellationToken.IsCancellationRequested)
break;
}
}
Et je me suis inscrit sur message dans Dialer.xaml.cs
dans NavigatedTo
comme ci-dessous
WeakReferenceMessenger.Default.Register(this, async (r, message) =>
{
var continueCallbackAnsweringDialog = new MessageDialog("آیا مایل به ادامه پاسخدهی به تماسهای درخواستی هستید؟");
TaskCompletionSource tcs = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
continueCallbackAnsweringDialog.Commands.Add(new UICommand(
"بلی",
new UICommandInvokedHandler((IUICommand command) =>
{
tcs.SetResult(new CancellationToken(false));
})));
continueCallbackAnsweringDialog.Commands.Add(new UICommand(
"خیر",
new UICommandInvokedHandler((IUICommand command) =>
{
tcs.SetResult(new CancellationToken(true));
})));
continueCallbackAnsweringDialog.DefaultCommandIndex = 0;
continueCallbackAnsweringDialog.CancelCommandIndex = 1;
await continueCallbackAnsweringDialog.ShowAsync();
message.Reply(tcs.Task);
});
Dans ce scénario, j’ai eu un InvalidOperationException
sur WeakReferenceMessenger.Default.Send
. Ma question est comment gérer cette situation ?
D’un autre côté, si j’enlève async
et await
mot-clé dans le gestionnaire, mon code fonctionne correctement. mais le problème c’est que je n’attends pas IAsyncOperation
et je veux await
dessus.
J’ajoute discussion sur CommunityToolkit GitHub à propos de ça.