XAML or C# code-behind

后端 未结 20 1840
盖世英雄少女心
盖世英雄少女心 2020-12-13 02:03

I don\'t like to use XAML. I prefer to code everything in C#, but I think that I am doing things wrong.

In which cases it is better to use XAML and when do you use C

相关标签:
20条回答
  • 2020-12-13 02:34

    Check out this video on MVVM in WPF. If you want wrap your head around how to organize a WPF application vis-a-vis what goes in XAML, code behind and other abstractions, this is a great place to start.

    0 讨论(0)
  • 2020-12-13 02:35

    I originally came from the Web side of development and am learning WPF/Silverlight at the moment. For me, the XAML model makes much more sense to me than WinForms ever did. I treat the XAML as if it were HTML and the .cs files just like a code-behind.

    0 讨论(0)
  • 2020-12-13 02:39

    The most important thing to bear in mind is that XAML is for presentation. All your presentation should be in the XAML. If you have logic, you keep that out of your XAML - in your C#.

    Imagine swapping out your XAML file with one that LOOKS completely different - but still uses the same data - that's where the division should be.

    0 讨论(0)
  • 2020-12-13 02:39

    XAML can be seen as being similar to a combination of XHTML and CSS or XML and XSL which are used for structure and design. Any form of logic should be in C#. This way structure and design are seperated from logic. Your code should be cleaner too by using this approach. Another positive thing is that tasks can be easier to separate between designers and programmers.

    One more thing... this is the definition of XAML from MSDN:

    Extensible Application Markup Language (XAML) is a markup language for declarative application programming. Windows Presentation Foundation (WPF) implements a Extensible Application Markup Language (XAML) loader and provides Extensible Application Markup Language (XAML) language support for Windows Presentation Foundation (WPF) types such that you can create the majority of your application UI in Extensible Application Markup Language (XAML) markup. In addition, the SDK includes a Extensible Application Markup Language (XAML) editing tool called XAMLPad. You can use this tool to experiment with Extensible Application Markup Language (XAML) in real time.

    Link to quote.

    0 讨论(0)
  • 2020-12-13 02:40

    One of the nice things about XAML is the separation of presentation an logic. This separation is not just theoretical but also practical. I my case, most of the my UIs are now being handled by a designer. This designer uses blend and does not know C#, has no interest of learnning c#, and frankly should not need to. This designer is a true designer, an artist, that knows how to use those tools to make things look really really nice. Basically my phylosipy is this, the more I use XAML, the less work I have to do on the UI because he can do it. This has worked well for us. I usually design my control to be lookless, give them a basic no frill look, use the DataContext to bind my object(btw DataTriggers are a great way to cut down on code). As a result, I will often checkin my code, come back the next day, synch it, and the UI will look completely different, but everything still work!!!

    Of course, it took at least 1/2 year to get there, but now this model seem to work and our UI look Kick A##, our application earns high praise, and I do little work on the UI itself and get to work on cooler things. To make a long story short, I think the code behind might be a bit more developer centric and forgets a whole other group that can benefit, trive, and make a living using WPF, namely the designers.

    Of course, there are still times when it takes a devloper to make XAML/WPF sing and dance, and sometime we need to educate the designers about the right way to do things, but I think its worth the investement pays off many times over in large scale projects (maybe not so in short one0

    0 讨论(0)
  • 2020-12-13 02:41

    Basically, XAML is meant for expressing visual-design, C# is meant for expressing logic.

    Any visual design should be done in XAML, any logic should be implemented in C#.

    - This enables giving the visual design to a designer to play on without worrying about changes to the logic and even replacing the entire visual design at run time using loose-XAML.

    - This also means you could replace either the logic or the visual-design without "breaking" either.

    - The connection between the two should be done with data bindings and with command bindings.

    The practice I use is:

    1. Define the model (the business data object model) in separate C# code.

    2. Define the constant parts of the view (the constant parts of the graphical user interface, e.g. the windows, menus, ...) in XAML (preferably use Blend and not VS for this).
    * Do not define styling (colors, fonts, ...) here.
    * Do not write event handlers for buttons (in most cases) in code-behind-the-XAML, use command-bindings instead.

    3. Define how the model is presented within the view (the GUI for viewing/editing the data objects) using XAML "ResourceDictionary"s located in separate files.

    - Write using blend then add bindings to XAML using VS (Jetbrains' Resharper add-on for VS will help with binding expressions).

    - If the object types are not known during design-time you can use "loose-XAML" and place the XAML in a folder which can have files added to / edited within without recompiling.

    4. Create a connection between the model and the view (a controller/view-model) in C# which:
    * Creates views as necessary (for dynamics objects)
    * Data-binds the view to the model (sets view's DataSource as the relevant object within the model)
    * Implements the commands
    * Command-binds the view to the command implementations within itself

    5. In Application.xaml delete the StartupUri="MainWindow.xaml" and add Startup="ApplicaitonStartUp" instead.
    Within the ApplicationStartUp() event handler:
    * Load any loose-XAMLs you have
    * Create the controller
    * Create the main window
    * Create the model
    * Connect controller to model and main window
    * Show main window
    * (Save model, controller and main window into private fields here to make sure they are all kept alive)

    6. Add styling (colors, fonts) to a separate XAML file under a ResourceDictionary (using blend for this or purchase a ready made XAML theme/skin file).

    0 讨论(0)
提交回复
热议问题