2016-09-03 2 views
0

J'ai du mal à implémenter IDialogService défini dans l'assembly GalaSoft.MvvmLight. Vous pouvez obtenir ce à partir du paquet NuGet MvvmLightLibsComment implémenter IDialogService

public interface IDialogService 
{ 

    Task ShowError(string message, string title, string buttonText, Action afterHideCallback); 
    Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback); 
    Task ShowMessage(string message, string title); 
    Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback); 
    Task<bool> ShowMessage(string message, string title, string buttonConfirmText, string buttonCancelText, Action<bool> afterHideCallback); 
    Task ShowMessageBox(string message, string title); 
} 

Je suis en train de mettre en œuvre la première méthode comme si

public Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback) 
    { 
     //var result = MessageBox.Show(""); 
     return Dispatcher.Invoke(() => MessageBox.Show("whatever")); 
     //return Dispatcher.BeginInvoke(delegate() { MessageBox.Show("your stuff"); }); 
     //var dg = new Action(() => { MessageBox.Show("", ""); }); 
     //return Dispatcher.CurrentDispatcher.BeginInvoke(dg); 
    }  

Comment puis-je utiliser Messagebox.Show cette méthode async pour WPF? S'il vous plaît aider.

J'essaie de voir qui fonctionne, mais jusqu'ici pas de chance. Quelqu'un peut-il m'aider s'il vous plaît?

+0

Vous voulez implémenter ShowError en tant que méthode asynchrone? – Joe

+0

oui, messagebo dans une méthode asynchrone – VivekDev

Répondre

3

Vous dites que vous voulez que votre showError soit async, mais n'ont pas réellement marqué la méthode avec le mot-clé async, ni ne rien attendent:

public async Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback) 
    { 
     await Dispatcher.InvokeAsync(() => MessageBox.Show("whatever")); 
     Console.WriteLine("awaited Box closed"); 
    } 

Exemple d'utilisation:

private async void Button_Click(object sender, RoutedEventArgs e) 
    { 
     await ShowError(new Exception("Test"), "test", "um", null); 
     Console.WriteLine("awaited showError"); 
    }