Xamarin.Mac 演练教程中 ClickedButton 事件处理函数不生效的 bug

参考

  1. 学习教程

环境

  1. mac os 12.3.1
  2. visual studio for mac 2019
  3. xcode 13.3.1
  4. dotnet 6.0.202

问题与解决办法

  1. 根据官网教程学习将代码复制到文件中
    下一步,添加代码以对用户点击按钮作出响应。 将下面的分部方法添加到 ViewController 类:
    partial void ClickedButton (Foundation.NSObject sender) {
    	// Update counter and label
    	ClickedLabel.StringValue = string.Format("The button has been clicked {0} time{1}.",++numberOfTimesClicked, (numberOfTimesClicked < 2) ? "" : "s");
    }
    
    此代码会附加到在 Xcode 和 Interface Builder 中创建的操作,且每次用户点击按钮时都会调用此代码。
  2. 代码提示错误
     No defining declaration found for implementing declaration of partial method 'ViewController.ClickedButton(NSObject)
    
  3. 解决办法将官网代码替换为下面的代码,出错原因就是部分类 partial 的定义需要一致,因为C#基础不好,所以会因为这个问题出错
    partial void ClickedButton(AppKit.NSButton sender)
    {
    	// Update counter and label
    	ClickedLabel.StringValue = string.Format("The button has been clicked {0} time{1}.", ++numberOfTimesClicked, (numberOfTimesClicked < 2) ? "" : "s");
    }
    
posted @ 2022-05-04 00:13  夏秋初  阅读(37)  评论(0)    收藏  举报