简体   繁体   English

在dll库中显示消息框

[英]Display messagebox in dll library

I am writing a dll library (for example for checking login details of a user) and I want to pop up a confirmation dialog or a informational dialog in the process, for example 我正在编写一个dll库(例如用于检查用户的登录详细信息),并且想在此过程中弹出一个确认对话框或信息对话框。

"Are you sure you want to continue? (Yes/No/Cancel)"

for some reason. 由于某些原因。 At present I am need this for an application in Winforms and I want to use same dll for my other webforms applications also. 目前,我需要Winforms中的应用程序使用它,并且我也想对其他Webforms应用程序使用相同的dll。 Can anyone guide how to achieve it? 任何人都可以指导如何实现它吗? Need less to say I don't want to reference System.Windows.Forms or System.Web . 无需多说,我不想引用System.Windows.FormsSystem.Web Can any one give me some pointers? 谁能给我一些指导? I am using .net 4.0 我正在使用.net 4.0

I don't want to for Publication/Subscription of events as I am not very familiar with cab architecture. 我不想进行事件的发布/订阅,因为我对出租车的体系结构不是很熟悉。

EDIT 编辑

I have a dll for creating database at runtime of an application. 我有一个用于在应用程序运行时创建数据库的dll。 So my dll generally checks if the database already exists and is upto date or not, if it is then stop. 因此,我的dll通常检查数据库是否已经存在并且是否为最新数据库,然后停止。 But if it does not exist or not upto date, then I want to ask user if he want to update it now or afterwards. 但是,如果它不存在或不是最新的,那么我想问用户是现在还是之后进行更新。 If user says now, update it and says afterwards continue to application. 如果用户现在说,请对其进行更新,然后再继续应用。 It is just an example and any relevant information on this is welcome. 这只是一个例子,欢迎您提供任何相关信息。

Thanks. 谢谢。

You really shouldn't. 你真的不应该。 This does not belong in a library, but should be done in the application that uses this library instead. 这不属于库,但应在使用该库的应用程序中完成。

Where a WinForms application might solve it by showing a MessageBox , a web application might perform the request without asking after a successful POST (since a POST usually shows the intent to modify a resource). WinForms应用程序可以通过显示MessageBox来解决该问题,而Web应用程序可以执行请求而无需询问成功的POST(因为POST通常显示修改资源的意图)。

When for example your library is used for logging in, simply throw a AuthenticationException , so the client application (whether it's WinForms, web, or whatever) can catch that and display the appropriate message. 例如,当您的库用于登录时,只需抛出AuthenticationException ,以便客户端应用程序(无论是WinForms,Web还是其他)都可以捕获并显示适当的消息。


As for your edit: 至于您的编辑:

I have a dll for creating database at runtime of an application ... 我有一个用于在应用程序运行时创建数据库的dll ...

Why not expose two methods like IsDatabaseUpToDate() and UpdateDatabase() ? 为什么不公开IsDatabaseUpToDate()UpdateDatabase()两个方法? Then in the client, you can call the first method. 然后在客户端中,可以调用第一个方法。 If it returns false, you ask the user (be it with a MessageBox, an HTML form, a JavaScript Alert, a simple Yes button or a blinking tile) whether they want to update the database. 如果返回false,则询问用户(使用MessageBox,HTML表单,JavaScript Alert,简单的Yes按钮还是闪烁的图块)是否要更新数据库。 If so, you call the UpdateDatabase() method and you're ready to go. 如果是这样,则调用UpdateDatabase()方法,就可以开始了。

To accomplish this you need to right click on the project and select "Add Reference", then select the ".NET" tab and click "System.Windows.Forms". 为此,您需要右键单击项目并选择“添加引用”,然后选择“ .NET”选项卡并单击“ System.Windows.Forms”。 Then this should work inside the .dll: 然后,这应该在.dll中起作用:

System.Windows.Forms.MessageBox.Show("Hello World!");

Of course this is a bad idea for the original poster (as covered by CodeCaster). 对于原始海报(如CodeCaster所述),这当然是一个坏主意。

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

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