有没有流利的WPF项目?

时间:2009-08-28 14:05:10

标签: wpf silverlight interface fluent

作为我正在尝试与WPF / XAML达成协议的一部分,我对将流畅的界面应用于UI编码感兴趣。

我知道Fluent Silverlight(http://code.google.com/p/fluent-silverlight/),但我似乎找不到任何与WPF相同的内容。

就像个人说明一样,我发现很难购买XAML和C#/ MVVM的组合。在我看来,UI编程的某些方面(例如数据绑定)在代码中比在声明性XAML中更好地表达。

流畅的WPF界面似乎只是实现这些目标的事情。

3 个答案:

答案 0 :(得分:2)

在最近的一个播种代码播客:http://herdingcode.com/?p=212其中一位嘉宾讨论了他们尝试使用流畅的界面来创建WPF UI。他们中的一个可能会做出他们已经完成的工作。

顺便提一下,同样的播客和之前的播客(http://herdingcode.com/?p=208)会先谈谈您对代码优先与视图的关注以及为什么专注于xaml是有利的。

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

在你的疑虑中,你并不孤单。希望这些播客能帮助您做出决定。

答案 1 :(得分: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));
        }
    }
}

流利版采用两种扩展方法AddChildren(而不是Children.Add)和AddClick(而不是Click += ...)。

程序如下:

enter image description here

我保留了我的个人FluentWpf library on github

答案 2 :(得分:1)

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