gpt4 book ai didi

wpf - WPF 对此的答案是什么?

转载 作者:行者123 更新时间:2023-12-03 03:30:10 27 4
gpt4 key购买 nike

我使用 WPF 开发了两个中等大小的应用程序。 WPF 的简洁性及其功能给我留下了深刻的印象。当我向我的一位同事(碰巧开发业务应用程序)解释 WPF 的各种好处时,他向我提出了这个问题,这让我完全难住了:

问题:

他用以下方式在大约 2 分钟内编写了一个应用程序:

  1. 打开一个新的 WinForms 项目。
  2. 定义一个类Loan
  3. 构建项目。
  4. 使用 Loan 定义对象数据源。
  5. 在数据源资源管理器中,将Loan 数据源的 View 类型更改为“详细信息”。
  6. 将数据源拖到设计器中的表单上。
  7. 向数据源提供包含一个对象的 Loan[]
  8. 构建并运行应用程序。

代码:

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; } }
}
}

设计师:

enter image description here

应用:

enter image description here

现在,只要您在窗口中更改AmountRate 的值,Total 的值就会相应更改。在解释这是业务应用程序中的一项非常有用的功能之后,您对实体中的一个属性所做的任何更改都会立即更新 View ,其中计算的属性会立即刷新,从而使用户体验更好。考虑到典型的业务实体类有很多属性,这节省了大量的编码。然后他要求我在 WPF 中做同样的事情。

我首先向他解释说我不明白这里发生了什么黑魔法。 Total 文本框如何自动更新?这是我的第一个问题:

Q1。 Loan 类没有实现 INotifyPropertyChanged 或类似的东西。那么,当 AmountRate 文本框失去焦点时,Total 文本框如何更新呢?

然后我告诉他我不知道如何在 WPF 中如此轻松地完成同样的事情。但是,我在 WPF 中编写了相同的应用程序,并在 UI 中使用了 3 个 TextBlock 和 3 个 TextBox。我还需要使 Loan 类实现 INotifyPropertyChanged。为 AmountRate 添加了支持字段。每当设置这些属性时,我都会针对属性 Total 发出属性更改通知。最后,我留下了一个控件对齐不良的应用程序,它的功能与 WinForms 应用程序相同。然而,这比 WinForms 方法更难做到。

我回到家,然后想到了一个好主意,将 Loan 数据源拖放到 WPF 窗口上(在我将 View 模式更改为详细信息之后)。果然,我得到了与 WinForms 应用程序中相同类型的 UI,并将数据源设置为与 WinForms 应用程序中相同的 Loan[] 后,它似乎就完成了。我运行了该应用程序,更改了 AmountRate 字段,希望看到 Total 自动更改。然而,我很失望。 Total 字段没有改变:

enter image description here

代码:

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() };
}
}
}

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>

第二季度。以前我对 WinForms 的黑魔法感到困惑,现在我很困惑,因为同样的黑魔法在 WPF 中不起作用。为什么?

第三季度。如何使 WPF 版本像 WinForms 示例一样自动更新 Total 字段?

第四季度。对于此类业务应用程序开发,哪个平台更好/更快?如果我要代表 WPF 提出更好的论点,我应该关注什么?

我希望我清楚这个问题。如果需要任何说明,请告诉我。谢谢。

最佳答案

问题 1:如果您查看 Windows 窗体的设计器文件,您将看到为 3 个文本框生成的大约 300 行代码。其中一些代码类似于:

this.amountTextBox.DataBindings.Add(
new System.Windows.Forms.Binding("Text",
this.loanBindingSource, "Amount", true));

Binding 和 BindingSource 协作更新绑定(bind)值,并导致每次其中一个值发生更改时更新所有绑定(bind)控件(使用反射)。

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);
}
}
}

第三季度:1) 当金额或费率更改时,会引发该属性以及计算属性“总计”的属性更改通知。2) 将 Amount 和 Rate 的绑定(bind)修改为 Binding="{Binding Amount, UpdateSourceTrigger=LostFocus}"

Q4:WPF 毫无疑问(恕我直言)。 WPF方式更易于测试、维护和理解。

关于wpf - WPF 对此的答案是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14858573/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com