How do I Convert from Anonymous Event Handlers in C# to VB.Net

 ̄綄美尐妖づ 提交于 2019-12-20 07:14:38

问题


I have the below example code for a Windows Phone 7 application, and I am trying to convert it to VB.Net as a starting point. The assignments like this:

Loaded += (_, __) => { anonymousMethodBody();}

are failing to convert when I use a C#-to-VB conversion tool. How should those be translated?

public MainPage()
{
    InitializeComponent();

    Loaded += (_, __) =>
        {
            PhoneApplicationService.Current.ApplicationIdleDetectionMode = IdleDetectionMode.Disabled;
            cam = new VideoCamera();
            cam.Initialized += (___, ____) =>
                {
                    cam.LampEnabled = true;
                    cam.StartRecording();
                };
            vCam.SetSource(cam);

            new Thread(() =>
                {
                    try
                    {
                        var isf = IsolatedStorageFile.GetUserStoreForApplication();

                        var files = isf.GetFileNames();
                        foreach (var file in files)
                        {
                            Debug.WriteLine("Deleting... " + file);
                            isf.DeleteFile(file);
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("Error cleaning up isolated storage: " + ex);
                    }
                }).Start();
        };
}

回答1:


The Loaded event handler is defined in the C# code you posted using a lambda expression. I suppose most VB.NET-C# converters don't handle those very well, since they are relatively new. Try this:

AddHandler Loaded, Sub() 'Pass the Loaded event parameters, I cannot see them in your code
                  'The code inside the big block
                   End Sub

You don't need to call RemoveHandler (read comments below).



来源:https://stackoverflow.com/questions/10178159/how-do-i-convert-from-anonymous-event-handlers-in-c-sharp-to-vb-net

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