Tomash Vovran
Tomash Vovran

Reputation: 43

DisplayAlert from ViewModel not displaying

I need to display DisplayAlert from the View Model, however its simply doesn't display. Is there some other way how to display alert from the VM? The permission is true so that works.

 private async Task TakePicture()
        {
            await Permission();
            var imageSource = Application.Current.MainPage.DisplayActionSheet(AppResources.AlertNewPhoto, AppResources.AlertNewPhoto, AppResources.AlertGallery);
            if (imageSource.Result == AppResources.AlertNewPhoto)

}

Upvotes: 1

Views: 328

Answers (2)

TrASH Mail
TrASH Mail

Reputation: 1

I solved this problem using events

public MainPageVewModel()
{
   Application.Current.MainPage.Loaded += LoadCards;
}

private async void LoadCards(object sender, EventArgs e)
{
   // your code on View Loaded
  await Application.Current.MainPage.DisplayAlert("working alert", "alert", "ok");
}

Upvotes: 0

Leon Lu
Leon Lu

Reputation: 9234

You can change your constructor of ViewModel like following code.

 public PersonsViewModel(ContentPage page){

  page.DisplayAlert("info","test","Ok");

}

In your Layout background code, you can use it following code.

  public partial class MainPage : ContentPage
    {
        PersonsViewModel personsViewModel;
        public MainPage()
        {
           InitializeComponent();

            personsViewModel =  new PersonsViewModel(this);
            this.BindingContext = personsViewModel;



        }

If you can use plugin, you can use ACR.UserDialogs. https://github.com/aritchie/userdialogs

Upvotes: 2

Related Questions