【问题标题】:DisplayAlert from ViewModel not displaying来自 ViewModel 的 DisplayAlert 未显示
【发布时间】:2020-07-08 20:05:29
【问题描述】:

我需要从视图模型显示 DisplayAlert,但它根本不显示。还有其他方法可以显示来自 VM 的警报吗?权限为 true,因此有效。

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

}

【问题讨论】:

    标签: xamarin.forms


    【解决方案1】:

    您可以像下面的代码一样更改ViewModel 的构造函数。

     public PersonsViewModel(ContentPage page){
    
      page.DisplayAlert("info","test","Ok");
    
    }
    

    在您的 Layout 后台代码中,您可以使用以下代码。

      public partial class MainPage : ContentPage
        {
            PersonsViewModel personsViewModel;
            public MainPage()
            {
               InitializeComponent();
    
                personsViewModel =  new PersonsViewModel(this);
                this.BindingContext = personsViewModel;
    
    
    
            }
    

    如果你可以使用插件,你可以使用ACR.UserDialogshttps://github.com/aritchie/userdialogs

    【讨论】:

    • 如果以上回答对您有帮助,请采纳为回答(点击此回答左上角的✔),对其他有类似问题的人会有所帮助。
    • 你好 Leon,抱歉我没有看到你回答。如果它有效,我稍后会告诉你。谢谢
    • 嗨,我使用了不同的解决方案。对不起......stackoverflow.com/questions/49172374/…
    • 好的,两个都工作了,能不能采纳我的答案或者分享你的答案,然后采纳,对有类似问题的其他人有帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多