locked
How to use DisplayAlert from a viewmodel without additional frameworks RRS feed

  • Question

  • User378323 posted

    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.

    Friday, December 7, 2018 3:20 AM

All replies

  • User2148 posted

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

    Friday, December 7, 2018 8:01 AM
  • User378323 posted

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

    Friday, December 7, 2018 8:15 AM
  • User2148 posted

    something like

    await Application.Current.MainPage.DisplayAlert("AA", "BB", "Ok");
    
    Friday, December 7, 2018 9:24 AM
  • User76049 posted

    @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

    Friday, December 7, 2018 9:33 AM
  • User41045 posted

    @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

    Friday, December 7, 2018 12:10 PM
  • User74 posted

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

    Friday, December 7, 2018 12:32 PM
  • User180523 posted

    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.

    Friday, December 7, 2018 1:21 PM
  • User76049 posted

    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.

    Friday, December 7, 2018 1:51 PM
  • User72117 posted

    @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

    Thursday, March 21, 2019 2:00 PM
  • User76049 posted

    @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 :)

    Thursday, March 21, 2019 5:10 PM
  • User370052 posted

    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. :/

    Wednesday, October 2, 2019 12:33 AM
  • User389442 posted

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

    Wednesday, October 2, 2019 6:28 AM
  • User76049 posted

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

    Wednesday, October 2, 2019 8:25 AM