Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Best Practice on View & View Model Seperation

henda79henda79 Member ✭✭

I looking for peoples opinion on firing user dialogs from the View Model.

Over the last several months I've had to learn Xamarin, XAML and MVVM to start a new project and I have to say its been fun :) I'm part way through the project and I'm starting to doubt the use of user dialogs which are fired from the VM.

Now correct me if I'm wrong; but the VM should know nothing of the View and should be able to function without a UI attached, so it basically fully testable !?!

So if that's the case surely the logic for dialogs should be placed in the code behind along with other functions specifically for the UI.

At the moment I'm using MVVMCross and Dependency Injection to allow the use of dialogs in the VM and it works fine, I'm just getting the feeling its not right.

Best Answer

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @henda79

    Different people have different approaches to this. Personally, I do not have the View Model display dialogs/alerts etc., taking the approach that the View should be the only place where UI operations are done.

    My approach is that the View Model maintains state (or provides access to the state in the Model). When state is changed (a state transition), that typically means that properties change value and raise PropertyChanged events. There may be occasions where the state is not made public through properties, but it's still useful to inform the View that something has happened. Again, an event can be used for this, but an explicit one rather than the usual PropertyChanged event. Views can then react to this event being raised, in whatever way is appropriate for the View. That may be that the View presents a dialog/alert or navigates to a new Page.

    For example, in a particular View Model, I have

    public event EventHandler SaveDone;
    

    In a View, I then have:

    viewModel.SaveDone += PageView_SaveDone;
    

    and

            private async void PageView_SaveDone(object sender, EventArgs e)
            {
                // In my code, I'm popping a page here, but it would also be fine to display a dialog/alert or navigate to another page here
            }
    

    Hope that helps.

    (As I said, other people will do this differently).

Answers

  • JohnHardmanJohnHardman GBUniversity admin
    Accepted Answer

    @henda79

    Different people have different approaches to this. Personally, I do not have the View Model display dialogs/alerts etc., taking the approach that the View should be the only place where UI operations are done.

    My approach is that the View Model maintains state (or provides access to the state in the Model). When state is changed (a state transition), that typically means that properties change value and raise PropertyChanged events. There may be occasions where the state is not made public through properties, but it's still useful to inform the View that something has happened. Again, an event can be used for this, but an explicit one rather than the usual PropertyChanged event. Views can then react to this event being raised, in whatever way is appropriate for the View. That may be that the View presents a dialog/alert or navigates to a new Page.

    For example, in a particular View Model, I have

    public event EventHandler SaveDone;
    

    In a View, I then have:

    viewModel.SaveDone += PageView_SaveDone;
    

    and

            private async void PageView_SaveDone(object sender, EventArgs e)
            {
                // In my code, I'm popping a page here, but it would also be fine to display a dialog/alert or navigate to another page here
            }
    

    Hope that helps.

    (As I said, other people will do this differently).

  • JohnHardmanJohnHardman GBUniversity admin

    @henda79 - Did the above answer your question?

  • henda79henda79 Member ✭✭

    @JohnHardman, thanks for your input. I know its not a straight Yes / No type question / answer, but it seems you have the same opinion as me.

Sign In or Register to comment.