Xamarin-点击按钮时不断获得NSInvalidArgumentException

本文关键字:NSInvalidArgumentException 按钮 Xamarin- | 更新日期: 2023-09-27 17:57:27

我目前正在使用Xamarin(在C#中也是如此)通过Visual Studio进行一些跨平台的移动开发,并且即将启动iOS部分。我以前从未做过iOS开发,我想我可以熟悉他们的"你好,iOS"教程。不幸的是,事情进展得并不顺利。我不断从我的TouchUpInside操作中获得NSInvalidArgumentExceptions:

Foundation.MonoTouchException: Objective-C exception thrown.
Name: NSInvalidArgumentException Reason:
-[ViewController TranslateButton_TouchUpInside:]:
unrecognized selector sent to instance 0x7b6200d0

我偶尔可以通过重新制作按钮来补救一下,但之后它几乎就坏了。实际错误本身发生在我的Main.cs文件中:

using UIKit;
namespace CheckinIOS
{
    public class Application
    {
        static void Main(string[] args)
        {
            UIApplication.Main(args, null, "AppDelegate"); //this line is where it breaks
        }
    }
}

如果它有任何帮助,我正试图部署到运行iOS 9.3的iPhone 5S模拟器上(但它在iPhone 6模拟器上也坏了)。如果需要的话,我也可以发布更多的代码,但我复制了Xamarin教程中的所有C#,并为Main.storyboard做了同样的事情。

我花了一段时间寻找和我有同样问题的人,但他们的解决方案要么不起作用,要么是因为略有不同的原因出错。如有任何协助,我们将不胜感激。

编辑:这是我对TranslateButton_TouchUpInside:的实现

    TranslateButton.TouchUpInside += (object sender, EventArgs e) =>
        {
            // Convert the phone number with text to a number
            // using PhoneTranslator.cs
            translatedNumber = PhoneTranslator.ToNumber(PhoneNumberText.Text);
            // Dismiss the keyboard if text field was tapped
            PhoneNumberText.ResignFirstResponder();
            if (translatedNumber == "")
            {
                CallButton.SetTitle("Call", UIControlState.Normal);
                CallButton.Enabled = false;
            }
            else
            {
                CallButton.SetTitle("Call " + translatedNumber, UIControlState.Normal);
                CallButton.Enabled = true;
            }
        };

Xamarin-点击按钮时不断获得NSInvalidArgumentException

iOS运行时正在ViewController类中寻找一个名为(在Obj-C中)TranslateButton_TouchUpInside:的方法。但是,没有使用该名称导出到Obj-C的方法。第一种猜测是,您在故事板中的按钮上添加了一个可能具有该名称的事件,但您要么删除了该方法,要么从未实现它

尝试在iOS Designer中打开情节提要,并在画布上选择按钮后从"属性"->"事件"选项卡中删除任何事件。此外,我假设在画布上选择按钮时,您的按钮在Properties->Widget窗格中的名称为TranslateButton

在Xamarin iOS中,有几种方法可以将事件附加到控件。一种也是首选的方法是在iOS Designer中为控件创建一个事件。如果执行此操作,.designer.cs文件中会有一个部分方法存根,该存根具有将方法名称导出到Obj-C运行时的Export属性。然后,您需要在ViewController的主.cs文件中使用相同的签名(不带导出属性)来实现此方法。在Obj-C领域,这被称为action

另一种方法是按照代码片段中显示的方式进行操作。在这种情况下,您只需要在Properties->Widget窗格中为控件指定一个名称,然后可以在代码中使用该名称来订阅TouchUpInside事件。在Obj-C领域,这被称为outlet

我的猜测是,您同时执行了这两项操作,但从未在ViewController中实现TranslateButton_TouchUpInside:方法。请注意,这是向控件添加事件时在.designer.cs文件中创建的方法存根的Export属性中使用的Obj-C名称。

但是很难说没有看到故事板以及主ViewController.cs文件和ViewController.designer.cs文件

相关文章:
  • 没有找到相关文章