修改Windows从另一个项目窗体控件 [英] Modify Windows Forms Control from another Project

查看:89
本文介绍了修改Windows从另一个项目窗体控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个控制 lblDate 在用户控件 MainScreen 。我想修改它的类日期的方法,这是另一个项目 AoWLibrary 。我不能引用它,因为AoWLibrary是从属的第一个项目。



我试图让lblDate静态的,而是编译器保持在我抛出的错误,我有公共财产的日期似乎访问不能:

 公共标签LabelDate 
{
{返回lblDate; }
集合{lblDate =价值; }
}

在类Date,我需要的公共方法CalculateDate修改的Text属性lblDate

 公共静态无效CalculateDate()
{
GameDate = month.ToString()+/ + displayDay.ToString()+/+ year.ToString();

// LabelDate.Text = GameDate;
//实际上,这就是我需要做的
}



此外,我甚至不能访问lblDate在其他控件在同一个项目。


解决方案

所以,一般的想法是,你需要跟踪的是什么范围。有很多方法去这样做你想做的事,但它们中的任何依靠引用对方正确的方式你可能不太了解完全没有对象。请放心,每个人都有在某个时候通过这个工作。



当你试图让该成员的静态是因为标签本身是编译器抱怨的原因不是一成不变的。被捆绑到MainScreen的一个实例,它具有在静态上下文,这是通过定义没有任何固有参考特定MainForm的一个范围没有任何意义。



您日期类,当然,一个完全不同的情况下,也是如此,但它的非静态成员做访问日期的实例。没有,直到你创建这两个上下文之间的路径。静态路由实际上会工作(查找Singleton模式:这个想法是,你存储类的第一个也是唯一实例,静态和从其他地方引用它),但可能不是好的设计。其他的方法来做到这一点是通过传递到日期MainScreen的一个实例(最好是通过一个接口),甚至LabelDate本身。所有这些都可以得到你需要一起来讨论两部分。



不过,我想建议你想想被存储你的游戏状态中。它是在图书馆或消费组装?如果是前者,你可能要考虑有关创建需要存储和在库类操纵数据的完整GUIless模式,只消耗了现成的数据,是在主应用程序。另一种方法是,你的模型住在主应用程序,而只是调用回你的图书馆是必要的。在这种情况下,你可能应该坚持现在要问的答案库(或最多监听库中的事件),而不是期待它达到回它自己(无论以获取或提供信息)。

在搞清楚如何做到这一点,你可能会发现你的范围管理问题更容易处理。即使是好的建筑一点点可以走很长的路。



但如果这是都有点喧宾夺主,不要强调太多。这听起来像你这样做是为了好玩,这是一个学习的好方法。你会碰到这样的问题,要么打一个好的设计或不那么好,但你与重要概​​念摔跤和任何一种方式会更好地离开它。


I have a Control lblDate in User Control MainScreen. I would like to modify it in a method in class Date, which is in another project AoWLibrary. I can't reference it because AoWLibrary is a dependent of the first project.

I tried to make lblDate static but the compiler kept throwing errors at me, and I have a public property that Date can't seem to access:

    public Label LabelDate
    {
        get { return lblDate; }
        set { lblDate = value; }
    }

In class Date, I need public method CalculateDate to modify the Text property of lblDate

    public static void CalculateDate()
    {
        GameDate = month.ToString() + "/" + displayDay.ToString() + "/" + year.ToString();

    // LabelDate.Text = GameDate;
    // This is essentially what I need to do
    }

Also, I can't even access lblDate in other controls in the same project.

解决方案

So, the general idea is that you need to keep track of what is in scope. There are a lot of ways to go about doing what you want to do, but any of them rely on objects referencing each other properly in ways you may not quite understand fully yet. Don't worry-- everyone has to work through this at some point.

The reason the compiler complained when you tried to make that member static is because the label itself is not static. Being tied to a single instance of MainScreen, it has no meaning in a static context, which is by definition a scope without any inherent reference to a particular MainForm.

Your Date class is, of course, a totally different context, too, but its non-static members do have access to an instance of Date. There isn't any path between these two contexts until you create it. The static route actually would have worked (look up the Singleton pattern: the idea being that you store the first and only instance of a class statically and refer to it from elsewhere) but is probably not good design. Other ways to do this are by passing to Date an instance of MainScreen (preferably through an interface) or even LabelDate itself. All of these could get the two pieces you need together to talk.

However, I would like to suggest you think about where your game state is being stored. Is it in the library or the consuming assembly? If the former, you might want to think about creating a complete GUIless model of the data that needs to be stored and manipulated in the library class and only consume that ready-made data as-is in the main application. The other alternative is that your model lives in the main application, and just calls back to your library as necessary. In this case you should probably stick for now to asking the library for answers (or at most listening for events in the library) and NOT expecting it to reach back to the application on its own (whether to get or provide information).

In figuring out how to do this you will probably find your scope management issues are much easier to handle. Even a little bit of good architecture can go a long way.

But if this is all a little overwhelming, don't stress it too much. It sounds like you are doing this for fun, which is a great way to learn. You'll come up against issues like this and will either hit on a good design or a not-so-good one, but either way you're wrestling with important concepts and will come away better for it.

这篇关于修改Windows从另一个项目窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆