当我遇到希望以流利风格编程的 WPF 部分时,我将支持的扩展方法添加到个人实用程序程序集中。
例如,这是一个演示TaskbarItemInfo
ProgressValue
和ProgressState
属性的程序。这个版本是用标准的非流利方式编写的。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shell;
namespace TaskbarItemProgress
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TaskbarItemInfo = new TaskbarItemInfo();
TaskbarItemInfo.ProgressValue = 0.5;
var stackPanel = new StackPanel();
Content = stackPanel;
var normalButton = new Button() { Content = "Normal" };
normalButton.Click += (s, e) =>
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal;
stackPanel.Children.Add(normalButton);
var pausedButton = new Button() { Content = "Paused" };
pausedButton.Click += (s, e) =>
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Paused;
stackPanel.Children.Add(pausedButton);
var errorButton = new Button() { Content = "Error" };
errorButton.Click += (s, e) =>
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Error;
stackPanel.Children.Add(errorButton);
var indeterminateButton = new Button() { Content = "Indeterminate" };
indeterminateButton.Click += (s, e) =>
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
stackPanel.Children.Add(indeterminateButton);
var noneButton = new Button() { Content = "None" };
noneButton.Click += (s, e) =>
TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None;
stackPanel.Children.Add(noneButton);
var increaseButton = new Button() { Content = "Increase" };
increaseButton.Click += (s, e) => TaskbarItemInfo.ProgressValue += 0.10;
stackPanel.Children.Add(increaseButton);
var decreaseButton = new Button() { Content = "Decrease" };
decreaseButton.Click += (s, e) => TaskbarItemInfo.ProgressValue -= 0.10;
stackPanel.Children.Add(decreaseButton);
}
}
}
这是流畅的版本:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shell;
using FluentWpf;
namespace TaskbarItemProgress
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
TaskbarItemInfo = new TaskbarItemInfo();
TaskbarItemInfo.ProgressValue = 0.5;
Content = new StackPanel()
.AddChildren(
new Button() { Content = "Normal" }
.AddClick((s, e) => TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Normal),
new Button() { Content = "Paused" }
.AddClick((s, e) => TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Paused),
new Button() { Content = "Error" }
.AddClick((s, e) => TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Error),
new Button() { Content = "Indeterminate" }
.AddClick((s, e) => TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate),
new Button() { Content = "None" }
.AddClick((s, e) => TaskbarItemInfo.ProgressState = TaskbarItemProgressState.None),
new Button() { Content = "Increase" } .AddClick((s, e) => TaskbarItemInfo.ProgressValue += 0.10),
new Button() { Content = "Decrease" } .AddClick((s, e) => TaskbarItemInfo.ProgressValue -= 0.10));
}
}
}
fluent 版本使用了两种扩展方法,AddChildren
(instead of Children.Add
) 和AddClick
(instead of Click += ...
)。
该程序如下所示:

我将我的个人FluentWpf
图书馆保存在 github 上。