没有 XAML 的 WPF
从架构上来说,我认为 WPF 非常令人惊奇。总的来说,我非常喜欢底层渲染/动画的内部工作原理。模板和样式设置的灵活性令人印象深刻。
但我讨厌 XAML - 我觉得它使很多事情变得复杂。我已经在大大小小的应用程序中使用过它,并且我发现自己多次试图弄清楚如何在 XAML 中执行某些操作,其基本原理是基本的,但语法却很古怪。不仅如此,我多次想知道解析/绑定的某些部分有多重。 (我知道它已编译,但我不确定运行时仍评估多少)
XAML 只是构建和加载可视化树的一种方法。是否有任何框架可以简化以非 XML、基于代码(但仍然主要是声明性)方式构建可视化树的框架?具体来说,我对能够在保留 MVVM 方法的同时缓解以下任何问题的框架感兴趣:
强类型绑定。指定 ViewModel 必须符合特定类型。我认为 BaseBinding 在底层使用了反射,并且我对其速度有点怀疑,更不用说损坏的绑定很烦人。
更快的绑定,非
INotifyPropertyChanged
绑定。似乎可以创建某种BindableProperty
,并且绑定可以直接侦听它,而不是接收所有 ViewModel 属性更改。与字符串参数相比,使用直接回调似乎也更有优势。不同的资源管理方法;同样,某种类型的强类型字典可能会非常好。我几乎希望将样式视为 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:
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.
Faster binding, non-
INotifyPropertyChanged
binding. It seems like some sort ofBindableProperty<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.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我支持您使用无 Xaml 的 WPF。我喜欢 WPF 的布局和绑定功能,但我也讨厌 XAML。我希望 WPF 可以用纯 C# 编写,有一些优点:
语法。我认为 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:
<Setter>
syntax.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.
简化?不。但是您可以在 XAML 中执行的所有操作都可以在代码中执行。例如,这是一个简单的 Windows Ink 绘图应用程序 - 非常简单:
无需保存,无需更改任何内容 - 但您可以通过代码。
Sketchpad.cs
Sketchpad.csproj
这就是所需要的。诚然,如果您想避免 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:
No saving, no changing anything - but you can through code.
Sketchpad.cs
Sketchpad.csproj
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 theBin
directory.这个问题肯定需要一个指向 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.
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.
在您的 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 代码注入属性。
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.