我想清除一些有关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