将项目转换为C#时,在哪里编写ApplicationEvents.vb的函数

本文关键字:ApplicationEvents vb 函数 在哪里 转换 项目 | 更新日期: 2023-09-27 18:22:17

我正在尝试将VB.NET项目转换为C#。我正在根据需要转换所有的表单和类,但我不知道在哪里需要从ApplicationEvents.vb(我相信它是从属性自动生成的)中编写事件

这是我的ApplicationEvent.vb文件中的代码:

Imports GM.Powertrain.RemoteCopy.Interfaces
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels
Namespace My
   Partial Friend Class MyApplication
      Private Shared serviceConfig As ServiceConfig =
         serviceConfig.Load()
      Protected Overrides Function OnStartup(
            ByVal eventArgs As StartupEventArgs) As Boolean
         Dim channel As TcpChannel = New TcpChannel()
         ChannelServices.RegisterChannel(channel, False)
         Me.MainForm = New Computer_Network_MAIN_SCREEN()
         Return MyBase.OnStartup(eventArgs)
      End Function
      Public ReadOnly Property Config() As ServiceConfig
         Get
            Return serviceConfig
         End Get
      End Property
      Public ReadOnly Property LocalMachine() As IRemoteCopier
         Get
          Return serviceConfig.GetObject(Of IRemoteCopier)("localhost")
         End Get
      End Property
   End Class
End Namespace

此外,任何可能有助于此转换的提示都将不胜感激。谢谢

将项目转换为C#时,在哪里编写ApplicationEvents.vb的函数

c#中没有等效的ApplicationEvent.vb文件。但是,在Program.cs.中开始循环之前,您可以在OnStartup函数中编写任何代码

[STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        //code in OnStartUp
        Application.Run(new Form1());
        Application.ApplicationExit += Application_ApplicationExit;
    }
    static void Application_ApplicationExit(object sender, EventArgs e)
    {
        throw new NotImplementedException();
    }

希望这能有所帮助。

以下是一种自己找到此类问题答案的方法:

  • 编译VB代码
  • 使用.net反编译器进行反编译
  • 查看Visual Studio编译代码和调用函数的位置和方式

以下是指向.Net反编译器的一些链接
http://www.telerik.com/products/decompiler.aspx
http://www.jetbrains.com/decompiler/
http://www.devextras.com/decompiler/
http://wiki.sharpdevelop.net/ilspy.ashx
或者你可以在免费的时候找到旧版本的.Net Reflector。。。

Visual Studio 2010为C#应用程序项目创建Program.cs文件。
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new frmMain());
    }
}

如果这是VB.NET,Application.Run()会触发"启动"事件。在这种情况下,您不需要捕获该事件,因为您可以使用上面指定的代码控制应用程序何时运行。

如果您想订阅应用程序即将关闭时触发的事件,请使用以下代码:

Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

然后定义处理此事件的函数:

static void Application_ApplicationExit(object sender, EventArgs e)
{
    // your shutdown code here ...
}

因此,您的代码应该是这样的:

using GM.Powertrain.RemoteCopy.Interfaces; 
using System.Runtime.Remoting.Channels.Tcp; 
using System.Runtime.Remoting.Channels; 
namespace ProjectName
{
    static class Program
    {
        private static ServiceConfig serviceConfig = serviceConfig.Load();
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            TcpChannel channel = new TcpChannel();
            ChannelServices.RegisterChannel(channel, false);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMainCard());
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
        }
        public static ServiceConfig Conifg
        {
            get { return serviceConfig; }
        }
        public static IRemoteCopier LocalMachine
        {
            get { return serviceConfig.GetObject<IRemoteCopier>("localhost"); }
        }
        static void Application_ApplicationExit(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
    }
}

如何转换此类完全取决于您如何实现新的C#项目。

如果这是一个标准的windows窗体项目,我会在打开主窗体之前将OnStartup代码添加到Main中。在这个代码中,您可能只需要serviceConfig和Channel相关的项,而不需要MainForm代码。