如何从C#中的匿名事件处理程序转换为VB.Net

时间:2012-04-16 16:40:14

标签: .net windows-phone-7 c#-to-vb.net

我有以下Windows Phone 7应用程序的示例代码,我正在尝试将其转换为VB.Net作为起点。这样的任务:

Loaded += (_, __) => { anonymousMethodBody();}
当我使用C#-to-VB转换工具时,

无法转换。应该如何翻译?

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 个答案:

答案 0 :(得分:3)

Loaded事件处理程序是使用lambda表达式发布的C#代码中定义的。我想大多数VB.NET-C#转换器都不能很好地处理它们,因为它们相对较新。试试这个:

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

您无需致电RemoveHandler(请阅读以下评论)。