简体   繁体   English

在WPF应用程序中是否过度使用了代码?

[英]Overusing code behind in my WPF application?

I'm creating a simple WPF application for practice purposes only, but I have little experience in WPF. 我正在创建一个简单的WPF应用程序,仅出于练习目的,但是我对WPF的经验很少。 Using WPF XAML seems troublesome to me, as I have a lot more experience with C# Console and especially with the C#- and JS-driven videogame engine Unity, where devs have access to an Editor (analogous to the Design Window in WPF) and the code behind, with no XAML whatsoever. 使用WPF XAML对我来说似乎很麻烦,因为我对C#控制台,尤其是C#和JS驱动的视频游戏引擎Unity有更多的经验,开发人员可以使用编辑器(类似于WPF中的“设计窗口”)和后面没有任何XAML代码。 This is why I tend to use less XAML and much code behind. 这就是为什么我倾向于使用较少的XAML和大量代码的原因。

For example here, I modify the Margin property of a button, while I set its Opacity to 1; 例如,在这里,我修改按钮的Margin属性,同时将其Opacity设置为1; this all happens upon a click event to the grid, which calls the function (exactly the way it's done in Unity): 所有这一切都发生在网格的click事件上,该事件调用了函数(正是在Unity中完成的方式):

public  void Grid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
        {
            RandomButton.Margin = new Thickness(0, 0, 0, 0);
            RandomButton.Opacity = 1;

        }

If I'm not mistaken, this can be achieved through Data Binding in XAML as well, although it seems to be a lot more complicated method seemingly producing the same result. 如果我没记错的话,这也可以通过XAML中的数据绑定来实现,尽管看起来似乎要产生相同结果的方法似乎要复杂得多。

So my questions are: is there a difference between the two ways, say, performance-wise or in terms of expandability (which one would be easier to expand/modify later)? 因此,我的问题是:两种方式之间是否存在差异,例如在性能方面还是在可扩展性方面(稍后会更容易扩展/修改)? Which one is used in the industry? 行业中使用哪一种?

There is no difference as far as the performance is concerned. 就性能而言,没有区别。 For any element that you define in your XAML markup, an instance of a corresponding type will be created by the framework at runtime. 对于您在XAML标记中定义的任何元素,框架将在运行时创建相应类型的实例。

It is generally much easier to define the visual presentation of an application using XAML though. 但是,使用XAML定义应用程序的视觉表示通常要容易得多。 This is what XAML is used for after all: https://msdn.microsoft.com/en-us/library/cc295302.aspx 毕竟,这就是XAML的用途: https : //msdn.microsoft.com/zh-cn/library/cc295302.aspx

You still use a programming language such as C# to implement your application logic though, ie what happens when a mouse button is pressed etc. So setting/modifying the properties of any element that you originally create in the XAML markup using C# is fine. 但是,您仍然使用诸如C#之类的编程语言来实现您的应用程序逻辑,例如,当按下鼠标按钮时会发生什么。因此,设置/修改最初使用C#在XAML标记中创建的任何元素的属性都可以。

Which one is used in the industry? 行业中使用哪一种?

When it comes to enterprise WPF applications the recommended design pattern to use is MVVM (Model-View-ViewModel). 对于企业WPF应用程序推荐使用的设计模式是MVVM(模型-视图-视图模型)。 You can refer to the following link for more information about it: https://msdn.microsoft.com/en-us/library/hh848246.aspx 您可以参考以下链接以获取有关它的更多信息: https : //msdn.microsoft.com/zh-cn/library/hh848246.aspx

The view is responsible for defining the structure, layout, and appearance of what the user sees on the screen and is typically created using only XAML whereas the view model and the model that are responsible for handling the view logic and business logic respectively is implemented using a programming language like C#. 视图负责定义用户在屏幕上看到的内容的结构,布局和外观,并且通常仅使用XAML创建,而视图模型和分别负责处理视图逻辑和业务逻辑的模型则使用以下方法实现像C#这样的编程语言。

You will find a lot more information and samples of how to implement the MVVM design pattern if you Google or Bing for it. 如果您是Google或Bing,您将找到更多有关如何实施MVVM设计模式的信息和示例。

Hi being a WPF/XAML developer I would suggest you to use XAML as much as you can.Make use of commands and bindings ie use MVVM architecture. 作为WPF / XAML开发人员,我建议您尽可能多地使用XAML。充分利用命令和绑定,即使用MVVM体系结构。 What if you want to set the opacity using keys (example: ctrl+shift) you will have to write another code behind for that making your code redundant affecting performance of your app. 如果您想使用按键设置不透明度(例如:ctrl + shift),您将不得不在后面编写另一个代码,以使代码变得多余,从而影响应用程序的性能。 Make use of MVVM pattern ( You can use MVVM light by galasoft). 利用MVVM模式(您可以使用Galasoft的MVVM light)。 As of expanding or modification you will have to modify a single command method rather than modifying each event etc on the code behind. 在扩展或修改时,您将不得不修改单个命令方法,而不是修改后面代码中的每个事件等。 Easy to test ..Easy to build..Easy to modify.. 易于测试..易于构建..易于修改..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM