【问题标题】:Block mouse bubble in Xamarin Mac在 Xamarin Mac 中阻止鼠标气泡
【发布时间】:2017-02-28 16:03:57
【问题描述】:

我创建了一个自定义NSView,我想将它放在窗口内容的顶部,以在加载所有内容时阻止任何交互。我遇到的问题是我可以点击NSView 到下面的控件,尽管现在已经修复了。新的问题是即使我不能点击控件,当我将鼠标移到文本控件上时,鼠标会切换到 I Beam 图标。

如何让 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;
    }
}

【问题讨论】:

    标签: c# cocoa xamarin xamarin.mac


    【解决方案1】:

    几周前我遇到了同样的问题,我可以通过以下方式解决这个问题:

    首先,为了防止用户在下面放置的 superview 上进行交互,我添加了一个透明按钮,它只是为了捕捉鼠标点击,如果你不需要做任何事情,什么也不做:

     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);
    
     }
    

    对于您的另一个问题,即鼠标光标从下面放置的超级视图中的内容发生变化,您可以在子视图上添加一个 NSTrackingArea,并实现覆盖方法“MouseMoved”来更改光标。你可以这样做:

    首先在您的子视图上添加 NSTrackingArea(您可以将此代码放在您的“Initialize”方法中)

    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();
    }
    

    这是为我做的,希望它也适合你

    【讨论】:

    • 追踪区破解了I-Beam问题,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多