Xamarin.Forms 如何實(shí)現(xiàn)Toast

Toast提示框?qū)τ谌粘5拈_(kāi)發(fā)非常有用,主要用于不需要用戶確認(rèn)的提示信息顯示。

Xamarin.Forms自帶的提示框需要用戶點(diǎn)擊確定,Android很好用Toast其實(shí)也可以實(shí)現(xiàn)滴。

首先,我們要?jiǎng)?chuàng)建一個(gè)接口類(lèi)

using?System;

using?System.Threading.Tasks;

namespace?MyTest

{

????public?interface?IAppHandler

????{?

????????void?ShowToastMessage(string?strMessage,?bool?bLong?=?false);

????}

}

然后在Android下面一個(gè)DependencyService繼承類(lèi),暫時(shí)命名為AppHandlerImplementation.cs

using System;

using System.Threading.Tasks;

using Android.Content;

using MyTest.Droid.DependencyService;

[assembly: Xamarin.Forms.Dependency(typeof(AppHandlerImplementation))]

namespace MyTest.Droid.DependencyService

{

? ? public class AppHandlerImplementation : IAppHandler

? ? {

? ? ? ? public AppHandlerImplementation()

? ? ? ? {

? ? ? ? }

? ? ? ? public void ShowToastMessage(string strMessage, bool bLong = false)

? ? ? ? {

? ? ? ? ? ? Android.Widget.Toast.MakeText(Xamarin.Forms.Forms.Context, strMessage, bLong ? Android.Widget.ToastLength.Long : Android.Widget.ToastLength.Short).Show();

? ? ? ? }

? ? }

}

對(duì)于iOS,由于蘋(píng)果的設(shè)計(jì)規(guī)范,是不推薦使用Toast這種提示框,原因是會(huì)造成用戶困擾,但是也不影響過(guò)審,具體可以在iOS項(xiàng)目下添加Nugut組件Toast.iOS,項(xiàng)目地址是

https://github.com/andrius-k/Toast

同理也添加AppHandlerImplementation.cs,代碼如下

using?System;

using?System.Threading.Tasks;

using?GlobalToast;

using?MyTest.iOS.DependencyService;

[assembly:?Xamarin.Forms.Dependency(typeof(AppHandlerImplementation))]

namespace?MyTest.iOS.DependencyService

{

????public?class?AppHandlerImplementation?:?IAppHandler

????{

????????public?AppHandlerImplementation()

????????{

????????}

????????public?void?ShowToastMessage(string?strMessage,?bool?bLong?=?false)

????????{

????????????Toast.MakeToast(strMessage).SetPosition(ToastPosition.Center).SetDuration(bLong???ToastDuration.Long?:?ToastDuration.Regular).Show();

????????}

????}

}

為了方便調(diào)用,可以加兩個(gè)靜態(tài)擴(kuò)展

public?static?void?ShowToastMessage(this?Xamarin.Forms.View?view,?string?strMessage,?bool?bLong?=?false)

????????{

????????????Xamarin.Forms.DependencyService.Get<IAppHandler>().ShowToastMessage(strMessage,?bLong);

????????}


public?static?void?ShowToastMessage(this?Xamarin.Forms.ContentPage?page,?string?strMessage,?bool?bLong?=?false)

????????{

????????????Xamarin.Forms.DependencyService.Get<IAppHandler>().ShowToastMessage(strMessage,?bLong);

????????}

這樣子在ContentPage和ContentView里面執(zhí)行this.ShowToastMessage("提示框內(nèi)容");即可

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,899評(píng)論 2 45
  • afinalAfinal是一個(gè)android的ioc,orm框架 https://github.com/yangf...
    wgl0419閱讀 6,602評(píng)論 1 9
  • 論學(xué)習(xí)的重要性,你想要專業(yè),想要學(xué)精,想要更通透,就需要不斷的學(xué)習(xí),學(xué)習(xí)啥都一樣,需要時(shí)間學(xué)習(xí),這種學(xué)習(xí)需要一萬(wàn)個(gè)...
    絲雨媽媽閱讀 448評(píng)論 0 2
  • 恰似春臨而無(wú)花,星夜無(wú)月卷沉沙。 云邊尋得一線牽,是謂滄桑兩處發(fā)。 年輪似轍不回還,誰(shuí)擺錦旗待余南? 路影霓裳空覺(jué)...
    黍安閱讀 159評(píng)論 2 2
  • 一捧鮮花不僅能愉悅心情 更是漫長(zhǎng)而平淡生活的一束光 它不止存在于節(jié)日里 更是日常生活儀式感的體現(xiàn) 愿生活日日有鮮花...
    一米空白閱讀 5,407評(píng)論 0 2

友情鏈接更多精彩內(nèi)容