Argument not specified for parameter

2019-03-02 15:21发布

问题:

Using VB.net & WPF

I've converted code available at Overlaying Controls in WPF with Adorners from C# to VB.Net

Original C# Code

    /// <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 Code (Converted by Me)

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

Now I've created MainWindow & an User Control UserControl1 in my test project and trying code

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

        End Using

Error Argument not specified for parameter 'adorningElement' of 'Public Shared Function Overlay(elementToAdorn As System.Windows.UIElement, adorningElement As TOverlay) As System.IDisposable'.

What is Wrong Here

回答1:

Below code block given on blog post you are referring to has incorrect usage shown.

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

Should be (in C#):

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

In VB.NET

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

Also note that: OverlayAdorner(Of UserControl1) should be OverlayAdorner(Of UserControl) because T here should be Type's name not name of object instance.

I hope that helps.

Some preaching

From this question of yours I get hint that you need to work yourself on language more. Give more time to VB.NET or C#.NET and use intellisense in Visual Studio to get yourself familiar with the class library and parameter types to be passed into methods you use. This will help you working in new class libraries which have some to no documentation.

I use this shortcut more when working in new class libaries. Type ctrl+k then ctrl+i. Generally said as ctrl+k,i

Update

Based on recent comment, Check the actual usage below:

XAML snippet:

<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>

Code behind snippet:

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