Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
283 views
in Technique[技术] by (71.8m points)

c# - MVVM Understanding Issues

A list of my questions towards mvvm, you don't need to answer on your own, links which help me further are always appreciated:

  1. If I have a Mainpage.xaml file and I'm using a viewmodelclass which code should be in the Mainpage.xaml.cs file? Nothing?

  2. Should that piece of code be in the Mainpage.xaml.cs file:

    Viewmodel base = new Viewmodel();

  3. If I implement ICommands how can I access for example a textbox.text on the Mainpage.xaml?

  4. ICommands completely replace the Button.Click event? Don't they?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. If I have a Mainpage.xaml file and I'm using a viewmodelclass which code should be in the Mainpage.xaml.cs file? Nothing?

You need to understand what's is view's and what viewmodel's responsibility. Codebehind is part of view, so only code that is covered by view's responsiblity should be in code behind.

Sometimes databinding to viewmodel is hard in xaml (e.g. PasswordBox) Then you can use codebehind to get the password or catch changes and set property in viewmodel. You should not validate the password agains't server in codebehind.

Sometimes it's difficult to define animations in xaml. Feel free to create or trigger them in C# (but in that case you wont be able to edit then in Expression Blend)

It's ok to use evens like Loaded / Unloaded in codebehind, e.g. I use them to call Activate/Deactivate methods in my viewmodels.

This is codebehind sample, that does not violate the view and viewmodel separation of concerns:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Closing += MainWindow_Closing;
    }

    MainWindowViewModel ViewModel
    {
        get { return (MainWindowViewModel)DataContext; }
    }

    void MainWindow_Closing(object sender, CancelEventArgs e)
    {
        var canExit = ViewModel.ShowConfirmExitDlg();
        if (!canExit) e.Cancel = true;
    }
}

In other words, sometimes you have to use codebehind. There are people that are saying that you should never use it, but be pragmatic and use it, when it helps you.

  1. Should that piece of code be in the Mainpage.xaml.cs file: Viewmodel base = new Viewmodel();

yep, that's ok. However you should use d:DataContext in xaml in order to let Visual Studio Designer know what datacontext you are using so it could provide you with intellisense and design time support. There is a lot of ways how to set instantiace viewmodel and set it to datacontext. It's up to you whatever you choose.

  1. If I implement ICommands how can I access for example a textbox.text on the Mainpage.xaml?

Create string property in viewmodel and databind the textbox.text to it. Use DelegateCommand, where you can pass method that has access to the property:

public class MainWindowViewModel 
{
    public MainWindowViewModel()
    {
        LoginCommand = new DelegateCommand(Login);
    }

    public DelegateCommand LoginCommand { get; private set; }

    public string Username { get; set; }
    public string Password { get; set; }

    private void Login()
    {
        if (Username == "user" && Password == "user")
        {

        }
    }
}

just FYI, there is also another way of passing parameter to command: check CommandParameter feature [link]

  1. ICommands completely replace the Button.Click event? Don't they?

Almost. The same rules as in answer to first question applies also here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...