Forum Xamarin Xamarin.Forms

How to use DisplayAlert from a viewmodel without additional frameworks

I need to launch a DisplayAlert () from the body of a command in my viewmodel, how can I do this without using additional libraries?
I have the idea that it should be done with the MessagingCenter like all these kinds of things but I would like someone more experienced to confirm it and if possible, indicate the correct way to use it without causing loss of memory.

Answers

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    I am not a MVVM purist so I call DisplayAlert directly in the ViewModel, and I feel fine

  • lavilasolavilaso Member ✭✭

    @AlessandroCaliaro said:
    I am not a MVVM purist so I call DisplayAlert directly in the ViewModel, and I feel fine

    Can you write the way you do it?

  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @lavilaso I believe it's better to keep the ViewModel separated from UI.
    However, you can look into this link which explains making use of the MessageCenter approach.
    https://forums.xamarin.com/discussion/22499/looking-to-pop-up-an-alert-like-displayalert-but-from-the-view-model-xamarin-forms-labs

    Just in case, you can also look at this Plugin:
    https://github.com/aritchie/userdialogs

  • JohnHJohnH GBMember ✭✭✭✭✭

    @lavilaso said:
    I need to launch a DisplayAlert () from the body of a command in my viewmodel, how can I do this without using additional libraries?
    I have the idea that it should be done with the MessagingCenter like all these kinds of things but I would like someone more experienced to confirm it and if possible, indicate the correct way to use it without causing loss of memory.

    We simply wrap calls to DisplayAlert in our own IAlertHelper and inject that into view models. Easy to mock, easy to unit test.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    You're going to outgrow the simple DisplayAlert quickly. Everyone does. First you get that simple alert going... Then a week later your boss wants different buttons like yes, no, cancel.... then you want to it auto-dismiss after 20 seconds... then you want a different background color {...}

    Consider using rg.plugins.popups to make your alerts and other popups. That way you have total control over the look and content. If you start using it from the start of your app you don't have to go in and refactor a bunch of code at a cost of 20 hours.

  • NMackayNMackay GBInsider, University admin

    Tis prism sample > @ClintStLaurent said:

    You're going to outgrow the simple DisplayAlert quickly. Everyone does. First you get that simple alert going... Then a week later your boss wants different buttons like yes, no, cancel.... then you want to it auto-dismiss after 20 seconds... then you want a different background color {...}

    Consider using rg.plugins.popups to make your alerts and other popups. That way you have total control over the look and content. If you start using it from the start of your app you don't have to go in and refactor a bunch of code at a cost of 20 hours.

    Yeah, we went down that route, we have popups for alerts, fancy busy popups, toasts & ActivitySheet, all behind an interface in the base class of course :smile: Works really well.

  • nick5454nick5454 USUniversity ✭✭✭

    @NMackay do you realize your prism project doesn't have the solution file on the root. I have issues with Git in Visual Studio doing that to me sometimes

  • NMackayNMackay GBInsider, University admin

    @nick5454 said:
    @NMackay do you realize your prism project doesn't have the solution file on the root. I have issues with Git in Visual Studio doing that to me sometimes

    Hi,

    yeah I do, that solution was knocked in in 3 hours as a coding test for an interview, it was never meant to be shared as a solution :)

  • MarceloPsMarceloPs Member ✭✭

    Hi, so i can't do this without an plugin or framework? In my case I am creating a study project to learn more about how MVVM works. :/

  • sparkist97sparkist97 Member ✭✭

    @MarceloPs UserDialogs is easy to use and amazing, you should check it out

    I'm sorry but I don't see how dialogs could help you understand MVVM better. MVVM is simple, you have your M (models), which only purpose is to describe data and maybe some methods that are only dependent on the data.
    You have your V (views) i.e. the UI, what the user sees and interacts with, it shouldn't call any logic code, so no web service calls, no database updates, etc.
    And finally you have your VM, the layer that calls business logic, update databases and do calculations, it has to be view free.

  • NMackayNMackay GBInsider, University admin

    @sparkist97 said:
    @MarceloPs UserDialogs is easy to use and amazing, you should check it out

    I'm sorry but I don't see how dialogs could help you understand MVVM better. MVVM is simple, you have your M (models), which only purpose is to describe data and maybe some methods that are only dependent on the data.
    You have your V (views) i.e. the UI, what the user sees and interacts with, it shouldn't call any logic code, so no web service calls, no database updates, etc.
    And finally you have your VM, the layer that calls business logic, update databases and do calculations, it has to be view free.

    You should separate your dialog implementation into an interface, that way it can be mocked and unit tested and your ViewModel doesn't reference the UI components.

Sign In or Register to comment.