gpt4 book ai didi

c# - 将 UI 对象链接在一起 : could I do it with attributes?

转载 作者:行者123 更新时间:2023-11-30 17:17:47 25 4
gpt4 key购买 nike

我目前正在为一款游戏开发自定义 UI 引擎。我有非常基本的 C# 技能(我是一名 C 程序员),但我在弄清楚如何将 UI 元素与其代表的数据链接起来时遇到了问题。这就是我的意思:

class SomeClass
{
//some stuff

//This is what I want to monitor and/or modify.
private SomeType _myVariable;
}

别处...

class UITextboxWidget
{
//methods and stuff
}

现在,我想要的是当用户在文本框中输入值更改时修改 _myVariable 元素,反之亦然。

我首先尝试通过在 MyClass 构造函数中调用一个方法并通过引用在 _myVariable 上传递一个属性来实现它,但我很快发现这在 C# 中是不可能的。

然后我查看了属性,发现您可以创建新属性:这就是我的想法。

class MyClass
{
[TextBoxAttribute(some, parameters)]
private SomeType _myVariable;
}

这将使嵌入到 UI 系统中变得非常容易,但我不太确定是否可以以这种方式使用属性。

所以,简单地说:

  1. 我可以用属性来做这件事吗?如果可以,怎么做?
  2. 如果不是,这样做的好方法是什么?

最佳答案

你想要做的是所谓的“数据绑定(bind)”。具体你的情况是Object Data Binding .点击此链接即可开始。

关于c# - 将 UI 对象链接在一起 : could I do it with attributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6237326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com