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)'
  1. 解决办法将官网代码替换为下面的代码,出错原因就是部分类 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 @   夏秋初  阅读(32)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示