MVVMCross中的Acr.UserDialogs:通知问题

时间:2018-01-23 13:52:57

标签: android notifications xamarin.android dialog mvvmcross

我有Xamarin Android项目并使用MVVMCross。

我需要显示通知。它应该是文字和图像。我用它创建了.axml文件,我需要在点击按钮后显示它。

我在这里做了研究并找到了Acr.UserDialogs。我安装了它:

PM> Install-Package Acr.UserDialogs -Version 7.0.0

...View.axml其中一个中我有按钮:

<Button
    android:id="@+id/enterButton"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_below="@+id/resendCodeLink"
    android:layout_centerHorizontal="true"
    local:MvxBind="Text Strings[EnterButton]; Click EnterCommand" />

点击此按钮后,我希望用户看到通知窗口。我将逻辑添加到ViewModel.cs

using MvvmCross.Core.ViewModels;
using System.Windows.Input;
using MvvmCross.Platform;

namespace My.Hello.Core.ViewModels
{
    public class HellooneViewModel : BaseViewModel
    {
        public ICommand EnterCommand => new MvxCommand(() => {
        Mvx.Resolve<IUserDialogs>().Alert("message");
    });
    }
}

但我需要看起来像这样的通知(而不是只有一个字符串)。如何添加警告,显示此notification.axml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:layout_width="280dp"
    android:layout_height="280dp"
    android:layout_marginTop="114dp">
<TextView
    android:id="@+id/noticeText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:layout_marginTop="24dp"
    android:textSize="20sp"
    android:text="Hello" />
<ImageView
    android:id="@+id/noticeImage"
    android:src="@mipmap/my_ic_90dp"
    android:layout_marginLeft="95dp"
    android:layout_marginRight="95dp"
    android:layout_marginTop="103dp" />
</LinearLayout>

所以我尝试使用Acr.UserDialogs。我的App.cs

using MvvmCross.Platform;
using MvvmCross.Platform.IoC;

namespace My.Hello.Core
{
    public class App : MvvmCross.Core.ViewModels.MvxApplication
    {
        public object UserDialogs { get; set; }

        public override void Initialize()
        {
            CreatableTypes()
                .EndingWith("Service")
                .AsInterfaces()
                .RegisterAsLazySingleton();

            Mvx.RegisterSingleton<IUserDialogs>(() => UserDialogs.Instance);

            RegisterCustomAppStart<AppStart>();
        }
    }

    internal interface IUserDialogs
    {
        void Alert(string v);
    }
}

还有一个问题是Instance有红色下划线,但例外情况是:“对象不包含实例的定义......”我应该创建什么实例以及如何创建?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是因为UserDialogs是您在该类中创建的object类型。相反,你应该这样称呼它:Acr.UserDialogs.Instance(它可能是全部大写ACR.UserDialogs.Instance,我记不太清楚了)

不需要public object UserDialogs { get; set; }