How to use DisplayAlert from a viewmodel without additional frameworks

lavilasolavilaso ✭✭Member ✭✭

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?

  • AlessandroCaliaroAlessandroCaliaro ✭✭✭✭✭ ITMember ✭✭✭✭✭

    something like

    await Application.Current.MainPage.DisplayAlert("AA", "BB", "Ok");
    
  • NMackayNMackay mod GBInsider, University mod

    @lavilaso

    Don't reference the app directly from your view model if possible, if flys in the face of the many benefits of a clear SOC between UI and viewmodel and is anti pattern.

    There's a sample here,

    https://github.com/mackayn/MVVM-Light-Sample

    It's uses MVVM Light's dialog service but just role your own, it's really simple.

    Also for example, I had to change all dialogs in out current app, because the original developer designed it correctly and used an interface implementation, I had to change 30 lines of code instead of every every line of dialog related code in the view model. I'm sorry but it's good design, not a purist approach.

    This prism sample also shows this concept in action
    https://github.com/mackayn/CrudSample

  • AnubhavRanjanAnubhavRanjan Xamurai 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 mod GBInsider, University mod

    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 mod GBInsider, University mod

    @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 mod GBInsider, University mod

    @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.