覆盖Xamarin表单中的警报方法

时间:2019-01-22 14:14:44

标签: xamarin xamarin.forms

我要覆盖警报方法。警报应具有当前内容+ 显示图形(带或不带文本)。

        public IDisposable Alert(string message, string title = null, string okText = null)
    {
        if (!SingletonsManager.Instance.RuntimeManagerInstance.AppInBackground)
        {
            return UserDialogs.Instance.Alert(message, title, okText);
        }

        return null;
    }

1 个答案:

答案 0 :(得分:0)

使用Plugins.Popup之类的包来实现此目的比较容易,没有自定义渲染器,就不可能将图像添加到默认的AlertDialog中,这将限制您的工作。

使用弹出插件,您只需将其添加到解决方案中,即可在iOS和Android中初始化:

@Test
public void testSomething() {

    withDefaultContextA(() -> {
        ... // do some asserts
    }

    withContext((new Context(...)) -> {
        ... // do some asserts
    }
}

创建弹出页面

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
      Rg.Plugins.Popup.Popup.Init();

      global::Xamarin.Forms.Forms.Init ();
      LoadApplication (new App ());
      return base.FinishedLaunching (app, options);
    }
}

而且,要显示在您的页面中:

<?xml version="1.0" encoding="utf-8" ?>
<pages:PopupPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:pages="clr-namespace:Rg.Plugins.Popup.Pages;assembly=Rg.Plugins.Popup"
    xmlns:animations="clr-namespace:Rg.Plugins.Popup.Animations;assembly=Rg.Plugins.Popup"
    x:Class="MyProject.MyPopupPage">

    <pages:PopupPage.Animation>
        <animations:ScaleAnimation 
            PositionIn="Center"
            PositionOut="Center"
            ScaleIn="1.2"
            ScaleOut="0.8"
            DurationIn="400"
            DurationOut="300"
            EasingIn="SinOut"
            EasingOut="SinIn"
            HasBackgroundAnimation="True"/>
    </pages:PopupPage.Animation>
    <!--You can use any elements here which are extended from Xamarin.Forms.View-->
    <StackLayout 
        VerticalOptions="Center" 
        HorizontalOptions="Center" 
        Padding="20, 20, 20, 20">
        <Label
            Text="Test"/>
    </StackLayout>
</pages:PopupPage>