如何将这行代码从C#转换为Visual Basic

时间:2013-06-05 15:12:54

标签: c# wpf vb.net bing-maps bing-api

我在翻译此代码时需要一些帮助:

原创于C#:

using System.Collections.ObjectModel;
using Microsoft.Maps.MapControl;

namespace Binding_Bing_Map_Control.Modal
{
public class MapModal
{
    public Location MapLocation { get; set; }
    public string TooltipText { get; set; }

    public static ObservableCollection<MapModal> getMapRecords()
    {
        ObservableCollection<MapModal> MapRecords = new ObservableCollection<MapModal>();
        MapRecords.Add(new MapModal() { MapLocation = new Location(47.610015, -122.188362), TooltipText = "Main St, Bellevue, WA 98004" });
        MapRecords.Add(new MapModal() { MapLocation = new Location(47.603562, -122.329496), TooltipText = "James St, Seattle, wa 98104" });
        MapRecords.Add(new MapModal() { MapLocation = new Location(47.609355, -122.189970), TooltipText = "Main St, Bellevue, WA 98004-6405" });
        MapRecords.Add(new MapModal() { MapLocation = new Location(47.615820, -122.238973), TooltipText = "601 76th Ave, Medina ,WA 98039" });
        return MapRecords;
    }
}
}

我对VB的翻译:

Imports System.Collections.ObjectModel
Imports Microsoft.Maps.MapControl

Namespace Map_Control.Modal

Public Class MapModal

    Public Property Location As WPF.Location
    Public Property TooltipTex As String

    Public Function getMapRecors() As ObservableCollection(Of MapModal)
        Dim MapRecords As New ObservableCollection(Of MapModal)
        MapRecords.Add(New MapModal() {Location = New WPF.Location(47, -122), TooltipTex = "Sample tooltiptext!"})
        Return MapRecords
    End Function

End Class

End Namespace

我收到了错误:

MapRecords.Add(New MapModal() {Location = New WPF.Location(47, -122), TooltipTex = "Sample tooltiptext!"})

错误:Boolean类型的值无法转换为WindowsApplication1.Map_Control.Modal.MapModal

澄清我在做什么。我正在尝试构建wpf应用程序并使用bing贴图。我正在关注此link.的代码,但我没有使用Silverlight,而且我正在使用VB进行编码。

3 个答案:

答案 0 :(得分:5)

尝试这样的事情:

MapRecords.Add(New MapModal() With {.Location = New WPF.Location(47, -122), .TooltipTex = "Sample tooltiptext!"})

答案 1 :(得分:1)

我认为问题在于:

public Location MapLocation { get; set; }

此行无法转换为

Public Property Location As WPF.Location

我认为你搞乱了Location类。请注意,在C#版本中没有对WPF名称空间的引用。

答案 2 :(得分:1)

VB.Net中的对象初始化语法不同 - 我使用an online translator得到了这个:

Imports System.Collections.ObjectModel
Imports Microsoft.Maps.MapControl

Namespace Binding_Bing_Map_Control.Modal
    Public Class MapModal

        Public Property Location As Location
        Public Property TooltipTex As String

        Public Shared Function getMapRecords() As ObservableCollection(Of MapModal)
            Dim MapRecords As New ObservableCollection(Of MapModal)()
            MapRecords.Add(New MapModal() With { _
                Key .MapLocation = New Location(47.610015, -122.188362), _
                Key .TooltipText = "Main St, Bellevue, WA 98004" _
            })
            MapRecords.Add(New MapModal() With { _
                Key .MapLocation = New Location(47.603562, -122.329496), _
                Key .TooltipText = "James St, Seattle, wa 98104" _
            })
            MapRecords.Add(New MapModal() With { _
                Key .MapLocation = New Location(47.609355, -122.18997), _
                Key .TooltipText = "Main St, Bellevue, WA 98004-6405" _
            })
            MapRecords.Add(New MapModal() With { _
                Key .MapLocation = New Location(47.61582, -122.238973), _
                Key .TooltipText = "601 76th Ave, Medina ,WA 98039" _
            })
            Return MapRecords
        End Function
    End Class
End Namespace