将C#转换为VB.NET:具有返回类型的委托类型的事件

时间:2019-07-10 16:09:48

标签: c# asp.net vb.net

我正在尝试在ASP.NET中处理多个客户端回调,而我正在遵循此article

我已经将C#代码转换为我的环境VB.NET/ASP.NET。

使用的命名空间为 SmartSoft.Web.UI.WebControls

在名为 ClientCallback 的类中,我有这段代码

Public Delegate Function OnRaiseCallbackEvent(ByVal eventArgument As String) As String

Public Event Raise As OnRaiseCallbackEvent

编译器用以下错误消息标记事件声明:

  

不能使用具有返回类型的委托类型声明事件。

我尝试遵循解决方案here,但如果声明

Public Raise As OnRaiseCallbackEvent

我在另一个名为 SampleCompositeControl

的类中收到另一个错误

我去过的地方

Private button1 As Button
Private button1Callback As ClientCallback

Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
   ......
End Sub

Protected Overrides Sub CreateChildControls()
    button1 = New Button With {.ID = "Button1",.Text = "Button 1",
            .OnClientClick = "button1Clicked('', '', callbackComplete);return false;"}

        Controls.Add(button1)
        button1Callback = New ClientCallback With {
            .ID = "button1Callback"}

        button1Callback.Raise += AddressOf button1Callback_Raise
        ..........
    End Sub

    Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
       ........
    End Sub

    Private Function button1Callback_Raise(ByVal eventArgument As String) As String
        Return "Button 1 callback processed."
    End Function

    Private Shared Function GetResource(ByVal resourceName As String) As String
       ..........
    End Function

    Private Function GetCallbackReference(ByVal control As Control) As String
        Return Page.ClientScript.GetCallbackEventReference(control, "arg", "callback", "context")
    End Function

编译器标记     button1Callback.Raise + = AddressOf button1Callback_Raise

出现此错误消息:

  没有为类型'SmartSoft.Web.UI.WebControls.SmartSoft.Web.UI.WebControls.ClientCallback.OnRaiseCallbackEvent'和'Void'定义

'+'运算符。

如何在不丢失功能的情况下将代码从C#正确转换为VB.NET?

2 个答案:

答案 0 :(得分:1)

VB.NET使用AddHandler进行事件注册,而C#使用+=语法。

尝试AddHandler button1Callback.Raise, AddressOf button1Callback_Raise

答案 1 :(得分:0)

以这种方式解决:

遵循djv的建议(在VB.NET中,事件无法返回C#中的值)

我已经用C#构建了一个WebControl,将控件添加到probject 并在ASPX页面中放置此标签

<%@ Register Assembly="SmartOBJ.Web.UI.WebControls" Namespace="SmartOBJ.Web.UI.WebControls" TagPrefix="SmartOBJ" %>

<SmartOBJ:SampleControl ID="SampleControl1" runat="server" />

在ASPX中点击该调用

..... other javascript code
switch (Type) {
    case 'E':
        delRECORD(ID_RECORDD.toString()+';E', '', callbackComplete); return false;break;
    case 'SC':
        delRECORD(ID_RECORDD.toString() + ';SC', '', callbackComplete); return false;break;
    case 'SS':
        mess = 'xxxx';break;}
........

此c#文件:

using System.IO;
using System.Reflection;
using System.Web.UI;

using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

[assembly: TagPrefix("SmartOBJ.Web.UI.WebControls", "SmartOBJ")]
namespace SmartOBJ.Web.UI.WebControls
{
    [ToolboxData("<{0}:SampleControl runat=server></{0}:SampleControl>")]
    public class SampleControl : CompositeControl
    {
        #region members
        private Button button1;
        private ClientCallback delRECORD;
        #endregion

        protected override void OnPreRender(System.EventArgs e)
        {
            base.OnPreRender(e);

            // Register client side script for the callbacks
            string clientScript = GetResource("SmartOBJ.Web.UI.WebControls.Resources.SampleControlClientScript.js");
            clientScript = clientScript.Replace("{button1_callback_reference}", GetCallbackReference(button1Callback));
            clientScript = clientScript.Replace("{delRECORD_callback_reference}", GetCallbackReference(delRECORD));
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "client_script", clientScript, true);
        }

        protected override void CreateChildControls()
        {
            // Create buttons
            button1 = new Button
                          {
                              ID = "Button1",
                              Text = "Button 1",
                              Visible = false,
                              OnClientClick = "button1Clicked(ID_RECORD, '', callbackComplete);return false;"

                          };
            Controls.Add(button1);


            // Create callback controls
            delRECORD = new ClientCallback { ID = "delRECORD" };
            delRECORD.Raise += delRECORD_Raise;
            Controls.Add(delRECORD);


        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            // Render buttons
            button1.RenderControl(writer);

            // Render result div
            writer.AddAttribute(HtmlTextWriterAttribute.Id, "result");
            writer.RenderBeginTag(HtmlTextWriterTag.Div);
            writer.RenderEndTag();

            // Render callback controls
            delRECORD.RenderControl(writer);
        }

        /// Handles client callback events for delRECORD.
        /// <param name="eventArgument">
        private string delRECORD_Raise(string eventArgument)
        {
            ToolsDB mytool=new ToolsDB();
            bool verify=false;
            string msg="";
            string msg2 = "";

            string[] parameters = Regex.Split(eventArgument, ";"); 

            if  (parameters[1] == "E")
                {
                    msg = "msg1";
                    msg2= " msg2"; 
                    verify=mytool.delRECORD(parameters[0],parameters[1]); // This part execute other code to manage DB and is defined in another class
                }

            if (parameters[1] == "SC")
            {
                msg = " msg1";
                msg2 = " msg2";
                verify = mytool.delRECORD(parameters[0], parameters[1]);
            }


            if (verify == true) 
                {return "ALLISOK;" + parameters[0] +";Record with ID " + parameters[0] + msg;}
            else
                { return "NOTOK;" + parameters[0] + ";Record with ID " + parameters[0] + msg2; }        
        }

        private string button1Callback_Raise(string eventArgument)
        {
            return eventArgument.ToString() + "_" + "Button 1 callback processed." ;
        }


        /// Helper to load embedded resource as a string.
        private static string GetResource(string resourceName)
        {
            Assembly assembly = Assembly.GetExecutingAssembly();
            string result = string.Empty;
            Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
            if (resourceStream != null)
            {
                using (TextReader textReader = new StreamReader(resourceStream))
                {
                    result = textReader.ReadToEnd();
                }
            }
            return result;
        }

        private string GetCallbackReference(Control control)
        {
            return Page.ClientScript.GetCallbackEventReference(control, "arg", "callback", "context");
        }
    }
}

此js中的资源文件包含

function button1Clicked(arg, context, callback) {
    {button1_callback_reference};
}


function delRECORD(arg, context, callback) {
    {delRECORD_callback_reference};
}

function callbackComplete(result, context) {
    var parameters = result.split(";");
    if (parameters[0] == 'ALLISOK')
        {
        $table.bootstrapTable('remove', {
            field: 'id',
            values: [IDRow]
        });
        alert(parameters[2]);
        $('#deletemovedir').modal('toggle');
    }
    else
        {
        alert(parameters[2]);
        }
}