其他分享
首页 > 其他分享> > Xamarin.Mac 演练教程中 ClickedButton 事件处理函数不生效的 bug

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");
        }

标签:事件处理,ClickedButton,partial,Xamarin,代码,教程,numberOfTimesClicked,clicked
来源: https://www.cnblogs.com/xiaqiuchu/p/16220101.html