VB WPF to C# WPF

本文关键字:WPF to VB | 更新日期: 2023-09-27 18:30:53

我正在尝试从使用 VB WPF 转向使用 C# WPF,到目前为止,由于我拥有的代码量,我尝试使用在线转换器。问题是我在理解出现的一些错误时遇到了一些麻烦,并且作为 C# 的初学者,我有点迷茫。

下面的代码是我目前与标准 VB WPF 一起使用的代码,工作得很好,并且是 c# 转换器将其更改为的内容的副本。(注意,我已将必应地图 WPF 引用添加到 VB 和 C#)

Private Sub Aberdeen() Handles BTNCounty.Click
    If TXTCounty.Text = "Aberdeen" Then
        Dim CountyLocation(2) As Microsoft.Maps.MapControl.WPF.Location
        CountyLocation(0) = New Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584)
        CountyLocation(1) = New Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584)
        CountyLocation(2) = New Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633)

Dim names = New String() {"Aberdeen Central",   "Aberdeen Lochnagar", "Aberdeen Kincorth"}
 For index = 0 To CountyLocation.Length - 1
            Dim Pin = New Microsoft.Maps.MapControl.WPF.Pushpin()
            Dim CoordinateTip = New ToolTip()
            CoordinateTip.Content = names(index)
            Pin.Location = CountyLocation(index)
            Pin.ToolTip = CoordinateTip
            BingMap.Children.Add(Pin)

        Next

    End If
End Sub

下面是将代码转换为 c# 的示例

private void Aberdeen()
{
if (TXTCounty.Text == "Aberdeen") {
    Microsoft.Maps.MapControl.WPF.Location[] CountyLocation = new Microsoft.Maps.MapControl.WPF.Location[3];
    CountyLocation(0) = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
    CountyLocation(1) = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
    CountyLocation(2) = new Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633);

    dynamic names = new string[] {
        "Aberdeen Central",
        "Aberdeen Lochnagar",
        "'tAberdeen Kincorth"
    };
    for (index = 0; index <= CountyLocation.Length - 1; index++) {
        dynamic Pin = new Microsoft.Maps.MapControl.WPF.Pushpin();
        dynamic CoordinateTip = new ToolTip();
        CoordinateTip.Content = names(index);
        Pin.Location = CountyLocation(index);
        Pin.ToolTip = CoordinateTip;
        BingMap.Children.Add(Pin);

    }

}
}

收到 3 个错误,我想知道您是否可以告诉我它们的含义以及如何解决问题?

  1. 县位置是一个变量,但像方法一样使用?

2 名称索引在当前上下文中不存在?

3 System.Windows.FrameworkElement.ToolTip 是一个属性,但像类型一样使用?

任何帮助将不胜感激,因为这对我来说非常陌生。

VB WPF to C# WPF

请参阅内联答案。主要问题是转换器已将所有类型推断调用(Dim variable = ...)转换为动态,这是不正确的。应使用 var 进行类型推断。

private void Aberdeen()
{
    if (TXTCounty.Text == "Aberdeen") {
        Microsoft.Maps.MapControl.WPF.Location[] CountyLocation = new Microsoft.Maps.MapControl.WPF.Location[3];
        // Error 1: Setting array variables is done using square brackets, otherwise it's considered a method invocation
        CountyLocation[0] = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
        CountyLocation[1] = new Microsoft.Maps.MapControl.WPF.Location(57.143652, -2.1056584);
        CountyLocation[2] = new Microsoft.Maps.MapControl.WPF.Location(57.124838, -2.0991633);
        // extra: you don't need dynamic here, just var will do
        var names = new string[] {
            "Aberdeen Central",
            "Aberdeen Lochnagar",
            "'tAberdeen Kincorth"
        };
        // Error 2: you need to declare the index variable (added var)
        for (var index = 0; index <= CountyLocation.Length - 1; index++) {
            // Error 3: don't need dynamic here
            var Pin = new Microsoft.Maps.MapControl.WPF.Pushpin();
            // don't need dynamic here 
            var CoordinateTip = new ToolTip();
            // Same as error 1: Array access is done with square brackets
            CoordinateTip.Content = names[index];
            // Same as error 1: Array access is done with square brackets    
            Pin.Location = CountyLocation[index];
            Pin.ToolTip = CoordinateTip;
            BingMap.Children.Add(Pin);
        }   
    }
}