登录
首页 >  Golang >  Go问答

修复"go"中的错误"类型*SimpleCard没有super字段或方法"问题的方法

来源:stackoverflow

时间:2024-02-05 21:45:27 479浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《修复"go"中的错误"类型*SimpleCard没有super字段或方法"问题的方法 》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我正在尝试扩展 fyne 小部件以具有带背景的简单可点击内容。 我搜索了 fyne widgets 来找到一个可以用作入门的示例,并在 list/listitem 中找到了类似的内容。

我基本上复制了列表项代码并对其进行了一些修改。 它看起来确实类似于 fyne 文档中的更简单的示例。 但由于某种未知的原因,go 给了我一个错误,我不知道原因是什么或如何修复它:

custom_widget/simple_card.go:80:24: c.card.super undefined (type *simplecard has no field or method super)

这是模块的代码(custom_widget/simple_card.go):

package custom_widget

import (
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/theme"
    "fyne.io/fyne/v2/widget"
    "log"
)

// Declare conformity with interfaces.
var _ fyne.Widget = (*SimpleCard)(nil)
var _ fyne.Tappable = (*SimpleCard)(nil)

type SimpleCard struct {
    widget.BaseWidget

    onTapped   func()
    background *canvas.Rectangle
    content    fyne.CanvasObject
    selected   bool
}

func NewSimpleCard(content fyne.CanvasObject, tapped func()) *SimpleCard {
    card := &SimpleCard{onTapped: tapped, content: content}
    card.ExtendBaseWidget(card)
    return card
}

// CreateRenderer is a private method to Fyne which links this custom_widget to its renderer.
func (c *SimpleCard) CreateRenderer() fyne.WidgetRenderer {
    c.ExtendBaseWidget(c)

    c.background = canvas.NewRectangle(theme.SelectionColor())
    c.background.Hide()

    objects := []fyne.CanvasObject{c.background, c.content}

    // NewBaseRenderer and BaseRenderer are copied from
    // https://github.com/fyne-io/fyne/blob/master/internal/widget/base_renderer.go
    // because the functionality is marked internal in fyne !?
    return &SimpleCardRenderer{NewBaseRenderer(objects), c}
}

func (c *SimpleCard) Tapped(_ *fyne.PointEvent) {
    log.Println("I have been tapped")
    if c.onTapped != nil {
        c.selected = true
        c.Refresh()
        c.onTapped()
    }
}

// Declare conformity with the WidgetRenderer interface.
var _ fyne.WidgetRenderer = (*SimpleCardRenderer)(nil)

type SimpleCardRenderer struct {
    BaseRenderer

    card *SimpleCard
}

// MinSize calculates the minimum size of a SimpleCardRenderer.
// This is based on the size of the status indicator and the size of the child object.
func (c *SimpleCardRenderer) MinSize() fyne.Size {
    return c.card.content.MinSize()
}

// Layout the components of the SimpleCardRenderer custom_widget.
func (c *SimpleCardRenderer) Layout(size fyne.Size) {
    c.card.background.Resize(size)
    c.card.content.Resize(size)
}

func (c *SimpleCardRenderer) Refresh() {
    if c.card.selected {
        c.card.background.FillColor = theme.SelectionColor()
        c.card.background.Show()
    } else {
        c.card.background.Hide()
    }
    c.card.background.Refresh()
    canvas.Refresh(c.card.super()) // compiler error !
}

正确答案


删除您创建的所有渲染器类型,并在 CreateRenderer 中仅返回 widget.NewSimpleRenderer(container .NewMax(c.background, c.content))。它比你想象的要简单。

从主小部件中复制代码通常不是最好的方法,因为我们有快捷方式和/或必须支持比您自己的小部件更多的功能。

到这里,我们也就讲完了《修复"go"中的错误"类型*SimpleCard没有super字段或方法"问题的方法 》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>