我想清除一些有關MVVM違規的問題。因爲我已經創建了一些解決方案,用一些項目來演示這些案例。 下面是該解決方案的定義(項目):MVVM違規
- 視圖(它的一個WPF類Libraray,顯然它的意見)
- 視圖模型(它的一類Libraray,顯然它具有的ViewModels)
- 模型(它的一類Libraray,顯然它的型號)
- 域(它的一類Libraray它具有應用dataModels)
- 核心(其A類Libraray,它有WPF像RelayCommnd或EventToCommand核心)
- 應用程序(它的一個WPF應用程序和啓動項目)
- ExternalCustomControl(它的一個WPF以假想的第三方公司創建的自定義控制庫)
我給你從下載的整體解決方案,以更好地理解 Here
第一期: 我的在EventToCommand MainWindow.xaml爲關閉活動窗口和連接它MainWindowClosingCommand與PassEventArgsToCommand設置爲true,那麼,在MainViewModel有一個名爲OnMainWindowClosing
private void OnMainWindowClosing(object parameter)
{
var arg = parameter as CancelEventArgs;
// What is the best way to show message dialog to user?
// Do i have to send message to the View to show the messageBox dialog and then the window send me the answer back to continue?
// What about IMessageBoxService? Doesn't it violates MVVM?
// Doesn't following code violates the MVVM?
// Cancel the Closing of a Window isnt a UI side duty?
arg.Cancel = true;
}
,完全每當命令處理程序你想設置e.Handled或e.Cancel你面臨這個問題。所以你知道任何其他方式,不需要投入參數 as CancelEventArgs?
第二期: 我已經在主窗口的EventToCommand。XAML爲PreviewMouseDown事件的電網和連接它MouseClickCommand與PassEventArgsToCommand設置爲true,那麼,在MainViewModel有名爲命令處理程序OnMouseClick:
private void OnMouseClick(object parameter)
{
// var arg = parameter as MouseButtonEventArgs;
// This is the violation of MVVM : To cast the parameter to MouseButtonEventArgs i have to add a refrence
// to PresentationCore.dll in the ViewModel Project
// The next and worse step is that in most cases we need to know the Original Source of the event
// (maybe its a StackPanel or a Label or etc) and this again vioaltes the MVVM
// So Whats the WorkAround?
}
第三期: 我使用了ThirdParty控件(Imagine Infragistics或DevExpress或任何其他第三方控件,但這裏作爲一個例子,我創建了虛構的c ONTROL在我主窗口我作爲ExternalCustomControl項目解決方案),就像這樣:
<thirdParty:ThirdPartyCustomControl Grid.Row="1"
ItemsSource="{Binding MyItemsSource,Converter={StaticResource converterKey}}" />
和ThirdPartyCustomControl具有IEnumarabe<CustomControlDataModel>
類型的屬性(CustomControlDataModel是存在於ExternalCustomControl組裝型),但你也知道如果你想創建一個屬性MainViewModel控制與類型CustomControlDataModel你必須添加一個參考ExternalCustomControl.dll在ViewModel項目和這違反了MVVM,所以我創建了一個ty PE命名MyDataModel和約束所述控制的的ItemsSource到MyItemsSource場所在MainViewModel:
// If i define MyItemsSource as List<CustomControlDataModel> i have to add a refrence to ExternalCustomControl.dll
// and i think its again violate the MVVM (because ExternalCustomControl.dll is a UI Side Controls Assembly)
public List<MyDataModel> MyItemsSource { get; set; }
所以我結合類型CustomControlDataModel的屬性類型的屬性MyDataModel當然我需要一個轉換器:
public object Convert(object value, Type targetType, object parameter, c System.Globalization.CultureInfo culture)
{
// Imagine when the source data (MyDataModel) is huge (for example 1 milion) it (this dummy Conversion)
// affects the performance
if (value is List<MyDataModel>)
{
var result = new List<CustomControlDataModel>();
(value as List<MyDataModel>).ForEach(myVal =>
{
var custDataModel = new CustomControlDataModel();
custDataModel.ID = myVal.ID;
custDataModel.Name = myVal.Name;
custDataModel.Age = myVal.Age;
result.Add(custDataModel);
});
return result;
}
return value;
}
和問題是你知道任何更好的w比這個虛擬轉換,或者你通常將你的第三方組件添加到你的視圖和viewmodel?
這些都是我遇到的問題,如果您知道其他問題並將您的專業知識分享給每個人,我將不勝感激。
UPADTE:
對於的MessageBox部分第一個問題,我建議這個鏈接 MesageBox
感謝。
[UserControls不應該有視圖模型。](http:// stackoverflow。com/a/25796096/1228)這是*代碼氣味*。你可以告訴,因爲你在做這件事時遇到問題。您也將UI關注點放到視圖模型中。用戶界面關注點在*代碼隱藏*中。 MVVM!=沒有隱藏代碼。 – Will 2014-10-09 13:09:34
@這個問題不是這種情況 – HaMEd 2014-10-12 20:02:04