【问题标题】:Where to write the functions of ApplicationEvents.vb when converting project to C#项目转C#时ApplicationEvents.vb的函数在哪里写
【发布时间】:2012-02-01 06:44:18
【问题描述】:

我正在尝试将 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# vb.net vb.net-to-c#


    【解决方案1】:

    在 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();
        }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      对于此类问题,您可以通过以下方式自行找到答案:

      • 编译你的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...

      【讨论】:

        【解决方案3】:

        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());
            }
        }
        

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

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

        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();
                }
            }
        }
        

        【讨论】:

          【解决方案4】:

          如何转换这个类完全取决于你如何实现新的 C# 项目。

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

          【讨论】:

          • 我的项目启动表单是Computer_Network_MAIN_SCREEN,所以您是说我必须将Startup function 添加到该表单代码后面??
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-10
          • 1970-01-01
          • 1970-01-01
          • 2013-01-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多