Where to write the functions of ApplicationEvents.vb when converting project to C#

耗尽温柔 提交于 2019-12-11 12:17:08

问题


I am trying to convert a VB.NET project to C#. I am conveting all the forms and classes as required, but I don't know where I need to write the events from ApplicationEvents.vb (I believe its autoGenerated from Properties)

Here is the code in my ApplicationEvent.vb file:

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

Also, any tips that may help on this conversion would be appreciated. Thanks!


回答1:


Visual Studio 2010 creates Program.cs file for C# application projects.

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() would, if this was VB.NET, fire "Startup" event. In this case, you don't need to catch that event because you control when is the application going to run using the code specified above.

If you want to subscribe to the event fired when the application is about to close, use this code:

Application.ApplicationExit += new EventHandler(Application_ApplicationExit);

and then define the function to handle this event:

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

As a result, your code should look something like this:

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



回答2:


There is no equivalent file for ApplicationEvent.vb in c#. But you can write whatever code is there in OnStartup function before you start the loop in Program.cs.

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

Hope this helps.




回答3:


Here is a way to find your answer by yourself for this kind of question:

  • Compile your VB code
  • Decompile it using a .net decompiler
  • Check where ad how Visual Studio compiled your code, and called your function

Here is some links to .Net decompiler
http://www.telerik.com/products/decompiler.aspx
http://www.jetbrains.com/decompiler/
http://www.devextras.com/decompiler/
http://wiki.sharpdevelop.net/ilspy.ashx
or maybe you'll be able to find an old version of .Net Reflector when it was free...




回答4:


How to convert this class depends entirely on how you have implemented the new C# project.

If this is a standard windows forms project, I would add the OnStartup code to Main just before the main form is opened. Of this code, you probably only need the serviceConfig and Channel-related items, not the MainForm code.



来源:https://stackoverflow.com/questions/9091872/where-to-write-the-functions-of-applicationevents-vb-when-converting-project-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!