2

作为我不断尝试与 WPF/XAML 达成协议的一部分,我对在 UI 编码中应用流畅的接口产生了兴趣。

我知道 Fluent Silverlight ( http://code.google.com/p/fluent-silverlight/ ),但我似乎找不到任何与 WPF 等效的东西。

就个人而言,我发现很难接受将 XAML 和 C#/MVVM 结合起来做所有事情。在我看来,UI 编程的某些方面(例如数据绑定)在代码中比在声明性 XAML 中更好地表达。

一个流畅的 WPF 界面似乎正是实现这些目标的东西。

4

3 回答 3

2

在最近的 Herding Code 播客中:http ://herdingcode.com/?p=212一位客人讨论他们尝试使用流畅的界面来创建 WPF UI。他们中的一个有可能使他们所做的事情变得可用。

顺便说一句,这个播客和之前的播客 ( http://herdingcode.com/?p=208 ) 谈到了您对代码优先与视图优先的担忧,以及为什么专注于 xaml 是有利的。

除了代码的可测试性之外,设计人员的论点主要是关于使 UI “可混合”(能够在 Microsoft Expression Blend 中设计它们)。如果你不是很小心,基于代码的方法会削弱这种能力。

你并不孤单。希望这些播客能帮助您做出决定。

于 2009-08-28T17:43:31.943 回答
2

当我遇到希望以流利风格编程的 WPF 部分时,我将支持的扩展方法添加到个人实用程序程序集中。

例如,这是一个演示TaskbarItemInfo ProgressValueProgressState属性的程序。这个版本是用标准的非流利方式编写的。

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 上

于 2012-04-19T00:05:25.993 回答
1

用于在 WPF http://code.google.com/p/present/中构建命令的流利 API

于 2010-09-07T10:16:33.033 回答