从C#转换为VB.NET

时间:2012-08-24 20:19:45

标签: c# vb.net

我在转换代码安静方面遇到了问题。我尝试使用转换器,但根本没有运气。所有转换器都有此部分“view.Filter = delegate(object item)”的问题 提前致谢。

    private void ApplyFilters()
    {
        // Get the view
        ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
        if (view != null)
        {
            // Create a filter
            view.Filter = delegate(object item)
            {
                // Show the current object
                bool show = true;

                // Loop filters
                foreach (KeyValuePair<string, string> filter in columnFilters)
                {
                    object property = GetPropertyValue(item, filter.Key);
                    if (property != null)
                    {
                        // Check if the current column contains a filter
                        bool containsFilter = false;
                        if (IsFilteringCaseSensitive)
                            containsFilter = property.ToString().Contains(filter.Value);
                        else
                            containsFilter = property.ToString().ToLower().Contains(filter.Value.ToLower());

                        // Do the necessary things if the filter is not correct
                        if (!containsFilter)
                        {
                            show = false;
                            break;
                        }
                    }
                }

                // Return if it's visible or not
                return show;
            };
        }
    }

2 个答案:

答案 0 :(得分:1)

您可以使用Lambda Expression转换C# anonymous method

view.Filter = Function(item As Object)
        ' Show the current object
        Dim show As Boolean = True

        ' Loop filters
        For Each filter As KeyValuePair(Of String, String) In columnFilters
            Dim prop As Object = GetPropertyValue(item, filter.Key)
            If prop IsNot Nothing Then
                ' Check if the current column contains a filter
                Dim containsFilter As Boolean = False
                If IsFilteringCaseSensitive Then
                    containsFilter = prop.ToString().Contains(filter.Value)
                Else
                    containsFilter = prop.ToString().ToLower().Contains(filter.Value.ToLower())
                End If

                ' Do the necessary things if the filter is not correct
                If Not containsFilter Then
                    show = False
                    Exit For
                End If
            End If
        Next

        ' Return if it's visible or not
        Return show
      End Function

答案 1 :(得分:0)

这是因为VB.Net不支持匿名方法。

您可以尝试将委托代码提取到单独的函数调用中,然后通过转换器运行它。