如何从C#中的匿名事件处理程序转换为VB.Net [英] How do I Convert from Anonymous Event Handlers in C# to VB.Net

查看:72
本文介绍了如何从C#中的匿名事件处理程序转换为VB.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Windows Phone 7应用程序的以下示例代码,并且我尝试将其转换为VB.Net作为起点。像这样的分配:

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

在我使用C#到VB转换工具时无法转换。

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


推荐答案

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

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

您不需要调用 RemoveHandler (请在下面阅读评论)。

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

这篇关于如何从C#中的匿名事件处理程序转换为VB.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆