Is "unobtrusive code" good or bad practice?

I'm a little surprised that when learning WPF / XAML / Silverlight, almost all the XAML / C # examples I came across have “Click” events in XAML and very few in the Window or Page constructor.

With all the emphasis these days on "non-intrusive Javascript," I would think that more developers would actually structure their XAML / code like this:

XAML:

<Grid> <Button x:Name="btnEdit"/> </Grid> 

Code behind:

 public Window1() { InitializeComponent(); btnEdit.Content = "Edit"; btnEdit.Click += new RoutedEventHandler(btnEdit_Click); } private void btnEdit_Click(object sender, RoutedEventArgs e) { btnEdit.Content = "This button was clicked."; } 

Any thoughts on why this would be good or bad practice?

+4
source share
4 answers

Most small examples of WPF give only the impression that it is possible without focusing on design issues or good style.

In real-world applications, XAML should only be used for declarative programming. For example, binding a command to a button or declaring a data binding. Karl Schifflett has excellent articles on MVVM , which very well separates the problems of your WPF / Silverlight application.

The code behind, in my opinion, is only suitable for tiny applications. It tends to mix presentation, control and data.

+6
source

If I remember correctly, I think there is a partial class that implements the Init code above, which is the code created by the visual studio. I can't speak for WPF, but it does it in ASP.Net 2.0, so I assume that it does the same here. It took me a while to get used to this.

I agree. I hate defining events in markup.

+1
source

I agree with your concern.

After much discussion, we follow a similar pattern of non-intrusive, highly accurate XAML strategies and encoding binding and data.

If you add events to XAML, the event code is displayed in the context menu. If you bind commands in XAML, there is no equivalent. You can move from a command declaration in XAML, but not where it is assigned to the Command property in the control.

0
source

MVVM is bad practice. You think you share data and presentation. So what? A common mechanism with XAML binding, binding commands, translating it into methods, and implementing INotifyPropertyChanged for what? For UTests (I did - I'm testing the on-on concept)? The right software is needed only in the user test ... This is your code is divided in such a way that you sometimes do not understand where what.

What are you using INotify for? Why does Microsoft ALL WPF controls and objects in general in WPF inherit from FrameworkElement using magic DependencyProperties?

Multiple binding is a method of hard resources by word (read the authors of MVVM).

I wrote a complex three-dimensional CAE system without any templates for less than a year ... with the classic organization of an application with classes and code. https://skydrive.live.com/?cid=ea6ad1087e3103f0&sc=photos&id=EA6AD1087E3103F0!103&sff=1#cid=EA6AD1087E3103F0&id=EA6AD1087E3103F0!118&sc=photos

All MVVM samples refer to customers in the company ... I propose to designate the MVVMCC template (Customer in the company)

-2
source

All Articles