没有 XAML 的 WPF

发布于 2024-11-02 08:01:17 字数 735 浏览 1 评论 0原文

从架构上来说,我认为 WPF 非常令人惊奇。总的来说,我非常喜欢底层渲染/动画的内部工作原理。模板和样式设置的灵活性令人印象深刻。

但我讨厌 XAML - 我觉得它使很多事情变得复杂。我已经在大大小小的应用程序中使用过它,并且我发现自己多次试图弄清楚如何在 XAML 中执行某些操作,其基本原理是基本的,但语法却很古怪。不仅如此,我多次想知道解析/绑定的某些部分有多重。 (我知道它已编译,但我不确定运行时仍评估多少)

XAML 只是构建和加载可视化树的一种方法。是否有任何框架可以简化以非 XML、基于代码(但仍然主要是声明性)方式构建可视化树的框架?具体来说,我对能够在保留 MVVM 方法的同时缓解以下任何问题的框架感兴趣:

  1. 强类型绑定。指定 ViewModel 必须符合特定类型。我认为 BaseBinding 在底层使用了反射,并且我对其速度有点怀疑,更不用说损坏的绑定很烦人。

  2. 更快的绑定,非INotifyPropertyChanged绑定。似乎可以创建某种BindableProperty,并且绑定可以直接侦听它,而不是接收所有 ViewModel 属性更改。与字符串参数相比,使用直接回调似乎也更有优势。

  3. 不同的资源管理方法;同样,某种类型的强类型字典可能会非常好。我几乎希望将样式视为 lambda 或捕获强类型方面的东西。

总之,是否有任何基于非 XAML、适合 MVVM 且强类型的框架?

Architecturally, I think WPF is pretty amazing. In general, I'm a big fan of the underlying rendering/animation inner workings. The flexibility of the templating and styling set up is pretty impressive.

But I loathe XAML - I feel like it complicates many things. I've used it on large and small applications and I've found myself many times trying to figure out how to do something in XAML for which the underlying principle is basic but the syntax is quirky. Not only that, but I've wondered many times how heavy certain parts of the parsing/binding are. (I know it's compiled, but I'm not sure how much is still evaluated at runtime)

XAML is just one way of building and loading the visual tree. Are there any frameworks for simplifying building the visual tree in a non-XML, code-based (but still largely declarative) way? Specifically, I'm interested in frameworks that mitigate any of the following issues while retaining an MVVM approach:

  1. Strongly typed binding. Specify that the ViewModel must conform to a specific type. I assume BaseBinding uses reflection under the hood and I'm a bit skeptical as to the speed of that, not to mention broken bindings being annoying.

  2. Faster binding, non-INotifyPropertyChanged binding. It seems like some sort of BindableProperty<T> could be created and the binding could listen directly to that rather than receiving all ViewModel property changes. And the use of a direct callback versus a string argument would also seem to be advantageous.

  3. A different approach to resource management; again, strongly typed dictionaries of some sort could be pretty nice. I'd almost like to see styles as lambdas or something to capture the strongly typed aspect.

In summary, any frameworks that are non-XAML based, fit well with MVVM, and are strongly typed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

惟欲睡 2024-11-09 08:01:17

我支持您使用无 Xaml 的 WPF。我喜欢 WPF 的布局和绑定功能,但我也讨厌 XAML。我希望 WPF 可以用纯 C# 编写,有一些优点:

  • 对象和集合初始化器可以取代 Xaml 实例化。 (遗憾的是xaml更喜欢自上而下而不是按钮向上)。
  • 绑定转换器可以只是 lambda。
  • 样式可以只是实例化后修改对象的 lambda,没有臃肿的 语法。
  • DataTemplates 只是在给定对象的情况下创建控件的 lambda。
  • DataTemplateSelectors 只是调用其他 DataTemplate 的 DataTemplate lambda。
  • ItemsControl 只是一个 Foreach,它接受 lambda (DataTemplate),并在将新项目添加到基础集合时再次调用它。
  • x:Names 只是变量名称。
  • 不需要很多 MarkupExtensions
    • x:静态
    • x:Type(特别是复杂的泛型!)
  • UserControls 只是函数。

我认为 WPF 的设计添加了太多的复杂性。从过去的 FrontPage 到 Razor,Web 开发已经输掉了这场战斗。

I support you in Xaml-free WPF. I love layout and binding capabilities of WPF but I hate XAML too. I would love that WPF could be written in plain C#, some advantages:

  • Object and Collection initializers could replace Xaml instantiations. (it's a pity that xaml prefers top-down than button up).
  • Binding converters could be just lambdas.
  • Styles could be just lambdas that modify an object after instantiation, no bloated <Setter> syntax.
  • DataTemplates would be just lambdas that create controls given an object
  • DataTemplateSelectors would be just a DataTemplate lambda that calls other DataTemplates.
  • ItemsControl Would be just a Foreach that takes a lambda (DataTemplate) and calls it again if a new item is added to the underlying collection.
  • x:Names would be just variable names.
  • No need for many MarkupExtensions
    • x:Static
    • x:Type (specially with complex generics too!)
  • UserControls would be just functions.

I think way too much complexity was added to WPF to allow it to be designed. Web development has already lost this battle from the old days of FrontPage to Razor.

勿忘心安 2024-11-09 08:01:17

简化?不。但是您可以在 XAML 中执行的所有操作都可以在代码中执行。例如,这是一个简单的 Windows Ink 绘图应用程序 - 非常简单:

谁需要 Visual Studio?紫色背景上的绿色墨水

无需保存,无需更改任何内容 - 但您可以通过代码。

Sketchpad.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Ink;

public class Sketchpad : Application {

    [STAThread]
    public static void Main(){
        var app = new Sketchpad();
        Window root = new Window();
        InkCanvas inkCanvas1 = new InkCanvas();

        root.Title = "Skortchpard";

        root.ResizeMode = ResizeMode.CanResizeWithGrip;
        inkCanvas1.Background = Brushes.DarkSlateBlue;
        inkCanvas1.DefaultDrawingAttributes.Color = Colors.SpringGreen;
        inkCanvas1.DefaultDrawingAttributes.Height = 10;
        inkCanvas1.DefaultDrawingAttributes.Width = 10;

        root.Content = inkCanvas1;
        root.Show();
        app.MainWindow = root;
        app.Run();
    }

}

Sketchpad.csproj

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">
  <PropertyGroup>
    <AssemblyName>Simply Sketch</AssemblyName>
    <OutputPath>Bin\</OutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="SketchPad.cs" />
  </ItemGroup>
  <Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
  </Target>
    <Target Name="Clean">
    <Delete Files="$(OutputPath)$(AssemblyName).exe" />
  </Target>
  <Target Name="Rebuild" DependsOnTargets="Clean;Build" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

这就是所需要的。诚然,如果您想避免 XAML,这基本上意味着您将不得不编写一堆备用 .NET 代码,因此这取决于您 - 您是否想将这些内容转储到 XAML 中,或者您愿意而是用代码编写?

我认为在 VS 中执行此操作的最大好处是您可以获得良好的设计师支持,因此通常很容易直观地看到所有内容的位置和去向。它也可能更少地查找文档,有时还需要进行一些跳跃。

但是没有 XAML 的 WPF也是可能的

您甚至不需要为此安装 Visual Studio,只需安装 .NET CLI 和开发人员命令提示符即可。将这两个文件放在一个文件夹中并运行msbuild,然后就可以运行Bin目录中的文件了。

Simplifying? No. But everything you can do in XAML you can just do in code. For example, here's a simple Windows Ink drawing app - pretty much as simple as you could possibly make it:

who needs visual studio? green ink on purple background

No saving, no changing anything - but you can through code.

Sketchpad.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Ink;

public class Sketchpad : Application {

    [STAThread]
    public static void Main(){
        var app = new Sketchpad();
        Window root = new Window();
        InkCanvas inkCanvas1 = new InkCanvas();

        root.Title = "Skortchpard";

        root.ResizeMode = ResizeMode.CanResizeWithGrip;
        inkCanvas1.Background = Brushes.DarkSlateBlue;
        inkCanvas1.DefaultDrawingAttributes.Color = Colors.SpringGreen;
        inkCanvas1.DefaultDrawingAttributes.Height = 10;
        inkCanvas1.DefaultDrawingAttributes.Width = 10;

        root.Content = inkCanvas1;
        root.Show();
        app.MainWindow = root;
        app.Run();
    }

}

Sketchpad.csproj

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="15.0">
  <PropertyGroup>
    <AssemblyName>Simply Sketch</AssemblyName>
    <OutputPath>Bin\</OutputPath>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Data" />
    <Reference Include="System.Xml" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Core" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="System.Net.Http" />
    <Reference Include="System.Xaml">
      <RequiredTargetFramework>4.0</RequiredTargetFramework>
    </Reference>
    <Reference Include="WindowsBase" />
    <Reference Include="PresentationCore" />
    <Reference Include="PresentationFramework" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="SketchPad.cs" />
  </ItemGroup>
  <Target Name="Build" Inputs="@(Compile)" Outputs="$(OutputPath)$(AssemblyName).exe">
    <MakeDir Directories="$(OutputPath)" Condition="!Exists('$(OutputPath)')" />
    <Csc Sources="@(Compile)" OutputAssembly="$(OutputPath)$(AssemblyName).exe" />
  </Target>
    <Target Name="Clean">
    <Delete Files="$(OutputPath)$(AssemblyName).exe" />
  </Target>
  <Target Name="Rebuild" DependsOnTargets="Clean;Build" />
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

That's all it takes. Granted, if you want to avoid XAML, that basically means you're going to have to write a bunch of alternate .NET code, so it's kind of up to you - do you want to dump of that stuff in XAML, or would you rather write it in code?

I think the biggest benefit to doing it within VS is you get good designer support so it's typically pretty easy to visually see where everything is and goes. It's also probably less looking up the documentation and sometimes having to make a few leaps.

But WPF without XAML is possible.

You don't even need Visual Studio installed for this, just the .NET CLI and the Developer Command Prompt. Drop these two files in a folder and run msbuild and then you can run the file in the Bin directory.

假装不在乎 2024-11-09 08:01:17

这个问题肯定需要一个指向 Bling UI Toolkit 的链接。它是一个超级天才的高级库,用于 WPF 之上的动画和丰富的 UI 原型设计。与 button.Width = 100 - slider.Value 绑定,动画如下:button.Left.Animate().Duration(500).To = label.Right,像素着色器编译器 - 太棒了。

可悲的是,我认为这个项目已经不再进行了。但很多非常聪明的想法值得深思。

This question definitely needs a link to the Bling UI Toolkit. It's a super-genius high-level library for animation and rich UI prototyping on top of WPF. Binding with button.Width = 100 - slider.Value, animation like this: button.Left.Animate().Duration(500).To = label.Right, a pixel shader compiler - amazing.

Sadly, I don't think the project is being worked on anymore. But lots of very smart ideas to give some food for thought.

給妳壹絲溫柔 2024-11-09 08:01:17

WPF 没有这样的框架。您在愿望清单中提到的三件事将是 WPF 已提供的组件的直接(且不同)替代品。此外,用您的版本替换绑定和资源系统将使您喜欢的 WPF 功能(动画、模板等)无法使用,因为它们严重依赖于绑定、资源等。

以下是一些可能会改善您的体验的建议。
1. 学习处理XAML(我以前也讨厌它,但现在我习惯了它太棒了)
2. 构建您自己的库,使您可以轻松地用代码创建 UI。毕竟,在 XAML 中完成的所有操作也可以在代码中完成。
3. 如果您真的讨厌 INotifyPropertyChanged,并且想要回调,请使用 DependencyProperty。没有事件可供您引发,并且您可以拥有回调和默认值!
4.) 不要使用WPF。尽管您说您喜欢该架构,但您列出的缺点/所需的“改进”几乎涵盖了所有内容。

There are no frameworks like this for WPF. The three things that you mention in your wishlist would be direct (and different) replacements for the components already provided by WPF. Also, replacing the binding and resource systems with your versions would make the things you like about WPF (animation, templating, etc.) unusable as they rely heavily on the binding, resources, etc.

Here are some suggestions that may improve your experience.
1. Learn to deal with XAML (I used to hate its guts too, but now that I'm used to it its great)
2. Build your own library that makes creation of the UI easy for you, in code. After all, everything that is done in XAML can be done in code just as well.
3. If you really hate INotifyPropertyChanged, and want a callback instead use a DependencyProperty instead. No event for you to raise, and you can have a callback and default values!
4.) Don't use WPF. Even though you say you love the architecture, your list of shortcomings / desired 'improvements' covers almost all of it.

冰雪梦之恋 2024-11-09 08:01:17
> non-INotifyPropertyChanged binding.

在您的 viewmodell 或 modell 中手动实现 INotifyPropertyChanged 是相当多的手动/重复性工作。不过,我读到了有关这些替代方案

  • DynamicViewModel: MVVM using POCOs with .NET 4.0 :该项目旨在提供一种使用普通旧 CLR 对象 (POCO) 实现模型视图视图模型 (MVVM) 架构模式的方法,同时充分利用 .NET 4.0动态对象类。
    利用 .NET 4.0 和 DynamicObject 类,我们可以创建从 DynamicObject 类派生的类型并指定运行时的动态行为。此外,我们可以在派生类型上实现 INotifyPropertyChanged 接口,使其成为数据绑定的良好候选者。

  • 更新控件.NET:WPF 和 Silverlight 数据绑定,无需 INotifyPropertyChanged。它会自动发现依赖关系,因此您不必在视图模型中管理它们。
    它可以与 Winforms 一起使用。使用事件通过代码进行绑定。

  • notifypropertyweaver :使用 IL 编织(通过 http://www.mono-project.com/Cecil) 将 INotifyPropertyChanged 代码注入属性。

    • 不需要任何属性
    • 无需参考资料
    • 不需要基类
    • 支持 .net 3.5、.net 4、Silverlight 3、Silverlight 4 和 Windows Phone 7
    • 支持客户端配置文件模式
> non-INotifyPropertyChanged binding.

To manually implement INotifyPropertyChanged in your viewmodell or modell is quite a lot of manual/repitative work. However I read about these alternatives

  • DynamicViewModel: MVVM using POCOs with .NET 4.0 :This project aims to provide a way to implement the Model View ViewModel (MVVM) architectural pattern using Plain Old CLR Objects (POCOs) while taking full advantage of .NET 4.0 DynamicObject Class.
    Taking advantage of the .NET 4.0 and the DynamicObject Class, we can create a type deriving from the DynamicObject Class and specify dynamic behavior at run time. Furthermore, we can implement the INotifyPropertyChanged Interface on the derived type making it a good candidate for Data Binding.

  • Update Controls .NET : WPF and Silverlight data binding without INotifyPropertyChanged. It discovers dependencies automatically so you don't have to manage them in your View Model.
    And it works with Winforms. Bind through code using events.

  • notifypropertyweaver : Uses IL weaving (via http://www.mono-project.com/Cecil) to inject INotifyPropertyChanged code into properties.

    • No attributes required
    • No references required
    • No base class required
    • Supports .net 3.5, .net 4, Silverlight 3, Silverlight 4 and Windows Phone 7
    • Supports client profile mode
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文