【发布时间】:2010-12-03 08:40:17
【问题描述】:
我们使用 Infragistics 控件基于 Composite Application Library 和 MVVM 构建了一个大型应用程序。
为了节省时间并使应用程序更直接,我们取消了 MVVM 要求。我们现在没有 Presenters 或 ViewModel,我们的 View 已经变成了简单的 UserControl,它们的创建方式如下:
BaseEditor.cs:
using System.Windows.Controls;
namespace App
{
public class BaseEditor : UserControl
{
public string Title { get; set; }
public BaseEditor()
{
Title = "This was defined in the Base Editor.";
Loaded += new System.Windows.RoutedEventHandler(BaseEditor_Loaded);
}
void BaseEditor_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
StackPanel sp = new StackPanel();
TextBlock tb = new TextBlock();
tb.Text = Title;
sp.Children.Add(tb);
this.Content = sp;
}
}
}
CustomerEditor.cs:
namespace App
{
public class CustomerEditor : BaseEditor
{
public CustomerEditor()
{
Title = "This was overwritten by the CustomerEditor.";
}
}
}
Window1.cs.xaml:
<Window x:Class="App.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:App"
Title="Window1" Height="300" Width="300">
<Grid>
<local:CustomerEditor/>
</Grid>
</Window>
除了可测试性问题以及像这样执行 WPF“感觉很脏”这一事实之外,我仅从这个决定中体验到了积极的影响,例如:
- 我们可以互相继承我们的非 XAML 用户控件
- 我们使用尽可能多的代码隐藏来加快开发速度
- 将基础架构控件直接附加到来自 Web 服务的模型类可以解决我们在将基础架构绑定到 ObservableCollections 时遇到的几十个小绑定问题
- 即使在直接的 WPF 中,缺少 ObservableCollections 也会使 not being able to create a simple Menu 等问题消失
- 我们正在使用 UserControls 和代码背后的直接事件逐一替换 EventAggregator,从而解决了各种事件问题
在 WPF 中做 MVVM 的其他人有类似的经历吗?从长远来看,您是否遇到过任何实际问题?
【问题讨论】:
-
那么,你把你的业务逻辑放在哪里了?
-
我们从 ESB 上的服务获取数据,大部分业务逻辑都在那里完成
标签: .net wpf architecture mvvm prism