c# - Toast 通知不起作用

标签 c# android xamarin

我写了一些代码,检查互联网连接的可用性,但它不起作用。

我没有看到任何 toast 通知。我在 list 文件中写了所有权限。

这段代码有什么问题?

namespace MurakamiKiev

[Activity(Label = "Murakami",MainLauncher = true,Icon = "@drawable/logo", Theme = "@android:style/Theme.Black.NoTitleBar", ScreenOrientation = ScreenOrientation.Portrait)]
public class MainActivity : Activity
{
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);
        ImageButton next = FindViewById<ImageButton> (Resource.Id.nextButton);
        ImageButton previous = FindViewById<ImageButton> (Resource.Id.previousButton);
        ImageButton home = FindViewById<ImageButton> (Resource.Id.homeButton);
        ImageButton cart = FindViewById<ImageButton> (Resource.Id.cartButton);
        Button sushi = FindViewById<Button> (Resource.Id.sushiButton);
        Button sets = FindViewById<Button> (Resource.Id.setsbutton);
        //Button rolli = FindViewById<Button>(Resource.Id.rollibutton);
        ImageButton menu = FindViewById<ImageButton> (Resource.Id.menuButton);

        //Otslezivaem click po knopke 'next' i perehodim dalshe
        /*rolli.Click += delegate
        {
            var intent139 = new Intent(this, typeof(RolliActivity));
            StartActivity(intent139);

        };*/
        sets.Click += delegate {
            var intent121 = new Intent (this, typeof(SetsActivity));
            StartActivity (intent121);

        };
        sushi.Click += delegate {
            var intent24 = new Intent (this, typeof(SushiActivity));
            StartActivity (intent24);

        };
        next.Click += delegate {
            var intent = new Intent (this, typeof(MenuActivity));
            StartActivity (intent);
        };
        //Otlezivaem click po knopke 'Korzina' i perehodim v nee
        cart.Click += delegate {
            var intent2 = new Intent (this, typeof(CartActivity));
            StartActivity (intent2);
        };
        menu.Click += delegate {
            var intent39 = new Intent (this, typeof(MenuTopActivity));
            StartActivity (intent39);

        };
    }

    public void CheckNetwork()
    {
        var connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);

        var activeConnection = connectivityManager.ActiveNetworkInfo;

        if ((activeConnection != null) && activeConnection.IsConnected)
        {
            Toast.MakeText (this, "OK", ToastLength.Short).Show ();
        } 

        else 
        {
            Toast.MakeText (this, "Connect to the wi-fi", ToastLength.Short).Show ();
        }       
    }
}

最佳答案

What's wrong in this code?

您没有在任何地方调用您的方法,因此它永远不会向您显示 Toast,因为该方法从不被调用。

How to solve it?

例如,如果您想在启动 Activity 时检查连接,只需按如下方式添加此方法:

 protected override void OnCreate (Bundle bundle)
{
    base.OnCreate (bundle);

    // Set our view from the "main" layout resource
    SetContentView (Resource.Layout.Main);
    ImageButton next = FindViewById<ImageButton> (Resource.Id.nextButton);
    ImageButton previous = FindViewById<ImageButton> (Resource.Id.previousButton);
    ImageButton home = FindViewById<ImageButton> (Resource.Id.homeButton);
    ImageButton cart = FindViewById<ImageButton> (Resource.Id.cartButton);
    Button sushi = FindViewById<Button> (Resource.Id.sushiButton);
    Button sets = FindViewById<Button> (Resource.Id.setsbutton);
    //Button rolli = FindViewById<Button>(Resource.Id.rollibutton);
    ImageButton menu = FindViewById<ImageButton> (Resource.Id.menuButton);


    CheckNetwork(); //Here you'll ask if you have connection or not and Toast will show

关于c# - Toast 通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32668785/

相关文章:

c# - 首先是ASP.NET mvc数据库教程

java - 刷新 fragment setText 属性

ios - Xamarin.Forms iOS UIKit.UIImage 错误

plugins - 在 Xamarin Forms 项目中发送电子邮件

c# - 委托(delegate)们,为什么?

c# - 在 Ninject 中拦截实例的创建

java - AsyncTask 接口(interface)中的 NullPointerException

c# - 如何在 Xamarin.Forms 的 NavigationPage.SetTitleView 中调用 XAML 代码?

c# - 使用C#更新多个sql表中的相同列

android - umano AndroidSlidingUpPanel Resize 主要内容