简体   繁体   English

如何在Visual C#2013中从另一个(引用的)项目访问表单组件?

[英]How to access a form component from another (referenced) Project in visual C# 2013?

I'm trying to change the text in a RichTextBox in my "Game" project from another "Engine" project in Visual Studio community 2013, which is already referenced by the "Game" project. 我正在尝试从Visual Studio社区2013中的另一个“引擎”项目更改“游戏”项目中RichTextBox中的文本,该项目已被“游戏”项目引用。 Everything I've tried to access it does not bring up the RichTextBox via Intellisense, causes a redline, and the "Engine" project refuses to build. 我尝试访问的所有内容都不会通过Intellisense弹出RichTextBox,导致出现红线,并且“ Engine”项目拒绝构建。

From checking around the site and Google, I've tried making the RichTextBox static and a few other tricks, including one I came up with myself- making a third project and referencing the other two. 通过检查站点和Google,我尝试使RichTextBox静态化,并尝试了其他一些技巧,包括我自己想到的一个技巧-制作第三个项目,并引用其他两个。 All to no avail. 一切都无济于事。

Here should be the important parts of the code I have now: 这应该是我现在拥有的代码的重要部分:

in Game.cs, I have the class public: 在Game.cs中,我有一个公共类:

namespace Game
{
    public partial class Game : Form
    {
        ...  //All the code for the form
    }
}

in Game.Designer.cs I have the RichTextBox public and static: 在Game.Designer.cs中,我有RichTextBox公共和静态的:

namespace Game
{
    partial class Game
    {
        ... // All the other unimportant stuff
        public static System.Windows.Forms.RichTextBox rtbMessages; //I've fixed the errors this caused in InitializeComponent() already
    }
}

Now from my "Engine" project, I'll try something like this (simplified but it shows what I'm trying to do): 现在,从我的“引擎”项目中,我将尝试以下操作(简化后的步骤,但它显示了我要执行的操作):

using Game;

nameSpace Engine
{
    public class Monster
    {
        ... // Properties, Constructor, etc.

        public static bool IsDead(string name, int currentHP)
            if (currentHP <= 0)
            {
                Game.rtbMessages.Text += String.Format("You have killed {0}.{1}", name, Environment.NewLine);
                return true;
            }

            return false;
        }
    }
}

I've only been learning C# and Object-Oriented programming for 3 weeks now using basic tutorials I've been able to find online, but from what I've learned I honestly don't know why this won't work. 我现在只能使用我在网上可以找到的基本教程来学习C#和面向对象的编程,已有3周的时间,但是从我学到的知识来看,老实说我不知道​​为什么这行不通。 I tried adding a reference from "Engine" back to "Game", but I get an error saying this would create a circular dependency since "Game" already references "Engine". 我尝试将“引擎”中的引用添加回“游戏”中,但是由于“游戏”已经引用了“引擎”,因此我收到一条错误消息,称这将创建循环依赖项。

Is this even possible? 这有可能吗? I'm starting to wonder. 我开始怀疑。

Edit: I made the mistake of assuming "Project" was just the name VS used for namespace. 编辑:我错误地认为“项目”只是用于名称空间的名称VS。 The problem isn't that they're in different namespaces, but different projects. 问题不在于它们位于不同的名称空间,而是不同的项目。 Hence the strikeouts. 因此三振出局。

Dependancies (project references) should only be one-way. 依赖关系(项目参考)只能是单向的。 In fact VS tries very hard to make it impossible for you to create circular references, as you have found out. 实际上,正如您所发现的,VS非常努力地使您无法创建循环引用。

Typically, a "Game" project will reference the "Engine" project. 通常,“游戏”项目将引用“引擎”项目。 Not the other way around. 并非相反。 An engine should never need to know about types in the game project. 引擎永远不需要了解游戏项目中的类型。

If the engine does need to know about a game type then something is wrong with the architecture. 如果引擎确实需要了解游戏类型,那么架构有问题。 Either the game type should actually be an engine type, or the functionality that you are coding into the engine is game-specific and should be in the game project. 游戏类型实际上应该是引擎类型,或者您要编码到引擎中的功能是特定于游戏的,并且应该在游戏项目中。

In your case the functionality (updating the rtbMessages control) belongs in the game project. 在您的情况下,功能(更新rtbMessages控件)属于游戏项目。 So from the game code you should do something like this: 因此,从游戏代码中,您应该执行以下操作:

if (Engine.Monster.IsDead("monster name", monster_hp)) {
    Game.rtbMessages.Text += String.Format("You have killed {0}.{1}", name, Environment.NewLine);
}

and remove the rtbMessages update from the Monster.IsDead function. 并从Monster.IsDead函数中删除rtbMessages更新。

Additionally I would consider making another change. 另外,我会考虑进行其他更改。
Do you really need Monster.IsDead to be static? 您真的需要Monster.IsDead保持静态吗? Presumably you have a reference to the monster already. 大概您已经参考了怪物。 A non-static IsDead method on the Engine.Monster type might make more sense. Engine.Monster类型上的非静态IsDead方法可能更有意义。 You could even make it a read-only property of the Engine.Monster class. 您甚至可以将其Engine.Monster类的只读属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从C#中的其他表单访问可视组件 - How to access a visual component from another form in c# 使用C#从Visual Studio中的其他表单访问变量 - Access variable from another form in Visual Studio with c# 从另一个线程c#访问主要形式的文本框组件 - access to textbox component in main form from another thread c# 我如何从C#中同一项目下的不同Windows窗体访问不同的组件? - How can i access the different component from different windows form which are under same project in c#? 如何从Visual C#中的其他项目插入表格 - how insert form from project other in visual c# 如何从Visual Studio 2013 C#项目的安装程序中获取变量? - How can I obtain a variable from the installer of a visual studio 2013 c# project? 从Windows Form内打开C#Word 2013项目 - Opening c# word 2013 project from inside Windows Form 如何使用C#中另一个命名空间的对象/类(Visual Studio 2013) - How can I use objects/classes from another namespace in C# (visual studio 2013) 如何从Visual Studio 2013 Ultimate中的另一个项目导入类 - How to import a class from another project in Visual Studio 2013 Ultimate C#访问另一个项目中的文件 - C# access files from another project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM