Xamarin表单按钮事件未在Android上触发

时间:2017-07-11 09:25:34

标签: xamarin xamarin.android xamarin.forms

我有一个Xamarin Forms应用程序,在我的一个页面中我有两个按钮,在Android上点击按钮时它不会触发事件,它只是突出显示按钮,如果你点击第二次然后它会触发事件,在iOS上它运行完美。下面是一些代码摘录,抱歉,如果我的代码有不良做法,我是菜鸟,所以请纠正我,最好的学习方法:

//初始化页面

public GameDetailsNew(Game myEvent)
    {
        InitializeComponent();
        MyEvent.Text = myEvent.EventName;
        MyTimeHeader.Text = myEvent.TimeOfEvent;
        myEventID = myEvent.EventID;
        Task.Run(() => { GetAttendingEvent(myEvent.EventID); });
    }

// GetAttendingEvent

private void GetAttendingEvent(int eventID)
    {
        var registerLable = "";
        var attendinIsVisible = false;
        var notAttendingIsVisible = false;
        try
        {
            HelperFiles.APICaller ac = new HelperFiles.APICaller();
            var x = (string)ac.CallCOFMOBILEBool("GetIsAttending?eventID=" + eventID);
            if (x == "Attending")
            {
                registerLable = "You are currently Attending this Event";
                attendinIsVisible = false;
                notAttendingIsVisible = true;
            }
            else
            {
                registerLable = "You are currently NOT Attending this Event";
                attendinIsVisible = true;
                notAttendingIsVisible = false;
            }
            Device.BeginInvokeOnMainThread(() =>
                {
                    RegisterLable.Text = registerLable;
                    AttendingButton.IsVisible = attendinIsVisible;
                    NotAttendingButton.IsVisible = notAttendingIsVisible;
                });
        }
        catch (Exception ex)
        {
            MyAlerts("Error", ex.Message, "OK");
        }
    }

//我的按钮事件处理程序

//Sets User as attending event
    void AttendingClicked(object sender, System.EventArgs e)
    {
        try
        {
            HelperFiles.APICaller ac = new HelperFiles.APICaller();
            var x = (string)ac.CallCOFMOBILEBool("GetCreateAttending?eventID=" + myEventID);
            if (x == "1")
            {
                RegisterLable.Text = "You are currently Attending this Event";
                AttendingButton.IsVisible = false;
                NotAttendingButton.IsVisible = true;
            }
            else
            {
                RegisterLable.Text = "You are currently NOT Attending this Event";
                AttendingButton.IsVisible = true;
                NotAttendingButton.IsVisible = false;
            }
            GetDeployingEvent(myEventID);

        }
        catch (Exception ex)
        {
            MyAlerts("Error", ex.Message, "OK");
        }

    }

    //Sets User as Not attending event
    void NotAttendingClicked(object sender, System.EventArgs e)
    {
        try
        {
            HelperFiles.APICaller ac = new HelperFiles.APICaller();
            var x = (string)ac.CallCOFMOBILEBool("GetCreateNotAttending?eventID=" + myEventID);
            if (x != "1")
            {
                RegisterLable.Text = "You are currently Attending this Event";
                AttendingButton.IsVisible = false;
                NotAttendingButton.IsVisible = true;
            }
            else
            {
                RegisterLable.Text = "You are currently NOT Attending this Event";
                AttendingButton.IsVisible = true;
                NotAttendingButton.IsVisible = false;
            }
            GetDeployingEvent(myEventID);

        }
        catch (Exception ex)
        {
            MyAlerts("Error", ex.Message, "OK");
        }
    }

1 个答案:

答案 0 :(得分:1)

我只是想对此进行更新,我设法解决了这个问题。大约8个月前,我在Xamarin工作室开始我的项目,快进几个月,Xamarin工作室改为Visual Studio。无论我做了什么,我无法摆脱这个错误,然后我开始一个全新的应用程序,并将我的所有代码粘贴到新的应用程序。这解决了这个问题。