Alerts or Popups in MvvmCross

2019-04-12 05:10发布

问题:

Does MvvmCross support a cross platform solution for displaying alerts or popups?

Searching the code I found MvxDialogActivityView but it has been commented out. Will this remain the case for now?

If there is no direct support how would you suggest this is best done? (Perhaps the ViewModel would change a property and call FirePropertyChanged so that the View would be aware of it and show an alert.)

Edit 16:04 16th June 2012

What I am trying to do for this specific case is as follows: On the page a button is clicked, which causes a method to run in the ViewModel which does an evaluation to determine which of two messages should be shown to the customer. The message would be shown as an alert or popup (either native, or preferably totally styled by me). The message would fade after (the click of the OK button, or preferably 3 seconds).

After the message has been dismissed a new page will be navigated too (depending on which of the two messages was shown).

回答1:

How to handle this definitely depends on what the situation is - there's no single best rule (IMHO)

For a general error display pattern, there's one proposal at http://slodge.blogspot.co.uk/2012/05/one-pattern-for-error-handling-in.html

I've used similar patterns for showing application level notifications - e.g. for when a long running operation completes or when a chat message arrives or...

One interesting post about how to display message boxes was: http://awkwardcoder.blogspot.co.uk/2012/03/showing-message-box-from-viewmodel-in.html - I'm not sure I'd completely follow the end solution, but there are definitely some good points there about what not to do.


For your updated scenario, I would consider using a messenger (like TinyMessenger) or using normal C# events exposed by the ViewModel and consumed by its View

On the page a button is clicked, which causes a method to run in the ViewModel

I would implement this using an ICommand bound to the button Click/Tap/TouchDown

which does an evaluation to determine which of two messages should be shown to the customer.

I would definitely implement the logic within a Service

This would be called from the ViewModel - and the result/decision would probably cause some property or private field state change.

How does the View then decide to show a message? I can think of 3 options:

  1. The View could just respond to a Property change (normal Mvvm INPC) - this would be my preference
  2. The ViewModel could expose a normal C# event which it triggers...
  3. The ViewModel could send a Message

This last option (Messenging) is probably the most flexible solution here - it decouples the View and ViewModel in case you later decide to change responsibilities. To implement messenging, either:

  • implement your own hub (like I do for errors in http://slodge.blogspot.co.uk/2012/05/one-pattern-for-error-handling-in.html)
  • or use a generic solution like TinyMessenger

The message would be shown as an alert or popup (either native, or preferably totally styled by me).

This is a View concern - so would be entirely controlled by the View project. I'd use controls like: UIAlert, Toast, ToastPrompt, etc - all of which can be styled

The message would fade after (the click of the OK button, or preferably 3 seconds). After the message has been dismissed...

I'd use some form of Code Behind (or maybe a Behaviour in WP7) in the View. This would detect the click/fade/disappear and would then invoke either an ICommand (my preference) or public method on the ViewModel

a new page will be navigated too

This navigation would be requested from the ViewModel

(depending on which of the two messages was shown).

This would be easy to track through the above flow... presumably the ViewModel already knows what to show.


So that's what I'd do...

  • it keeps the application flow logic inside the ViewModels (and lower)
  • it keeps the presentation inside the Views

...but I'm sure there are other options :)


One final note... the fade out and then navigate logic can really get "messed up" by Switching/Tombstoning on WP7 and Android - this may or may not matter for your particular scenario.