- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 WPF 开发了两个中等大小的应用程序。 WPF 的简洁性及其功能给我留下了深刻的印象。当我向我的一位同事(碰巧开发业务应用程序)解释 WPF 的各种好处时,他向我提出了这个问题,这让我完全难住了:
问题:
他用以下方式在大约 2 分钟内编写了一个应用程序:
Loan
。Loan
定义对象数据源。Loan
数据源的 View 类型更改为“详细信息”。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; } }
}
}
设计师:
应用:
现在,只要您在窗口中更改Amount
或Rate
的值,Total
的值就会相应更改。在解释这是业务应用程序中的一项非常有用的功能之后,您对实体中的一个属性所做的任何更改都会立即更新 View ,其中计算的属性会立即刷新,从而使用户体验更好。考虑到典型的业务实体类有很多属性,这节省了大量的编码。然后他要求我在 WPF 中做同样的事情。
我首先向他解释说我不明白这里发生了什么黑魔法。 Total
文本框如何自动更新?这是我的第一个问题:
Q1。 Loan
类没有实现 INotifyPropertyChanged
或类似的东西。那么,当 Amount
或 Rate
文本框失去焦点时,Total
文本框如何更新呢?
然后我告诉他我不知道如何在 WPF 中如此轻松地完成同样的事情。但是,我在 WPF 中编写了相同的应用程序,并在 UI 中使用了 3 个 TextBlock
和 3 个 TextBox
。我还需要使 Loan
类实现 INotifyPropertyChanged
。为 Amount
和 Rate
添加了支持字段。每当设置这些属性时,我都会针对属性 Total
发出属性更改通知。最后,我留下了一个控件对齐不良的应用程序,它的功能与 WinForms 应用程序相同。然而,这比 WinForms 方法更难做到。
我回到家,然后想到了一个好主意,将 Loan
数据源拖放到 WPF 窗口上(在我将 View 模式更改为详细信息之后)。果然,我得到了与 WinForms 应用程序中相同类型的 UI,并将数据源设置为与 WinForms 应用程序中相同的 Loan[]
后,它似乎就完成了。我运行了该应用程序,更改了 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() };
}
}
}
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/
(function() { main(); function main() { jQuery(document).ready(function($) {
所以我必须为我们的类(class)软件设计制作一个 GUI,我们正在为 children 制作一个游戏来练习乘法表。到目前为止,当您执行一次测试或练习时它工作正常,但是当您进行第二次运行时,它会出错。
我刚开始学习 python,想做一些琐事。基本上,我想从列表中随机询问一个问题,然后使用“输入”运算符来判断用户输入的 Y/N 是否正确。我坚持确定如何检查它是否正确。也许我的(不正确的)代码可以更好
我目前正在做一个暑期实习项目,我必须制作一个不经意的 DNS 翻译服务器。我不会在这里详细讨论被忽视的部分,但我会解释我的程序的架构。 有一个服务器端接收混淆的请求并发回一个它自己无法理解的答案。 在
我想用ajax请求翻译单词到谷歌翻译 如果我使用 curl,它会像: curl_init("http://translate.google.com/translate_a/t?client=t&tex
这是我运行dig www.google.com时的答案部分: ;; ANSWER SECTION: www.google.com. 108 IN A 74
我在ES上有以下简单数据: curl -XPUT localhost:9200/dt/art/1 -d '{ "age": 77 }' curl -XPUT localhost:9200/dt/art
我从编码开始,我有一个多维数组的示例。但它没有给出预期的答案。 我只得到“C”,我期待“JohnnyCash:Live at Folsom Prison”。出了什么问题? var music = []
我们有一个应用程序与 Crashlytic 和 Answers 配合得很好。我们需要为这个应用程序做一个不同的风格。因此,我们的 Gradle 编译工作正常,并为两个不同的品牌制作了两个不同的 APK
我正在尝试从数据库获取歌曲列表。 我在查询行中发送一个 ID 数组(永久链接),并且我希望返回值的顺序与我在数组中给出的顺序相同。有没有办法做到这一点? function getByPermalink
我有一个表单可以输入这样的值 test 有没有办法用jquery改变输入类型 我基本上想把这个添加到输入类型中 data-slider="true" data-sl
好吧,我距离数学高手还很远。哎呀,我记住了足够多的高中代数,可以拼凑出任何有效的公式,这对我来说是一个胜利。因此,如果您注意到这里有一个不必要的长或令人困惑的公式,那就可以解释了。 但是,正如人们可以
所以我的问题有点令人困惑,但仍然如此。我从外部源获取一个由 8 个字符串组成的数组,其中所有字符串都包含 double 值。这些值通常为小数点后 4 位: 12345.5678 我想做的是将其转换为小
我成功地构建了一个函数来提示用户提出问题,然后是随机排列的答案选项。但是,由于答案选择现在是随机的,python 如何识别用户输入(数字:1、2、3 或 4)以获得“正确”答案? import ran
我正在尝试使用蛮力来回答这个问题,这样我就可以理解发生了什么: https://www.interviewcake.com/question/java/product-of-other-numbers
尝试使用刚刚宣布的 Answers OSX平台框架: pod 'Fabric' pod 'Answers' pod 'Crashlytics' #import #import #import [
在我添加的页面上检索忘记的用户名 步骤 1) 输入电子邮件地址(通过电子邮件获取帐户) 第 2 步)验证安全问题(他们提供答案,我对其进行验证) 第 3 步)向他们发送带有用户名的电子邮件 第 2 步
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭10 年前。 Improve th
在我的测试中,我需要模拟一种情况,当使用实体管理器(em)将新对象保存到数据库中时,在此过程中,该对象的id属性设置为数据库中该行的自动递增ID。我想将该id属性设置为我自己的值,以便稍后在测试中进行
我有这个代码。调用askToContinue() 方法来询问用户是否要继续,但我的问题是它只是忽略选择并重新启动程序,无论我输入什么。我在代码中遗漏了什么导致它忽略我的选择? public class
我是一名优秀的程序员,十分优秀!