C#到VB接口转换错误

时间:2014-12-29 14:01:21

标签: c# vb.net

这是C#代码

namespace WpfApplication9.ViewModels.Interfaces
{
    public interface ITradeViewModel
    {
        ObservableCollection<Trade> GetTrades();
    }
}

namespace WpfApplication9.ViewModel
{
    public class TradesViewModel : BaseViewModel, ITradeViewModel
    {
        private ObservableCollection<Trade> _trades = new ObservableCollection<Trade>();

        private ICommand _saveCommand;
        private ICommand _loadCommand;

        public TradesViewModel()
        {
            _saveCommand = new SaveTradesCommand();
            _loadCommand = new LoadTradesCommand();
            _trades = GetTrades();
        }

        public ObservableCollection<Trade> Trades { get {return _trades;} }

        public ObservableCollection<Trade> GetTrades()
        {
            if (_trades == null || _trades.Count == 0)
                _loadCommand.Execute(_trades);
            return _trades;
        }
    }
}

当我将其转换为VB时,如下所示

Namespace WpfApplication9.ViewModels.Interfaces
    Public Interface ITradeViewModel
        Function GetTrades() As ObservableCollection(Of Trade)
    End Interface
End Namespace


Namespace WpfApplication9.ViewModel
    Public Class TradesViewModel
        Inherits BaseViewModel
        Implements ITradeViewModel

        Private _trades As New ObservableCollection(Of Trade)()

        Private _saveCommand As ICommand
        Private _loadCommand As ICommand

        Public Sub New()
            _saveCommand = New SaveTradesCommand()
            _loadCommand = New LoadTradesCommand()
            _trades = GetTrades()
        End Sub

        Public ReadOnly Property Trades() As ObservableCollection(Of Trade)
            Get
                Return _trades
            End Get
        End Property

        Public Function GetTrades() As ObservableCollection(Of Trade)
            If _trades Is Nothing OrElse _trades.Count = 0 Then
                _loadCommand.Execute(_trades)
            End If
            Return _trades
        End Function
    End Class
End Namespace

它会返回一个错误,告诉我没有实现接口ITradeViewModel的GetTrades?

我做错了什么?

感谢,

1 个答案:

答案 0 :(得分:3)

您必须通过添加Implements ITradeViewModel.GetTrades

来明确实施该方法
    Public Function GetTrades() As ObservableCollection(Of Trade) Implements ITradeViewModel.GetTrades
        If _trades Is Nothing OrElse _trades.Count = 0 Then
            _loadCommand.Execute(_trades)
        End If
        Return _trades
    End Function

VB.NET,与C#不同,does not have implicit interface implementation