C#到VB.NET转换 - 哪里出错了?

时间:2014-09-20 18:53:14

标签: c# wpf vb.net modern-ui

我最近在CodePlex上发现了现代用户界面(Metro)图表,它看起来非常出色,正是我正在寻找的完成这个项目的。

https://modernuicharts.codeplex.com/documentation

然而,由于我不会编写C#,我在这种情况下依赖于在线转换器 - 它们通常非常有效,但我似乎无法使这些代码正常工作。有人能指出我必要的修正方向吗?非常感谢!

C#代码:

namespace TestApplication
{
    // bind this view model to your page or window (DataContext)
    public class TestPageViewModel
    {
        public ObservableCollection<TestClass> Errors { get; private set; }

        public TestPageViewModel()
        {
            Errors = new ObservableCollection<TestClass>();
            Errors.Add(new TestClass() { Category = "Globalization", Number = 75 });
            Errors.Add(new TestClass() { Category = "Features", Number = 2 });
            Errors.Add(new TestClass() { Category = "ContentTypes", Number = 12 });
            Errors.Add(new TestClass() { Category = "Correctness", Number = 83});
            Errors.Add(new TestClass() { Category = "Best Practices", Number = 29 });
        }

        private object selectedItem = null;
        public object SelectedItem
        {
            get
            {
                return selectedItem;
            }
            set
            {
                // selected item has changed
                selectedItem = value;                
            }
        }
    }

    // class which represent a data point in the chart
    public class TestClass
    {
        public string Category { get; set; }

        public int Number  { get; set; }        
    }
}

VB.NET翻译:

Public Class TestPageViewModel
    Public Property Errors() As ObservableCollection(Of TestClass)
        Get
            Return m_Errors
        End Get
        Private Set
            m_Errors = Value
        End Set
    End Property
    Private m_Errors As ObservableCollection(Of TestClass)

    Public Sub New()
        Errors = New ObservableCollection(Of TestClass)()
        Errors.Add(New TestClass() With { _
            Key .Category = "Globalization", _
            Key .Number = 75 _
        })
        Errors.Add(New TestClass() With { _
            Key .Category = "Features", _
            Key .Number = 2 _
        })
        Errors.Add(New TestClass() With { _
            Key .Category = "ContentTypes", _
            Key .Number = 12 _
        })
        Errors.Add(New TestClass() With { _
            Key .Category = "Correctness", _
            Key .Number = 83 _
        })
        Errors.Add(New TestClass() With { _
            Key .Category = "Best Practices", _
            Key .Number = 29 _
        })
    End Sub

    Private m_selectedItem As Object = Nothing
    Public Property SelectedItem() As Object
        Get
            Return m_selectedItem
        End Get
        Set
            ' selected item has changed
            m_selectedItem = value
        End Set
    End Property
End Class

错误:

Error   2   Name of field or property being initialized in an object initializer must start with '.'.   C:\Users\Major\documents\visual studio 2013\Projects\WpfApplication3\WpfApplication3\pModernChart.xaml.vb   17  4   WpfApplication3

Warning 1   'Public Sub New()' in designer-generated type 'WpfApplication3.pModernChart' should call InitializeComponent method.    C:\Users\Major\documents\visual studio 2013\Projects\WpfApplication3\WpfApplication3\pModernChart.xaml.vb   14  16  WpfApplication3

Error   1   Type 'ObservableCollection' is not defined. C:\Users\Major\documents\visual studio 2013\Projects\WpfApplication3\WpfApplication3\pModernChart.xaml.vb   2   33  WpfApplication3

2 个答案:

答案 0 :(得分:3)

  • 删除属于anonymous-types的所有Key,而不是TestClass等具体类型。
  • 添加Imports System.Collections.ObjectModel(VS告诉您,如果您将错误悬停)

Public Class TestPageViewModel
    Public Property Errors() As ObservableCollection(Of TestClass)
        Get
            Return m_Errors
        End Get
        Private Set(value As ObservableCollection(Of TestClass))
            m_Errors = value
        End Set
    End Property
    Private m_Errors As ObservableCollection(Of TestClass)

    Public Sub New()
        Errors = New ObservableCollection(Of TestClass)()
        Errors.Add(New TestClass() With {
             .Category = "Globalization",
             .Number = 75
        })
        Errors.Add(New TestClass() With {
            .Category = "Features",
            .Number = 2
        })
        Errors.Add(New TestClass() With {
             .Category = "ContentTypes",
             .Number = 12
        })
        Errors.Add(New TestClass() With {
         .Category = "Correctness",
             .Number = 83
        })
        Errors.Add(New TestClass() With {
             .Category = "Best Practices",
             .Number = 29
        })
    End Sub

    Private m_selectedItem As Object = Nothing
    Public Property SelectedItem() As Object
        Get
            Return m_selectedItem
        End Get
        Set(value As Object)
            ' selected item has changed '
            m_selectedItem = value
        End Set
    End Property
End Class

使用Key,您可以指定使用哪些属性来确定两个匿名类型是否相等以及哪些属性在编译器生成的哈希码算法中使用。在C#中,所有属性都会自动使用,您无法更改它。

答案 1 :(得分:1)

Tim Schmelter解决了你的第一个问题。

对于上一个错误,您需要正确的命名空间

Imports System.Collections.ObjectModel