区分按钮单击xamarin.forms

区分按钮单击xamarin.forms,xamarin.forms,Xamarin.forms,您好,我在xamarin.forms应用程序中有四个按钮。每次单击按钮都会在弹出窗口中打开一个listview。我尝试在每次单击按钮时打开相同的弹出页面。我使用messeging Center将listview所选项目返回到按钮页面。我被卡住的地方是如何区分弹出页面中的按钮点击?我应该使用标志还是其他东西 我的按钮页面 void Button1_Tapped(object sender, EventArgs e) { PopupNavigation

您好,我在xamarin.forms应用程序中有四个按钮。每次单击按钮都会在弹出窗口中打开一个listview。我尝试在每次单击按钮时打开相同的弹出页面。我使用messeging Center将listview所选项目返回到按钮页面。我被卡住的地方是如何区分弹出页面中的按钮点击?我应该使用标志还是其他东西

我的按钮页面

  void Button1_Tapped(object sender, EventArgs e)
        {


            PopupNavigation.PushAsync(new AnswerPopup(tranzaction));

            MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
            {
                string receivedData = value.Myvalue;
                Answer1.Text = receivedData;
            });
        }

        void Button2_Tapped(object sender, EventArgs e)
        {

            PopupNavigation.PushAsync(new AnswerPopup(tranzaction));

            MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
            {
                string receivedData = value.Myvalue;
                Answer2.Text = receivedData;
            });
        }
        void Button3_Tapped(object sender, EventArgs e)
        {

            PopupNavigation.PushAsync(new AnswerPopup(tranzaction));

            MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
            {
                string receivedData = value.Myvalue;
                Answer3.Text = receivedData;
            });
        }

首先,您不需要多次订阅,只需每页订阅一次(通常在构造函数中)

其次,在
MyMessage
中添加一个属性,该属性将告诉您调用了哪个按钮

MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
        {
            string receivedData = value.Myvalue;

            switch (value.Question) {
              case "Q1":
                Answer1.Text = receivedData;
                break;
              case "Q2":
                Answer2.Text = receivedData;
                break;
              case "Q3":
                Answer3.Text = receivedData;
                break;
            }


        });

为什么不为每个按钮发送一个唯一的键到AnswerPopup,反过来,让AnswerPopup通过MessagingCenter发回钥匙?@Jason bro你能解释一下吗?@AndroDevil你可以为所有这些按钮创建一个信息中心,并在其中接收buttonId,然后使用switch/case识别哪个按钮被点击。让我检查一下bro在呼叫MessagingCenter时如何传递钥匙。send()?您已经在发送MyMessage,只需添加一个包含问题ID的属性即可
MessagingCenter.Subscribe<MyMessage>(this, "AnsData", (value) =>
        {
            string receivedData = value.Myvalue;

            switch (value.Question) {
              case "Q1":
                Answer1.Text = receivedData;
                break;
              case "Q2":
                Answer2.Text = receivedData;
                break;
              case "Q3":
                Answer3.Text = receivedData;
                break;
            }


        });
void Button1_Tapped(object sender, EventArgs e)
    {
        // use "Q2", "Q3", etc as appropriate
        PopupNavigation.PushAsync(new AnswerPopup(tranzaction, "Q1"));
    }