未为参数指定参数

时间:2014-08-05 06:20:13

标签: wpf vb.net c#-to-vb.net adorner

使用VB.net& WPF

我已将Overlaying Controls in WPF with Adorners的代码从C#转换为VB.Net

原始C#代码

    /// <summary> 
/// Overlays a control with the specified content 
/// </summary> 
/// <typeparam name="TOverlay">The type of content to create the overlay from</typeparam> 
public class OverlayAdorner<TOverlay> : Adorner, IDisposable where TOverlay : UIElement, new()
{
    private UIElement _adorningElement; private AdornerLayer _layer; /// <summary> /// Overlay the specified element /// </summary> /// <param name="elementToAdorn">The element to overlay</param> /// <returns></returns> public static IDisposable Overlay(UIElement elementToAdorn) { return Overlay(elementToAdorn, new TOverlay()); } 
    /// <summary> 
    /// Overlays the element with the specified instance of TOverlay 
    /// </summary> 
    /// <param name="elementToAdorn">Element to overlay</param> 
    /// <param name="adorningElement">The content of the overlay</param> 
    /// <returns></returns> 
    public static IDisposable Overlay(UIElement elementToAdorn, TOverlay adorningElement)
    {
        var adorner = new OverlayAdorner<TOverlay>(elementToAdorn, adorningElement);
        adorner._layer = AdornerLayer.GetAdornerLayer(elementToAdorn);
        adorner._layer.Add(adorner);
        return adorner as IDisposable;
    }

    private OverlayAdorner(UIElement elementToAdorn, UIElement adorningElement)
        : base(elementToAdorn)
    {
        this._adorningElement = adorningElement;
        if (adorningElement != null)
        {
            AddVisualChild(adorningElement);
        }
        Focusable = true;
    }

    protected override int VisualChildrenCount
    {
        get { return _adorningElement == null ? 0 : 1; }
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (_adorningElement != null)
        {
            Point adorningPoint = new Point(0, 0);
            _adorningElement.Arrange(new Rect(adorningPoint, this.AdornedElement.DesiredSize));
        }
        return finalSize;
    }

    protected override Visual GetVisualChild(int index)
    {
        if (index == 0 && _adorningElement != null)
        {
            return _adorningElement;
        }
        return base.GetVisualChild(index);
    }
    public void Dispose()
    {
        _layer.Remove(this);
    }
}

VB.Net代码(由我转换)

Public Class OverlayAdorner(Of TOverlay As {UIElement, New})
Inherits Adorner
Implements IDisposable

Private _adorningElement As UIElement
Private _layer As AdornerLayer

Public Shared Function Overlay(elementToAdorn As UIElement, adorningElement As TOverlay) As IDisposable
    Dim adorner = New OverlayAdorner(Of TOverlay)(elementToAdorn, adorningElement)
    adorner._layer = AdornerLayer.GetAdornerLayer(elementToAdorn)
    adorner._layer.Add(adorner)
    Return TryCast(adorner, IDisposable)
End Function

Private Sub New(elementToAdorn As UIElement, adorningElement As UIElement)
    MyBase.New(elementToAdorn)
    Me._adorningElement = adorningElement
    If adorningElement IsNot Nothing Then
        AddVisualChild(adorningElement)
    End If
    Focusable = True
End Sub

Protected Overrides ReadOnly Property VisualChildrenCount() As Integer
    Get
        Return If(_adorningElement Is Nothing, 0, 1)
    End Get
End Property

Protected Overrides Function ArrangeOverride(finalSize As Size) As Size
    If _adorningElement IsNot Nothing Then
        Dim adorningPoint As New Point(0, 0)
        _adorningElement.Arrange(New Rect(adorningPoint, Me.AdornedElement.DesiredSize))
    End If
    Return finalSize
End Function

Protected Overrides Function GetVisualChild(index As Integer) As Visual
    If index = 0 AndAlso _adorningElement IsNot Nothing Then
        Return _adorningElement
    End If
    Return MyBase.GetVisualChild(index)
End Function

Public Sub Dispose() Implements IDisposable.Dispose
    _layer.Remove(Me)
End Sub
End Class

现在我创建了MainWindow&amp;我的测试项目中的用户控件 UserControl1 并尝试代码

        Using OverlayAdorner(Of UserControl1).Overlay(G1)
               'Err in First Line Itself

        End Using

错误没有为'公共共享函数覆盖的参数'adorningElement'指定参数(elementToAdorn As System.Windows.UIElement,adorningElement As TOverlay)As System.IDisposable'。

这里错在哪里

1 个答案:

答案 0 :(得分:1)

在您所指的博客文章中给出的代码块下方显示的用法不正确。

using (OverlayAdorner<ProgressMessage>.Overlay(LayoutRoot)) 
{ 
   // Do some stuff here while adorner is overlaid
}

应该是(在C#中):

using (OverlayAdorner<ProgressMessage>.Overlay(LayoutRoot, this)) // Here I assume `this` is somehow `UserControl`'s object
{ 
   // Do some stuff here while adorner is overlaid
}

在VB.NET中

Using OverlayAdorner(Of UserControl).Overlay(G1, UserControl1)
   ' Do some stuff here while adorner is overlaid
End Using

另请注意:OverlayAdorner(Of UserControl1)应为OverlayAdorner(Of UserControl),因为T此处应为Type的名称而非对象实例的名称。

我希望有所帮助。

一些讲道

从你的这个问题我得到提示你需要更多地学习语言。给VB.NET或C#.NET留出更多时间,并在Visual Studio中使用intellisense让自己熟悉要传递给您使用的方法的类库和参数类型。这将有助于您在新的类库中工作,这些库有一些文档。

在新的类库中工作时,我更多地使用此快捷方式。键入 ctrl + k 然后 ctrl + i 。通常称为 ctrl + k i

更新

根据最近的评论,请查看以下实际用法:

XAML摘要:

<Window x:Class="WpfTestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid x:Name="G1">
        <UserControl x:Name="ucMyControl"></UserControl>
    </Grid>
</Window>

代码段落背后的代码:

Using OverlayAdorner(Of UserControl).Overlay(G1, ucMyControl)
   ' Do some stuff here while adorner is overlaid
End Using