65.9K
CodeProject 正在变化。 阅读更多。
Home

Bug 提交对话框

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.67/5 (5投票s)

2009年4月28日

GPL3

3分钟阅读

viewsIcon

36771

downloadIcon

446

允许用户通过 Web 服务提交各种类型 bug 的对话框

引言

此对话框允许用户提交各种类型的错误,并内置验证功能。提交的信息随后将被发送到 WebService。

bugreporter_form.gif

背景

我开发了几个小型应用程序来提高我的编码知识。如果用户能够方便地提交对我的应用程序的反馈和报告错误,将对我所有的应用程序都非常有帮助。我创建了这个对话框,并牢记它应该稳定、用户友好且易于集成到我任何的应用程序中。

Using the Code

对话框本身可以很容易地集成到您的应用程序中,但 WebService 需要一些关注。

这里有一个显示对话框的简单示例

With New BugSubmitter
    .SubmitterFile = "BN+converter-pro.php"
    .SubmitterPath = "http://service.bn2vs.com/bugs"
    .ApplicationVersion = GetAppVersion("[M].[m].[b].[r]")

    .ShowDialog()
End With

GetAppVersion 是一个简单的 sub,它返回格式化为 string 的应用程序版本。您也可以手动获取版本数据或使用 static 值。

''' <summary>Returns the version of the application</summary>
''' <param name="format">The format of the version.
''' [M]: Major
''' [m]: Minor
''' [b]: Build
''' [r]: Revision</param>
Public Function GetAppVersion(Optional ByVal format As String = _
						"v[M].[m].[b]") As String
    With My.Application.Info
        GetAppVersion = format.Replace("[M]", .Version.Major.ToString)
        GetAppVersion = GetAppVersion.Replace("[m]", .Version.Minor.ToString)
        GetAppVersion = GetAppVersion.Replace("[b]", .Version.Build.ToString)
        GetAppVersion = GetAppVersion.Replace("[r]", .Version.Revision.ToString)
    End With
End Function 

这就是显示对话框所需的全部代码!现在让我们仔细看看一些属性。

在提交错误报告之前,信息会经过验证。这可以通过用户点击提交按钮时使用错误提供程序来完成,或者通过实时验证来完成,后者仅在所有字段都有效时才启用提交按钮。

''' <summary>Gets or sets if an error provider should be used 
''' to notify a user of errors.
''' When false, live validation will only enable the submit button when all fields 
''' are valid</summary>
Public Property UseErrorProvider() As Boolean
    Get
        Return m_useErrorProvider
    End Get
    Set(ByVal value As Boolean)
        m_useErrorProvider = value
    End Set
End Property

对话框有一些与提交进度相关的事件,但在大多数情况下您无需担心这些。提交进度中有三个步骤:

  1. 创建错误报告(参见本文顶部的截图)
  2. 提交
  3. 成功屏幕(参见下图) 

用户会清楚地看到每个状态。

bugreporter_complete.gif

现在您可以开始使用该对话框并理解它的工作原理了。让我们来看看 WebService。 

Web服务

在我的应用程序中,我选择使用一个 PHP WebService,它会生成一封包含提交数据的电子邮件,然后将其发送到我的收件箱。这里的示例遵循相同的原则,但请记住,您可以以任何您想要的方式处理提交的数据,例如将其存储在数据库中。另外请注意,您也可以使用 ASP 或任何其他服务器脚本来编写 WebService。

这是一个我创建的小脚本,可以轻松地为不同的应用程序构建多个电子邮件 WebService: 

<?php

/**
 * BN+ bug report mailer
 * 
 * common.php
 * Version 1.0.0
 * 
 * By De Dauw Jeroen - jeroendedauw@gmail.com
 * April 2009
 */

// Deny access when one of the arguments is not provided
if (empty($_GET['type']) || empty($_GET['description']) || 
		empty($_GET['version'])) die("Hack attempt");

// Get the URL arguments
$type = $_GET['type'];
$description = str_replace("[amp]", "&", $_GET['description']);
$email = strlen($_GET['email']) > 0 ? $_GET['email'] : "None provided";
$version = $_GET['version'];

// Include the phpmailer class
require_once '../includes/phpMailer/class.phpmailer.php';

// Create a new instance of a phpmailer
$mail = new PHPMailer();  

// Set the sender data
$mail->From     = "bug-report@your-server.com";  
$mail->FromName = "Bug reporter @ your-server.com";    

// Create the mail with a simple layout
$body = "
<table border='1' width='100%'>

<tr bgcolor='gray'>
<td colspan='2'><u>Bug report for $app_name</u></td>
</tr>

<tr><td width='200'>Application version:</td><td>$version</td></tr>

<tr><td>Bug type:</td><td>$type</td></tr>

<tr><td>Bug description:</td><td>$description</td></tr>

<tr><td>User email:</td><td>$email</td></tr>

<tr bgcolor='gray'>
<td colspan='2'>Report send on ".date("F j, Y, g:i a")." (unix: ".time().")</td>
</tr>

</table>
";

// Set the mail subject, body and the address of the receiver(s)
$mail->Subject = "$mail_subject $type"; 
$mail->Body    = $body;  
$mail->AltBody = "Your client does not support html emails. Data cannot be accessed.";  
$mail->AddAddress("your-mail@your-server.com", "yourName"); 

// Send the email, and display either a success message and the mail contents, 
// or in case of failure the error details
// Displaying this is not required. You can also just send the mail with $mail->Send()
if ($mail->Send()) {
    echo "Mail send successfully.<br />\n\n\n\n\n\n\n\n";
    echo $body;
}
else {
    echo "Error sending mail. ".$mail->ErrorInfo;
}

?> 

通过此脚本发送的邮件将与此类似: 

bugReporter-email.gif

现在,每个 BugSubmitter 只需要几行代码。

<?php

/**
 * BugSubmitter demo bug report mailer
 * 
 * your-application-webservice.php
 * Version 1.0.0
 * 
 * By De Dauw Jeroen - jeroendedauw@gmail.com
 * April 2009
 */

$app_name = "BugSubmitter demo application";
$mail_subject = "BUG REPORT: BugSubmitter demo:";

require_once 'common.php';

?>

请注意,在此示例中,我假设您仅使用一次此 WebService,或将所有邮件发送到同一个地址。如果不是这种情况,您可以轻松地在your-application-webservice.php 中添加一个新的变量,在包含common.php 之前,包含该 WebService 的地址,然后将其添加到common.php 的邮件发送地址列表中。 

值得关注的点  

这对我来说是一次很好的练习,我创建了一个与 WebService 松散通信的 .NET 对话框。我对结果非常满意,并且现在我所有的 .NET 应用程序都在使用这个对话框。我也收到了很多积极的反馈,以及改进此对话框文档的要求,这促使我创建了这篇文章。

历史

  • 2009 年 4 月 28 日:在 The Code Project 上发布了带有 WebService 示例的文章
  • 2009 年 4 月 13 日:发布了版本 1.0.0 

参考文献

  • 有关此类文件的荷兰语支持可以在此处找到。
© . All rights reserved.