简体   繁体   English

c#从DLL调用winform中的代码

[英]c# Calling back from a DLL a code in the winform

I am building many winform applications where i will be using alot of vertical menus (and since there is no control already existing, i had to build them dynamicly using code based on RadioButtons), so i thought about building a separated DLL that will take care of the creation of those menus, and use this DLL everytime i need that kind of menus, this is a peace of the code that was used to create dynamicly the menu : 我正在构建许多Winform应用程序,在这些应用程序中我将使用很多垂直菜单(并且由于不存在任何控件,因此我不得不使用基于RadioButtons的代码动态地构建它们),因此我考虑构建一个单独的DLL,这将引起注意这些菜单的创建,并且每当我需要那种菜单时都使用此DLL,这是用于动态创建菜单的代码的安全性:

//Creating a menubutton for each item :
foreach (MenuItem menuItem in _allItems)
{
var menuButton = CreateMenuBottons(menuItem);
_allMenuButtons.Add(menuButton);
}

// Dynamicly create the RadioButton and turn it into a menuButton

private RadioButton CreateMenuBottons(MenuItem item)
{
var index = _allItems.IndexOf(item);
var menuButton = new RadioButton();

menuButton.Appearance = Appearance.Button;
menuButton.AutoSize = true;
menuButton.Dock = DockStyle.Fill;
...
...
...
...
menuButton.Tag = item.Value;
menuButton.UseVisualStyleBackColor = true;

menuButton.CheckedChanged += Radio_CheckedChanged; //My problem is here !!!

return menuButton;

}

and this is how i detect which button was clicked on the menu : 这就是我检测菜单上单击哪个按钮的方式:

private void Radio_CheckedChanged(object sender, EventArgs e)
{
var selectedMenuButton = (RadioButton)sender;
if (selectedMenuButton.Checked)
{
// code that will be excuted when we
// click one menuButton (belongs to the DLL)
// How can i point back to the Exe project
// that is using this DLL !
}
}

my problem is that i will be using this DLL in multiple other projects (winforms), and the code that needs to be excecuted in each project will be diffrent from the rest, so how can i call a methode in the winform project from the DLL (without having to change the DLL and recompile each time i use it) ? 我的问题是,我将在其他多个项目(winforms)中使用此DLL,并且每个项目中需要执行的代码将与其余代码不同,因此如何从DLL中调用winform项目中的方法(无需更改DLL并在每次使用时重新编译)?

Just a delegate 只是代表

You can define a delegate specifying the parameters you will give 您可以定义一个委托,指定要提供的参数

public delegate void MenuButtonClickCallbackDelegate(TypeOfData data);
public MenuButtonClickCallbackDelegate MenuButtonClickCallback;

Then call it when you need: 然后在需要时调用它:

private void Radio_CheckedChanged(object sender, EventArgs e)
{
var selectedMenuButton = (RadioButton)sender;
if (selectedMenuButton.Checked)
{
    if(MenuButtonClickCallback != null)
        MenuButtonClickCallback(parameter);
}
}

This is how you assign a function to MenuButtonClickCallback from the exe: 这是从exe向MenuButtonClickCallback分配功能的方式:

void MenuButtonClicked(TypeOfData data)
{
}
/*
.
.
.
*/
theObject.MenuButtonClickCallback = MenuButtonClicked;

Event 事件

Or you can provide an event (if it is possible that more than one method will be called back). 或者,您可以提供一个事件(如果可能会调用多个方法)。 You have to define MenuButtonClickCallback as follow: 您必须定义MenuButtonClickCallback如下:

public event MenuButtonClickCallbackDelegate MenuButtonClickCallback;

And the assignment of a method is done like this: 方法的分配是这样完成的:

void MenuButtonClicked(TypeOfData data)
{
}
void MenuButtonClickedAnotherMethod(TypeOfData data)
{
}
/*
.
.
.
*/
theObject.MenuButtonClickCallback += MenuButtonClicked;
theObject.MenuButtonClickCallback += MenuButtonClickedAnotherMethod;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM