2017-02-27 48 views
3

我已經創建了一個自定義NSView,我希望將其置於窗口內容的頂部,以在加載所有內容時阻止任何交互。我遇到的問題是,我可以通過NSView點擊下面的控件,但現在已經修復。新問題是,即使我不能點擊控件,當我將鼠標移動到文本控件上時,鼠標切換到I Beam圖標。在Xamarin Mac中阻止鼠標泡泡

我如何使NSView完全阻止與它下面的所有交互?我創建

的一個NSView低於:

[Register("StupidView")] 
public class StupidView : NSView 
{ 


    public StupidView() 
    { 
     // Init 
     Initialize(); 
    } 

    public StupidView(IntPtr handle) : base (handle) 
    { 
     // Init 
     Initialize(); 
    } 

    [Export("initWithFrame:")] 
    public StupidView(CGRect frameRect) : base(frameRect) { 
     // Init 
     Initialize(); 
    } 

    private void Initialize() 
    { 
     this.AcceptsTouchEvents = true; 
     this.WantsLayer = true; 
     this.LayerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay; 
    } 

    public override void DrawRect(CGRect dirtyRect) 
    { 
     var ctx = NSGraphicsContext.CurrentContext.GraphicsPort; 
     ctx.SetFillColor(new CGColor(128, 128, 128, 0.7f)); 
     ctx.FillRect(dirtyRect); 
    } 

    public override void MouseDown(NSEvent theEvent) 
    { 
     if (Hidden) 
     { 
      base.MouseDown(theEvent); 
     } 
    } 

    public override void MouseDragged(NSEvent theEvent) 
    { 

     if (Hidden) 
     { 
      base.MouseDragged(theEvent); 
     } 
    } 

    public override bool AcceptsFirstResponder() 
    { 
     return !this.Hidden; 
    } 

    public override bool AcceptsFirstMouse(NSEvent theEvent) 
    { 
     return !this.Hidden; 
    } 

    public override NSView HitTest(CGPoint aPoint) 
    { 
     return Hidden ? null : this; 
    } 
} 

回答

2

我幾個星期前,有同樣的問題,這裏是我怎麼能管理這樣的:在上海華

一是防止用戶交互放在下面,我添加了一個透明的按鈕,它在那裏只是爲了抓老鼠點擊,如果你沒有做,什麼都不做:

private void Initialize() 
{ 
    this.AcceptsTouchEvents = true; 
    this.WantsLayer = true; 
    this.LayerContentsRedrawPolicy = NSViewLayerContentsRedrawPolicy.OnSetNeedsDisplay; 

    //Add button to prevent user interactions 

    NSButton buttonToPreventUserInteraction = new NSButton(); 
    buttonToPreventUserInteraction.Bordered = false; 
    buttonToPreventUserInteraction.Transparent = true; 
    buttonToPreventUserInteraction.TranslatesAutoresizingMaskIntoConstraints = false; 
    AddSubview(buttonToPreventUserInteraction); 

    //If you want to add some constraints on the button, for it to resize and keep the same size of your subview 

    var dicoViews = new NSMutableDictionary(); 
    dicoViews.Add((NSString)"buttonToPreventUserInteraction", buttonToPreventUserInteraction); 
    NSLayoutConstraint[] buttonToPreventUserInteractionHorizontalConstraints = NSLayoutConstraint.FromVisualFormat("H:|[buttonToPreventUserInteraction]|", NSLayoutFormatOptions.DirectionLeadingToTrailing, null, dicoViews); 
    NSLayoutConstraint[] buttonToPreventUserInteractionVerticalConstraints = NSLayoutConstraint.FromVisualFormat("V:|[buttonToPreventUserInteraction]|", NSLayoutFormatOptions.DirectionLeadingToTrailing, null, dicoViews); 
    AddConstraints(buttonToPreventUserInteractionHorizontalConstraints); 
    AddConstraints(buttonToPreventUserInteractionVerticalConstraints); 

} 

您的其他問題,爲m鼠標光標從下面放置的超級視圖中的內容中更改,您可以在子視圖上添加NSTrackingArea,並實現覆蓋方法「MouseMoved」以更改光標。你可以做這樣的事情:

首先添加NSTrackingArea你的子視圖(你可以把這段代碼在你的「初始化」方法)

NSTrackingAreaOptions opts = ((NSTrackingAreaOptions.MouseMoved | NSTrackingAreaOptions.ActiveInKeyWindow | NSTrackingAreaOptions.InVisibleRect)); 
var trackingArea = new NSTrackingArea(new CGRect(0, 0, FittingSize.Width, FittingSize.Height), opts, Self, null); 

AddTrackingArea(trackingArea); 

,然後實現覆蓋方法:

public override void MouseMoved(NSEvent theEvent) 
{ 
    //You can choose the type of cursor you want to use here 
    NSCursor.ArrowCursor.Set(); 
} 

這使它對我來說,希望它也能爲你

+0

跟蹤區域破解了I-Beam問題,謝謝 – bizzehdee