2013-08-26 38 views
0

您好我正在使用WPF和Devexpress!我是c#世界的新手,對MVVM沒有經驗。我看過不同的視頻來學習MVVM。但所有這些都與MVVM的基礎知識有關(爲什麼它很高效等)我在WPF中編寫代碼。我的代碼中有三個按鈕:添加新數據,編輯數據和刷新網格。每個按鈕都有定義它功能的單擊事件。我想將這個簡單的基本WPF代碼轉換成MVVM框架。任何人都可以指導我轉換到MVVM。 如何在MVVM中工作

 void EditRow(int focRowHand, Entities a) 
     { 

     Name nametext = grid.GetRow(focRowHand) as Name; 

     try 
      { 
      if (nametext.Name1 != string.Empty) 
       { 

       update_id = nametext.ID; 
       txtName2.Text = update_text = nametext.Name; 

       if (Panel3.Visibility == System.Windows.Visibility.Visible) 
        { 
        Panel1.Visibility = System.Windows.Visibility.Visible; 
        Panel3.Visibility = System.Windows.Visibility.Collapsed; 
        } 

       else 
        { 
        Panel1.Visibility = System.Windows.Visibility.Collapsed; 
        Panel3.Visibility = System.Windows.Visibility.Visible; 
        } 
       } 
      } 
     catch (Exception err) 
      { 
      DXMessageBox.Show(err.StackTrace); 

      } 

     } 

     private void Button1_Copy_Click(object sender, RoutedEventArgs e) 
      { 

      if (view.FocusedRowHandle == 0) 
       { 
       DXMessageBox.Show("Please Select any Item From Grid List"); 
       } 
      else 
       { 
       try 
        { 
        int FocRowHand = view.FocusedRowHandle; 
        Entities a = new Entities(); 

        if (grid.IsGroupRowHandle(FocRowHand)) 
         { 
         int childCount = grid.GetChildRowCount(FocRowHand); 
         for (int i = 0; i < childCount; i++) 
          { 
          int childHandle = grid.GetChildRowHandle(FocRowHand, i); 
          EditRow(childHandle, a); 
          } 
         } 
        else 
         { 
         EditRow(FocRowHand, a); 
         } 

        } 
       catch (Exception ee) 
        { 
        DXMessageBox.Show(ee.StackTrace); 
        } 
       } 

      } 

插入

private void Insertion() 
    { 
    if (txtName.Text != string.Empty) 
     { 
     if (DXMessageBox.Show("Are You Sure, you Want to Insert?", "Insert Item-Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) 
      { 
      try 
       { 

        Entities dbContext = new Entities(); 
        Name name = new Name(); 
        name.my_name = txtName.Text; 
        dbContext.Names.Add(name); 
        dbContext.SaveChanges(); 
        txtName.Text = null; 
        Refresh(); 
        } 

      catch (Exception err) 
       { 
       DXMessageBox.Show(err.StackTrace); 

       } 
      } 
     else 
      txtName.Text = null; 
     } 

    } 

如果我的問題不明確您或您想了解更多信息請諮詢我。謝謝:)

+2

如果您使用mvvm,您的XAML在哪裏? – Default

+0

我沒有使用MVVM。我想把這個簡單的代碼放到MVVM中。請仔細閱讀我的查詢。謝謝@Default –

+0

@Default'我想將這段代碼轉換成MVVM。任何人都可以引導我轉換到MVVM。「# – Viv

回答

1

首先創建ViewModel類並創建處理每種情況(添加,編輯等)的方法。然後在你的事件處理程序中調用相關的ViewModel函數。看看你的代碼,你將不得不通過一些狀態信息,如Panel3.Visibility

一旦你完成了這一步,你就可以開始通過將相關屬性綁定到你的虛擬機而不是將它傳遞通過,將你的設計移到一個完全MVVM模式。

最簡單的方法似乎是插入。要使其完全MVVM,您必須將您的txtName綁定到屬性。我會從那裏開始。

+0

I'被教導不要使用事件處理程序,而是'Command'屬性和'ICommand'接口。這確保了視圖中邏輯的最小化。 – Default

+1

這是對的,但它更復雜的處理馬上。正如我所說,我會從我建議的事情開始,然後取得進展。首先將代碼移動到虛擬機,然後最終創建命令應該很容易 –

3

我認爲沒有人會花時間爲您編寫代碼。你必須通過學習一些教程來學習它。或許你可以遵循一些非常好的教程,讓您深入瞭解開發基於MVVM的應用程序(不只是着眼於效率)

按照此線程:

MVVM: Tutorial from start to finish?

它包含了所有你需要的。