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

2022/5/4 6:22:33

本文主要是介绍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");
        }


这篇关于Xamarin.Mac 演练教程中 ClickedButton 事件处理函数不生效的 bug的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程