首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VB WPF到C# WPF

VB WPF到C# WPF
EN

Stack Overflow用户
提问于 2016-01-17 16:47:17
回答 1查看 428关注 0票数 1

我正在尝试从使用VB到C# WPF,到目前为止我尝试的是使用一个在线转换器,因为我有大量的代码。问题是,我遇到了一些麻烦,理解了一些错误,并且是C#的初学者,我有点迷路了。

下面的代码是我目前与标准VB一起使用的代码,非常好地工作,以及c#转换器将其转换成的副本。(注:我在VB和C#中都添加了必应地图WPF引用)

代码语言:javascript
复制
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#的示例

代码语言:javascript
复制
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);



    }


}
}

我收到了三个错误,我想知道你能不能告诉我它们的意思以及如何解决这个问题?

  1. CountyLocation是一个变量,但使用起来像一种方法?

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

3 System.Windows.FrameworkElement.ToolTip是一个属性,但它被用作类型吗?

对我来说,任何帮助都将是非常感谢的,因为这是非常外国的领土。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-17 16:55:33

请在线查看答案。主要问题是转换器已经将所有类型推断调用(Dim variable = ...)转换为dynamics,这是不正确的。您应该使用var进行类型推断。

代码语言:javascript
复制
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);
        }   
    }
}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34840836

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档