MVVM

2010-09-14 22 views
13

Olası Çoğalt messageBox geçirtmeyi:
How have you successfully implemented MessageBox.Show() functionality in MVVM?MVVM

benim MVVM WPF uygulamasında mesaj kutusunu göstermek istiyorum

. böylece MessageBox.Show aramak yerden ().

+0

bana gerçekten yardımcı oluyor –

+1

Bu çok azalıyor ve çift olarak işaretlendi, ama aynı zamanda en yüksek Google sonucu için 'mvvm messagebox wpf' ... – Benjol

+0

Sadece ... güzel ya da her neyse olmayabilir, ama siz sadece System.Windows.MessageBox.Show (params) arayın; .. herhangi bir sınıftan. – Para

cevap

6

MVVM programcılar içinde OKB bir çizgi (ı deneyimlerimizden biliyoruz) çağırır olduğunu tespit ettik. Bu iyi birşey. Ama bazı şeyler için çaba sadece karmaşıklığı ortadan sırasını sadece kullanıcıya sorulacak tanıtır, özellikle buna değmez "sen xxxx istediğinize emin misiniz?"

Bence MessageBox.Show() kod arkadan çağrılabilir yani, ama asla ViewModel. iletişim kutuları XAML ile daha iyi entegre kadar, sadece isabet almak ve bu konuda kötü hissetmiyorum. Gerçekten WPF UI tasarımının mevcut durumu için gri bir alan. Windows Forms içinde

+2

Komutu kullanıyorum, eğer butonun çalışması ve Execute fonksiyonundan sonra mesaj kutusunu gösterecek olursam, bazı verileri Dosyanın arkasındaki kodu görüntüle, sonra mvvm bozulur. –

13

veya WPF MVVM olmadan, sadece ve o()MessageBox.Show söyleyebiliriz! MVVM'de de aynısını yapsanız iyi olmaz mıydı?

İşte bunu yapmanın bir yolu - ve ben MessageBox.Show alabilirsiniz olduğunca yakın().

İşte MVVM dostu MessageBox_Show()!

( ) İşte

nasıl işliyor: Tüm bu MessageBox_Show aslında yapar

bir olay yangın, bu yüzden mükemmel MVVM dostudur. ViewModel, onu tüketen veya tüketmeyen herhangi bir görünüm hakkında hiçbir şey bilmez ve Windows MessageBox'ın kendi başına gösterimini gerçekleştirmez, bu nedenle aynı zamanda ünite testi de güvenle yapılabilir. aslında bir MessageBox göstermek için neden olacaktır, bir görünümünde kullanmak için

, aynen bu şekilde, olay işleyicisi içinde e.Show() olaya abone ve çağrı:

public partial class MyView : UserControl 
{ 
    public MyView() 
    { 
     InitializeComponent(); 

     this.DataContext = new MyViewModel(); 
     (this.DataContext as MyViewModel).MessageBoxRequest += new EventHandler<MvvmMessageBoxEventArgs>(MyView_MessageBoxRequest); 
    } 

    void MyView_MessageBoxRequest(object sender, MvvmMessageBoxEventArgs e) 
    { 
     e.Show(); 
    } 
} 

Ve MVVM dostu Windows MessageBox'larını göstermek için yapmanız gereken tek şey budur.

Aşağıdaki kodun yalnızca projenizde bir kez uygulanması veya yeniden kullanılabilir bir paylaşılan kitaplığa yerleştirilmesi gerekir.

public class MvvmMessageBoxEventArgs : EventArgs 
{ 
    public MvvmMessageBoxEventArgs(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None) 
    { 
     this.resultAction = resultAction; 
     this.messageBoxText = messageBoxText; 
     this.caption = caption; 
     this.button = button; 
     this.icon = icon; 
     this.defaultResult = defaultResult; 
     this.options = options; 
    } 

    Action<MessageBoxResult> resultAction; 
    string messageBoxText; 
    string caption; 
    MessageBoxButton button; 
    MessageBoxImage icon; 
    MessageBoxResult defaultResult; 
    MessageBoxOptions options; 

    public void Show(Window owner) 
    { 
     MessageBoxResult messageBoxResult = MessageBox.Show(owner, messageBoxText, caption, button, icon, defaultResult, options); 
     if (resultAction != null)resultAction(messageBoxResult); 
    } 

    public void Show() 
    { 
     MessageBoxResult messageBoxResult = MessageBox.Show(messageBoxText, caption, button, icon, defaultResult, options); 
     if (resultAction != null) resultAction(messageBoxResult); 
    } 
} 

Birim test kolaydır: olay işleyicisi için EventArgs sınıfı eklemek sonra

public class ViewModelBase : INotifyPropertyChanged 
{ 
    //... 

    public event EventHandler<MvvmMessageBoxEventArgs> MessageBoxRequest; 
    protected void MessageBox_Show(Action<MessageBoxResult> resultAction, string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.None, MessageBoxOptions options = MessageBoxOptions.None) 
    { 
     if (this.MessageBoxRequest != null) 
     { 
      this.MessageBoxRequest(this, new MvvmMessageBoxEventArgs(resultAction, messageBoxText, caption, button, icon, defaultResult, options)); 
     } 
    } 
} 

Ve:

herhangi ViewModel dan kullanılabilir böylece ViewModel temel sınıf için bu ekleyin: Kodlama

target.AskTheQuestion(); 
target.ProcessTheAnswer(System.Windows.MessageBoxResult.Yes); 

Mutlu!

+0

Çok faydalı! Teşekkürler – Latrova

+0

İyi uygulama, ancak Olaylar'ı kullandığından, bu, çok sayıda Görüntülemeniz olduğunda kesinlikle bellek sızıntısına neden olacaktır. –

+0

Ancak MessageBoxResult için ViewModelBase içinde System.Windows için başvuruları kullanarak, MessageBoxOptions MVVM amacı yenmek değil mi? –