vs开发android页面跳转,C#-Xamarin的Android项目开发(一)——创建项目

[Activity(Label = "KibaXamarin_Android" )]

public class BaseActivity : Activity

{

public void ShowActivity() where T : Activity

{

Intent intent = new Intent(this, typeof(T));

StartActivity(intent);

}

public void OpenService() where T : Service

{

Intent intent = new Intent(this, typeof(T));

StartService(intent);

}

#region 各种提示信息

public void ShowToast(string msg)

{

Toast.MakeText(this, msg, ToastLength.Short).Show();

}

private AlertDialog dialog;

public AlertDialog InitDialog(string msg, Action comfirmCallback, Action cancelCallback)

{

AlertDialog cdialog;

//构造器

AlertDialog.Builder builder = new AlertDialog.Builder(this);

//标题

builder.SetTitle( "提示" );

//图标

//builder.SetIcon(android.R.drawable.btn_dialog);

//内容

builder.SetMessage(msg);

//setPositiveButton(表示按钮的文本,表示单击按钮触发单击事件)

builder.SetPositiveButton( "OK" , new EventHandler((s, e) =>

{

if (comfirmCallback != null )

{

comfirmCallback(dialog);

}

}));

builder.SetNegativeButton( "Cancel" , new EventHandler((s, e) =>

{

if (cancelCallback != null )

{

cancelCallback(dialog);

}

}));

//builder.SetNeutralButton( "稍后提醒" , new EventHandler((s, e) => { }));

cdialog = builder. Create ();//构建dialog对象

return cdialog;

}

public void ShowAlert(string msg, Action comfirmCallback = null , Action cancelCallback = null )

{

if (comfirmCallback == null )

{

cancelCallback = (d) => { dialog.Dismiss(); };

}

if (cancelCallback == null )

{

cancelCallback = (d) => { dialog.Dismiss(); };

}

dialog = InitDialog(msg, comfirmCallback, cancelCallback);

if (dialog != null && !dialog.IsShowing)

{

dialog.Show();

}

}

public void NotifyMessage(string message, string title = "消息" )

{

NotificationManager manager = (NotificationManager)GetSystemService(Context.NotificationService); // 在Android进行通知处理,首先需要重系统哪里获得通知管理器NotificationManager,它是一个系统Service。

PendingIntent pendingIntent = PendingIntent.GetActivity(this, 0,

new Intent(this, typeof(MainActivity)), 0);

Notification notify1 = new Notification();

notify1.Icon = Resource.Drawable.logo;

notify1.TickerText = JaveString( "您有新短消息,请注意查收!" );

notify1. When = DateTime.Now.ToFileTime();

notify1.SetLatestEventInfo(this, title, message, pendingIntent);

notify1.Number = 1;

notify1.Flags |= NotificationFlags.AutoCancel; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。

// 通过通知管理器来发起通知。如果id不同,则每click,在statu那里增加一个提示

manager.Notify(1, notify1);

}

public static Java.Lang.String JaveString(string str)

{

return new Java.Lang.String( "您有新短消息,请注意查收!" );

}

#endregion

#region 寻找资源

public T FindControl(string name ) where T : View

{

return FindViewById(GetCode( name ));

}

public T FindControl(string name , Action callBack) where T : View

{

View view = FindViewById(GetCode( name ));

view .Click += (s, e) =>

{

callBack();

};

return FindViewById(GetCode( name ));

}

public int GetCode(string name )

{

var R = this.Resources;

var code = (typeof(Resource.Id)).GetFields().FirstOrDefault(f => f. Name == name ).GetValue(R);

return ( int )code;

}

#endregion

#region 异步调用

public void AsyncLoad( Action action )

{

IAsyncResult result = action .BeginInvoke((iar) =>

{

}, null );

}

public void AsyncLoad( Action action , Action callback)

{

IAsyncResult result = action .BeginInvoke((iar) =>

{

this.RunOnUiThread(callback);

}, null );

}

public void AsyncLoad( Action action , T para, Action callback)

{

IAsyncResult result = action .BeginInvoke(para, (iar) =>

{

this.RunOnUiThread(callback);

}, null );

}

public void RunOnUi( Action action )

{

((BaseActivity)this).RunOnUiThread(() => { action (); });//回UI线程

}

#endregion

#region 获取数据

public void GetRest(string url, Action callback)

{

Task.Run(() =>

{

try

{

HttpWebRequest request = (HttpWebRequest)HttpWebRequest. Create (new Uri(url));

request.ContentType = "application/json" ;

request.Method = "GET" ;

using (WebResponse response = request.GetResponse())

{

using (Stream stream = response.GetResponseStream())

{

JsonValue jsonDoc = JsonObject. Load (stream);

callback(jsonDoc);

}

}

}

catch (Exception ex)

{

Log.Debug( "BaseActivity" , $ "Exception at GetRest" + ex.Message);

}

});

}

#endregion

}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值