在 WinForms 应用程序中利用预先编写的视图代码

发布于 2024-10-29 08:15:26 字数 247 浏览 2 评论 0原文

我的工作涉及用 C#.NET 编写相对较小的 WinForms 应用程序。有时,我收到的项目与我已经完成的项目略有不同。我已经能够通过将域逻辑抽象到自己的程序集中来利用业务逻辑代码进行重用,并在新项目中简单地引用它。

然而,我不得不重写很多视图代码(例如表单)。我过去曾尝试对 Form 对象使用继承,但即使它有效,它对设计者来说并不是一个好兆头,并且还需要对项目文件进行一些修改才能以正确的顺序进行编译。是否有一种简单的方法来利用我的视图代码在新项目中重复使用?

My job involves writing relatively small WinForms applications in C#.NET. Sometimes I am given projects which are slight variants on projects that I have already completed. I've been able to leverage business logic code for re-use by abstracting the domain logic into its own assembly, and simply reference it in newer projects.

However, I've had to rewrite a lot of the view code (e.g. Forms). I've tried in the past to use inheritance with Form objects, but even though it worked it did not bode well with the designer, and also required some hacking to the project file in order to compile in the correct order. Is there an easy way to leverage my view code to be re-used in new projects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不奢求什么 2024-11-05 08:15:26

我在尝试在 winforms 项目中构建真正的可重用 UI 代码时也遇到了麻烦。

然后我设法通过使用 UserControls 或多或少地实现了这个目标,但有时很麻烦,因为当它的目标是在布局非常不同的不同项目中重用时,通常需要对大量属性/方法进行子类化。

这就是为什么我使用 Helper 类来设置一些控件属性,以这种方式在参数中传递控件。例如:

void SetupEditableGrid(UltraGrid myGrid)
{
    myGrid.BackColor...
}

此外,对于需要更多工作的最复杂的对象,我使用与某些特定控件关联的控制器类。例如,我使用 MyEditableUltraGridController 来准备原始 Infragistic 的 UltraGrid,使其完全可编辑。

public class MyEditableUltraGridController()
{
    UltraGrid _myGrid;

    public MyEditableUltraGridController(UltraGrid myGrid)
    {
        _myGrid = myGrid;
        _myGrid.InitializeLayout += ... // some common initialization code
        _myGrid.KeyPressed += ... // some keystroke handling code
        ... etc ...
    }

    void InitializeLayout(object sender, EventArgs e)
    {
        ... some specific UltraGrid common initialization code
    }

    ... // some code that make my UltraGrid editable, etc...
}

然后,考虑到我的窗体上现有的 UltraGrid1 控件,我会:

Form_Load(object sender, EventArgs e)
{
    var MyEditableUltraGridController = 
        new MyEditableUltraGridController(UltraGrid1);
}

我对这种基于类的方法感到满意,因为它允许我以舒适的方式保持控件行为并具有良好的灵活性。

I had troubles too to try to build true reusable pieces of UI code in my winforms projects.

Then I managed to achieve this goal more or less by using UserControls, but it is cumbersome sometimes because it often requires a lot of properties / method to be subclassed when it is aimed to be reused in different projects with a very different layout.

That's why I use Helper classes which set some controls properties, passing the control in parameter this way. For example:

void SetupEditableGrid(UltraGrid myGrid)
{
    myGrid.BackColor...
}

Also, for the most complex objects that requires more work, I am using controller classes associated with some specific controls. For example, I use a MyEditableUltraGridController to prepare a raw Infragistic's UltraGrid to be fully editable.

public class MyEditableUltraGridController()
{
    UltraGrid _myGrid;

    public MyEditableUltraGridController(UltraGrid myGrid)
    {
        _myGrid = myGrid;
        _myGrid.InitializeLayout += ... // some common initialization code
        _myGrid.KeyPressed += ... // some keystroke handling code
        ... etc ...
    }

    void InitializeLayout(object sender, EventArgs e)
    {
        ... some specific UltraGrid common initialization code
    }

    ... // some code that make my UltraGrid editable, etc...
}

Then, considering an existing UltraGrid1 control on my form, I will have :

Form_Load(object sender, EventArgs e)
{
    var MyEditableUltraGridController = 
        new MyEditableUltraGridController(UltraGrid1);
}

I feel confortable with this class based approach, because it allows me to maintain control behaviour a confortable way with a good flexibility.

许久 2024-11-05 08:15:26

有没有一种简单的方法可以利用我的视图代码在新项目中重复使用?

我不知道您所说的“简单方法”是什么意思,也不知道您的确切用例场景,但您可以将视图逻辑封装到 可重用的 WinForms 控件 放入单独的程序集中。这些控件可能包含不同的属性,允许针对每个项目对其进行个性化设置。

这正是像 TelerikDevExpress 所做的只是您可以更进一步,甚至合并一些可以重用的业务逻辑。

Is there an easy way to leverage my view code to be re-used in new projects?

I don't know what you mean by easy way nor I know your exact use case scenario but you can encapsulate view logic into reusable WinForms controls into a separate assembly. Those controls might contain different properties allowing to personalize them for each project.

That's exactly what companies like Telerik and DevExpress do except that you can get a step further and even incorporate some business logic that could be reused.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文