我使用WPF开发了两个中等大小的应用程序.WPF的清晰度及其功能给我留下了深刻的印象.当我向我的一位同事(谁正在开发商业应用程序)解释WPF的各种好处时,他向我提出了这个让我完全难过的问题的挑战:
问题:
他在大约2分钟内用以下方式编写了一个应用程序:
Loan
.Loan
.Loan
数据源的视图类型更改为"详细信息".Loan[]
包含一个对象的数据源.代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinForms_DataBinding_Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
loanBindingSource.DataSource = new Loan[] { new Loan() };
}
}
public class Loan
{
public decimal Amount { get; set; }
public decimal Rate { get; set; }
public decimal Total { get { return Amount * Rate; } }
}
}
Run Code Online (Sandbox Code Playgroud)
设计师:
应用程序:
现在,无论何时更改窗口中Amount
或Rate
的值,都会相应地Total
更改值.在解释这是业务应用程序中非常有用的功能之后,您对实体中的一个属性所做的任何更改会立即更新视图,其中计算属性会立即刷新,从而使用户体验更好.考虑到典型的业务实体类具有许多属性,这节省了大量编码.然后他让我在WPF中做同样的事情.
我首先向他解释说我不明白这里有什么黑魔法.Total
文本框如何自动更新?这是我的第一个问题:
Q1.本Loan
类不落实INotifyPropertyChanged
或类似的东西.那么如何Total
当文本框获得更新Amount
或Rate
文本框失去焦点?
然后我告诉他我不知道如何在WPF中轻松做同样的事情.但是,我在UI 中用3 TextBlock
秒和3 TextBox
秒在WPF中编写了相同的应用程序.我还需要进行Loan
类实现INotifyPropertyChanged
.为Amount
和添加了支持字段Rate
.每当设置这些属性时,我都会为属性引发属性更改通知Total
.最后,我留下了一个带有严重对齐控件的应用程序,它与WinForms应用程序完全相同.但是,这比WinForms方法更难做到.
我回到家,然后明确地将Loan
数据源拖放到WPF窗口(我将视图模式更改为详细信息后).果然,我得到了与WinForms应用程序相同的UI,并且在将数据源设置Loan[]
为与WinForms应用程序相同之后,它似乎已经完成了.我跑的应用,改变了Amount
和Rate
领域希望能看到Total
自动地改变自己.但是,我很失望.该Total
领域没有改变:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WinForms_DataBinding_Example;
namespace WPF_Grid_Example
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Window_Loaded_1(object sender, RoutedEventArgs e)
{
System.Windows.Data.CollectionViewSource loanViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("loanViewSource")));
// Load data by setting the CollectionViewSource.Source property:
loanViewSource.Source = new List<Loan>() { new Loan() };
}
}
}
Run Code Online (Sandbox Code Playgroud)
xaml:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:WinForms_DataBinding_Example="clr-namespace:WinForms_DataBinding_Example;assembly=WinForms_DataBinding_Example" mc:Ignorable="d" x:Class="WPF_Grid_Example.MainWindow"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
<Window.Resources>
<CollectionViewSource x:Key="loanViewSource" d:DesignSource="{d:DesignInstance {x:Type WinForms_DataBinding_Example:Loan}, CreateList=True}"/>
</Window.Resources>
<Grid>
<Grid x:Name="grid1" DataContext="{StaticResource loanViewSource}" HorizontalAlignment="Left" Margin="121,123,0,0" VerticalAlignment="Top">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="Amount:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
<TextBox x:Name="amountTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding Amount, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<Label Content="Rate:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/>
<TextBox x:Name="rateTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" Text="{Binding Rate, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
<Label Content="Total:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="2" VerticalAlignment="Center"/>
<TextBox x:Name="totalTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="2" Text="{Binding Total, Mode=OneWay}" VerticalAlignment="Center" Width="120"/>
</Grid>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
Q2.我之前因为WinForms的黑魔法而感到困惑,我现在感到困惑,因为同样的黑魔法在WPF中不起作用.为什么?
Q3.如何在WPF版本中Total
自动更新字段,如WinForms示例中所示?
Q4.对于这种业务应用程序开发,哪个平台更好/更快?如果我代表WPF做出更好的论证,我该怎么看?
我希望我对这个问题很清楚.如果需要任何说明,请告诉我.谢谢.
Q1:如果您查看Windows窗体的设计器文件,您将看到为3个文本框生成的大约300行代码.其中一些代码类似于:
this.amountTextBox.DataBindings.Add(
new System.Windows.Forms.Binding("Text",
this.loanBindingSource, "Amount", true));
Run Code Online (Sandbox Code Playgroud)
Binding和BindingSource合作更新绑定值,并在每次更改其中一个值(使用反射)时更新所有绑定控件.
Q2:因为WPF设计器没有创建.Designer.cs文件和相关的代码.您需要显式实现INotifyPropertyChange,可以通过使用MVVM Light的ViewModelBase来简化,例如
public class Loan : ViewModelBase
{
public decimal Amount
{
get
{
return this.amount;
}
set
{
if (Set(() => Amount, ref this.amount, value))
{
RaisePropertyChanged(() => Total);
}
}
}
Run Code Online (Sandbox Code Playgroud)
Q3:1)当金额或费率变化时,会提高该属性的属性更改通知,但也会计算属性"总计".2)修改金额和费率的绑定Binding="{Binding Amount, UpdateSourceTrigger=LostFocus}"
Q4:WPF毫无疑问(恕我直言).WPF方式更易于测试,可维护且易于理解.
归档时间: |
|
查看次数: |
715 次 |
最近记录: |