Xamarin.Form 弹出自定义对话框

第一步先在MainActivity.cs 文件把当前Activity 获取。注意:Xamarin.Form是可以直接调用安卓原生方法的,有些伙伴可能比较迷茫

	public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
	{
		protected override void OnCreate(Bundle bundle)
		{
			TabLayoutResource = Resource.Layout.Tabbar;
			ToolbarResource = Resource.Layout.Toolbar;
			base.OnCreate(bundle);
			global::Xamarin.Forms.Forms.Init(this, bundle);//获取当前的活动视图
			LoadApplication(new App());
		}
	}
然后创建Layout文件,

xml代码如下:



    
    
    
然后再创建对应的cs文件;

using Android.App;
using Android.Content;
using Lierda.HandApp.Droid;
public class CreateAreaInformation : Dialog
{
	public Context context { get; private set; }
	public CreateAreaInformation(Context context) : base(context)
	{
		this.context = context;
	}
	protected override void OnCreate(Android.OS.Bundle savedInstanceState)
	{
		this.SetContentView(Resource.Layout.CreateAreaInformationDialog);
		var dialogWindow = this.Window;
		var act = context as Activity;
		var manager = act.WindowManager;
		var display = manager.DefaultDisplay;
		var attr = dialogWindow.Attributes;
#pragma warning disable CS0618 // Type or member is obsolete
		attr.Width = (int)(display.Width * 0.8);
#pragma warning restore CS0618 // Type or member is obsolete
		dialogWindow.Attributes = attr;
		base.OnCreate(savedInstanceState);
	}
}

这里需要继承Dialog 类,注意命名空间,别弄错了。


最后便是调用了。

        /// 
        /// 单击新增小区
        /// 
        /// 
        /// 
        void Handle_Clicked(object sender, System.EventArgs e)
        {
	  //注意,Xamarin.Forms.Forms.Context便是当前活动。
          CreateAreaInformation createAreaInformation = new CreateAreaInformation(Xamarin.Forms.Forms.Context);
			createAreaInformation.Show();
        }

效果如下:

Xamarin.Form 弹出自定义对话框_第1张图片

你可能感兴趣的:(Xamarin.Forms)