前言
这个框架写到这里, 应该有很多同学发现, 框架很多地方的细节, 其实是违背了 MVVM 的设计逻辑的.
没错, 它的确是违背了.
但为什么明知道违背设计逻辑, 还要这样编写框架呢?
那是因为, 我们编写的是框架, 是使用 MVVM 的概念编写框架, 而并不是要完美的实现 MVVM 设计.
两者有什么区别呢? 区别就是前者是实战, 后者只是个理念.
在实战架构中, 并不是 UI 的东西都一定要放在 UI 层写, 逻辑的东西放在逻辑层写的. 因为, 架构的目的是让程序员更好的写代码, 而不是让代码死死的固定在某一层.
所以, 我们在编写框架时, 设计模式中该切割的东西, 就不要犹豫的切割. 因为, 架构师是设计模式的使用者, 而不是被使用者.
举个例子, 当你的逻辑全部提取到某一层中以后, 你突然发现, 该逻辑执行过程中要弹出提示框, 但提示框又是属于 UI 层的, 此时你犹豫了, 把提示框移动到逻辑层, 不符合设计理念, 但不在逻辑层做, 开发又很难受.
遇到这样的情况, 我们该怎么做呢?
很简单, 让设计理念去死吧, 不要犹豫, 直接把弹出提示框封装到逻辑层中即可.
现实中, 设计逻辑永远是要向开发逻辑低头的, 因为实战永远高于理论.
框架是什么?
框架就是规则, 规则在人类社会被称之为法律; 换言之, 框架是代码界的法律.
人类社会建立法律之初, 是抱着人人守法, 秩序稳定的理想的.
可现实是残酷的, 总有人, 因为各种原因, 践踏法律.
事实上, 代码界也一样, 总是会那不守规矩的程序员触犯法律, 他们会让代码跨边界引用类库或者拒绝使用接口声明对象等等.
为什么不能准守规则呢?
因为他们想更快速的完成任务, 所以他们不惜触犯法律, 也要拼一次一夜暴富...
所以, 架构师作为代码界的人民警察, 一定要做好惩治工作...
因为, 当一个坏代码出现后, 马上就会有若干个类似的坏代码出现, 犹如劣币逐良币一样, 时间一长, 框架就会被破坏.
接着好代码就得依赖着坏代码写.
当坏代码多了到一定程度, 好代码就会变成 Bug 了...
所以, 任重道远, 人民警察还需警惕...
为什么要编写数据控件
我们之前编写的数据控件功能相对单一; 完全可以用属性和事件代替, 所以有些同学会觉得, 数据控件好像没什么用.
其实不然, 现实中我们要处理的逻辑, 并不是简单的对象属性一对一绑定就能处理解决的.
我们需要做很多操作, 其中也包括 UI 操作. 而数据控件就是用来应对这种复杂的 UI 操作的.
因为数据控件通过绑定 UI 控件后, 已经将复杂的 UI 操作, 变成了简单的数据逻辑操作了.
如果没有数据控件, 那当我们实现一个控件联动时, 就得在 Xaml.cs 文件中处理了.
如果该控件联动还要触发数据变化, 那我们就又得从 Xaml.cs 文件中, 穿越回 ViewModel 中处理逻辑了; 亦或者, 我们直接在 Xaml.cs 文件中处理数据逻辑.
不论哪种模式, 都会将我们好容易做的逻辑层与 UI 层混淆到一起. 而这个问题, 并不是一个弹出框那么简单的 UI 越界问题, 因为它包含了更多复杂的业务逻辑.
数据控件解决这个烦恼.
我们通过数据控件, 实现了控件是控件, 数据是数据, 清晰的, 层次分离; 并且通过简洁的绑定, 实现了数据变化与控件变化同步.
DataGrid 数据控件
DataGrid 数据控件可以说是数据控件的精髓了, 因为 DataGrid 相对复杂, 不像其他的数据控件那样功能单一.
所以, 当然我们学习了 DataGrid 数据控件后, 就可以更好的理解, 数据控件的意义了.
下面我们先看下 DataGrid 数据控件的代码:
- public class DataGrid<T> : Control<T>
- {
- private Action<T> LoadAction = null;
- public Action<T> SelectCallBack = null;
- private Func<object, bool> DataFilter = null;
- #region 分页
- private volatile int _CurrentPage = 1;
- public int CurrentPage
- {
- get { return _CurrentPage; }
- set
- {
- _CurrentPage = value;
- if (_CurrentPage> PageCount)
- {
- _CurrentPage = PageCount;
- }
- if (_CurrentPage <1)
- {
- _CurrentPage = 1;
- }
- OnPropertyChanged();
- }
- }
- private int _PageCount = 1;
- public int PageCount { get { return _PageCount; } set { _PageCount = value; OnPropertyChanged(); } }
- private int _RecordCount = 0;
- public int RecordCount
- {
- get { return _RecordCount; }
- set
- {
- _RecordCount = value;
- if (_RecordCount <= SkipNumber)
- {
- PageCount = 1;
- }
- else
- {
- PageCount = int.Parse(Math.Ceiling((double)RecordCount / (double)SkipNumber).ToString());
- }
- if (_CurrentPage> PageCount)
- {
- _CurrentPage = PageCount;
- }
- OnPropertyChanged();
- }
- }
- private int _SkipNumber = 30;
- public int SkipNumber { get { return _SkipNumber; } set { _SkipNumber = value; OnPropertyChanged(); } }
- private TextBox<string> _JumpTextBox = new TextBox<string>();
- public TextBox<string> JumpTextBox
- {
- get { return _JumpTextBox; }
- set { _JumpTextBox = value; OnPropertyChanged(); }
- }
- #region 跳页
- public BaseCommand JumpCommand
- {
- get
- {
- return new BaseCommand(JumpCommand_Executed);
- }
- }
- void JumpCommand_Executed(object send)
- {
- int pagenum = 0;
- if (int.TryParse(JumpTextBox.Text, out pagenum))
- {
- if (pagenum <= PageCount && pagenum> 0)
- {
- CurrentPage = pagenum;
- if (LoadAction != null)
- {
- LoadAction(Condition);
- }
- }
- else
- {
- MessageBox.Show("请正确填写跳转页数.", "提示信息");
- }
- }
- else
- {
- MessageBox.Show("请正确填写跳转页数.", "提示信息");
- }
- }
- #endregion
- #region 上一页
- public BaseCommand PreviousCommand
- {
- get
- {
- return new BaseCommand(PreviousCommand_Executed);
- }
- }
- void PreviousCommand_Executed(object send)
- {
- if (CurrentPage> 1)
- {
- CurrentPage -= 1;
- if (LoadAction != null)
- {
- LoadAction(Condition);
- }
- }
- else
- {
- MessageBox.Show("已至首页.", "提示信息");
- }
- }
- #endregion
- #region 下一页
- public BaseCommand NextCommand
- {
- get
- {
- return new BaseCommand(NextCommand_Executed);
- }
- }
- void NextCommand_Executed(object send)
- {
- if (CurrentPage <PageCount)
- {
- CurrentPage += 1;
- if (LoadAction != null)
- {
- LoadAction(Condition);
- }
- }
- else
- {
- MessageBox.Show("已至末页.", "提示信息");
- }
- }
- #endregion
- #endregion
- private ObservableCollection<T> _ItemsSource = new ObservableCollection<T>();
- public ObservableCollection<T> ItemsSource
- {
- get { return _ItemsSource; }
- set
- {
- _ItemsSource = value;
- if (_ItemsSource != null && _ItemsSource.Count> 0 && SelectedItem == null)
- {
- SelectedItem = _ItemsSource.First();
- }
- OnPropertyChanged();
- }
- }
- public void SetItemsSource(List<T> itemSource)
- {
- ItemsSource = new ObservableCollection<T>(itemSource);
- }
- public T _SelectedItem;
- public T SelectedItem
- {
- get { return _SelectedItem; }
- set
- {
- _SelectedItem = value;
- if (SelectCallBack != null)
- {
- SelectCallBack(_SelectedItem);
- }
- OnPropertyChanged();
- }
- }
- private ICollectionView _ItemsSourceView;
- public ICollectionView ItemsSourceView
- {
- get
- {
- _ItemsSourceView = CollectionViewSource.GetDefaultView(_ItemsSource);
- return _ItemsSourceView;
- }
- set
- {
- _ItemsSourceView = value;
- OnPropertyChanged();
- }
- }
- private T _Condition = (T)Activator.CreateInstance(typeof(T));
- public T Condition { get { return _Condition; } set { _Condition = value; OnPropertyChanged(); } }
- #region 方法
- public DataGrid()
- {
- }
- public void BindSource(Action<T> loadAction, T conditionRow = default(T))
- {
- LoadAction = loadAction;
- if (LoadAction != null)
- {
- CurrentPage = 1;
- LoadAction(conditionRow);
- }
- }
- public void BindSource(Action loadAction)
- {
- LoadAction = new Action<T>((obj) => {
- loadAction();
- }); ;
- if (LoadAction != null)
- {
- CurrentPage = 1;
- LoadAction(default(T));
- }
- }
- public void ItemsSourceReBind()
- {
- BindSource(LoadAction);
- }
- public void SelectedItemReBind()
- {
- T newitem = (T)Activator.CreateInstance(typeof(T));
- List<System.Reflection.PropertyInfo> plist = typeof(T).GetProperties().ToList();
- foreach (var propertyInfo in plist)
- {
- propertyInfo.SetValue(newitem, propertyInfo.GetValue(SelectedItem));
- }
- SelectedItem = newitem;
- }
- public void SetFilter(Func<object, bool> dataFilter)
- {
- try
- {
- DataFilter = dataFilter;
- _ItemsSourceView = CollectionViewSource.GetDefaultView(_ItemsSource);
- _ItemsSourceView.Filter = new Predicate<object>(DataFilter);
- }
- catch(Exception ex)
- {
- }
- }
- public void Refresh()
- {
- if (_ItemsSourceView == null)
- {
- _ItemsSourceView = CollectionViewSource.GetDefaultView(this.ItemsSource);
- }
- _ItemsSourceView.Refresh();
- }
- #endregion
- }
从代码中我们可以看到, DataGrid 控件不仅包含了基础属性, 还包含了上一页, 下一页, 刷新, 甚至过滤的功能.
下面, 我们看下一下 DataGrid 控件的基础应用.
Xaml 页面代码如下:
- <DataGrid Margin="5" FontSize="12" ItemsSource="{Binding TestDataGrid.ItemsSource}" AutoGenerateColumns="True"
- SelectedItem="{Binding TestDataGrid.SelectedItem}"> </DataGrid>
ViewModel 页面代码如下:
- public DataGrid<User> TestDataGrid { get; set; }
- TestDataProxy proxy = new TestDataProxy();
- public VM_PageDataGrid()
- {
- TestDataGrid = new DataGrid<User>();
- int currentPage = TestDataGrid.CurrentPage;
- int skipNumber = TestDataGrid.SkipNumber;
- proxy.GeDataGridData(null, currentPage, skipNumber, (list, count, msg) =>
- {
- TestDataGrid.SetItemsSource(list);
- TestDataGrid.RecordCount = count;
- });
- TestDataGrid.SelectCallBack = (user) =>
- {
- MessageBox(user.Name);
- };
- }
我们可以看到, 基础的 DataGrid 应用很简单, 只要设置好绑定, 然后将读取的数据赋值给数据控件的 ItemSource 属性即可.(这里我们使用 SetItemSource 方法为 ItemSource 赋值)
然后我们会发现, 只要我们操作数据控件的 ItemSource, 不论是增加数据, 删除数据, 变更数据, 页面都会自动的同步刷新.
DataGrid 的中级应用
我们在上面的代码中可以看到, DataGrid 数据控件还包含了分页功能. 那么如何实现分页功能呢.
很简单, 我们只需要在 Xaml 页面多绑定几个属性即可实现.
Xaml 代码如下:
- <StackPanel DataContext="{Binding TestDataGrid}" Orientation="Horizontal" DockPanel.Dock="Bottom" HorizontalAlignment="Right" Margin="0,10,0,0">
- <Button Content="上一页" Width="60" Command="{Binding PreviousCommand}" Height="20" Margin="20,0,0,0" VerticalAlignment="Top" />
- <Button Content="下一页" Width="60" Command="{Binding NextCommand}" Height="20" Margin="20,0,0,0" VerticalAlignment="Top" />
- <TextBlock VerticalAlignment="Center" Text="每页" Margin="20,0,0,0"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="{Binding SkipNumber}" Margin="0,0,0,0"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="条"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="{Binding CurrentPage}" Margin="20,0,0,0"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="/"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="{Binding PageCount}"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="总记录数:" Margin="20,0,0,0"></TextBlock>
- <TextBlock VerticalAlignment="Center" Text="{Binding RecordCount}"></TextBlock>
- <TextBox VerticalAlignment="Center" Width="40" Height="20" Margin="40,0,0,0" Text="{Binding JumpTextBox.Text}"></TextBox>
- <Button Content="GO" Command="{Binding JumpCommand}" Width="40" Height="20" Margin="5,0,0,0" VerticalAlignment="Top" />
- </StackPanel>
- <GroupBox DockPanel.Dock="Top" Header="DataGrid" Margin="10,0,0,0">
- <DataGrid Margin="5" FontSize="12" ItemsSource="{Binding TestDataGrid.ItemsSource}" AutoGenerateColumns="True"
- SelectedItem="{Binding TestDataGrid.SelectedItem}">
- </DataGrid>
- </GroupBox>
这样我们就实现了分页功能, 代码很简单, 并且彻底分割了 UI 和 ViewModel.
但是那么复杂的 UI, 就这样简单的被彻底搞定了吗?
当然是不可能的! UI 很复杂, 仅仅靠数据控件是无法彻底搞定的.
那么我们应该怎么办呢?
很简单, 我们去编写 UI 控件就好啦.
当然, 我们要编写的 UI 控件不是普通的 UI 控件, 而是配合数据控件应用的 UI 控件.
这种定制 UI 控件在功能上与其他自定义控件是一样, 但好处就在于, 编写方便, 易于理解和二次开发.
来源: https://www.cnblogs.com/kiba/p/9753893.html