Mouse wheel event

Does XF expose any mouse wheel events?
Any workarounds?

Xamarin.Forms doesn't give enough love for tablets.

Tagged:

Best Answers

  • ClintStLaurentClintStLaurent US ✭✭✭✭✭
    Accepted Answer

    I don't think you can blame Xamarin.Forms for that.
    Generally you don't see a mouse on an iPhone.
    Thus you don't have hover or mouse wheel etc. on mobile devices

  • ShimmyWeitzhandlerShimmyWeitzhandler US ✭✭✭
    edited March 2018 Accepted Answer

    @ClintStLaurent

    Generally you don't see a mouse on an iPhone.

    Although you could in a MacBook, ChromeBook etc. - I agree it's still not the general case tho.

    Thus you don't have hover or mouse wheel etc. on mobile devices

    You're kinda right, although I think it's time for XF to grow up and introduce more controls and events that aren't necessarily strictly native in all platforms.

    Bottom line, what I'm gonna do is use MessagingCenter or EventAggregator from the platform specific project and listen to those events from the appropriate VMs and wire them to the commands I need them to raise.

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭
    Accepted Answer

    I don't think you can blame Xamarin.Forms for that.
    Generally you don't see a mouse on an iPhone.
    Thus you don't have hover or mouse wheel etc. on mobile devices

  • ShimmyWeitzhandlerShimmyWeitzhandler USMember ✭✭✭
    edited March 2018 Accepted Answer

    @ClintStLaurent

    Generally you don't see a mouse on an iPhone.

    Although you could in a MacBook, ChromeBook etc. - I agree it's still not the general case tho.

    Thus you don't have hover or mouse wheel etc. on mobile devices

    You're kinda right, although I think it's time for XF to grow up and introduce more controls and events that aren't necessarily strictly native in all platforms.

    Bottom line, what I'm gonna do is use MessagingCenter or EventAggregator from the platform specific project and listen to those events from the appropriate VMs and wire them to the commands I need them to raise.

  • djgtramdjgtram HUMember ✭✭

    No, you don't see it on a phone. But you seem to forget that XF targets more than just phones. Windows UWP, for one, and macOS is just around the corner.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @djgtram said:
    No, you don't see it on a phone. But you seem to forget that XF targets more than just phones. Windows UWP, for one, and macOS is just around the corner.

    But it is designed around a central concept of "lowest common denominator". Just because iPhone can take a photo with variable depth of field don't expect to see that magically handled on Android. Nor do you see the same ActivityIndicator across all platforms. Xamarin was born out of mobile development and still shows those roots today.

    Should there be more support for the desktop OSes? Absolutely. As @ShimmyWeitzhandler said, Xamarin does need to grow up and embrace them. They also have 10,000 other things they need to do. Like all companies they have to prioritize. It is open source. There is nothing stopping someone such as yourself from writing that integration and submitting it.

  • djgtramdjgtram HUMember ✭✭
    edited September 2018

    I needed it and I wrote my own platform effect. It's easy to do. I don't say Xamarin has to supply everything at once. I just said the argument about XF targeting mobile platforms exclusively ("Generally you don't see a mouse on an iPhone.") and therefore not even needing to handle non-mobile use cases was simply not a good argument.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @ClintStLaurent said:
    I don't think you can blame Xamarin.Forms for that.
    Generally you don't see a mouse on an iPhone.
    Thus you don't have hover or mouse wheel etc. on mobile devices

    No offense @djgtram but I didn't say XF targeted mobile exclusively. That's you reading between the line of someone else's comment. I said and meant no more than what I said: That you don't generally see mouse week on a mobile device. But you could with a bluetooth mouse. Or you could be on a desktop. Nobody said otherwise. I wasn't spoiling for an argument nor looking to get into a deep dive about all the different platforms that are supported. Just a 20 work comment on the 90%+ of typical use. Geez... Its comments like that that I actually have to defend because others want to pile on and read more into than was actually said that make me want to leave this site at least three times a week. When did people stop just reading what was said and have to add 25% out of thin air?

  • djgtramdjgtram HUMember ✭✭

    You read too much into a passing comment. This one will be my last one in this topic because I never intended to start a serious dispute. I just mentioned that your comment seemed to forget that there was more to it than mere phones. It wasn't targeting or insulting either you or anybody else, it wasn't trolling, it was just a passing comment on the topic, pointing out that the need the OP described is real and important. That's all.

  • DirkWeltzDirkWeltz DEMember ✭✭✭

    @djgtram You talked about a platform effect for the mouse wheel. Would it be possible to see the code for this?

  • djgtramdjgtram HUMember ✭✭
    edited November 2018

    Why not?

    Portable

    namespace My_Mobile.Controls {
      public static class Mice {
    
        public static readonly BindableProperty WheelChangedProperty = BindableProperty.CreateAttached("WheelChanged", typeof(MouseEffect.MouseHandler), typeof(Mice), null, propertyChanged: WheelChangedHandlerChanged);
        public static void SetWheelChanged(BindableObject view, MouseEffect.MouseHandler value) => view.SetValue(WheelChangedProperty, value);
        public static MouseEffect.MouseHandler GetWheelChanged(BindableObject view) => (MouseEffect.MouseHandler)view.GetValue(WheelChangedProperty);
    
        private static void WheelChangedHandlerChanged(BindableObject bindable, object oldValue, object newValue) {
          const string EffectName = "My_Mobile.MouseEffect";
    
          if (bindable is View view) {
            var effect = view.Effects.FirstOrDefault(e => (e.ResolveId == Effect.Resolve(EffectName).ResolveId));
            if (effect == null)
              view.Effects.Add(Effect.Resolve(EffectName));
          }
        }
      }
    
      public class MouseEffect : RoutingEffect {
        public delegate void MouseHandler(object sender, MouseArgs e);
    
        public MouseEffect()
          : base("My_Mobile.MouseEffect") {
        }
    
        // ***** Args *****
    
        public class MouseArgs : EventArgs {
          public int WheelDelta { get; set; }
          public object Parameter { get; set; }
    
          public MouseArgs(int wheelDelta) {
            WheelDelta = wheelDelta;
          }
        }
    
      }
    }
    

    UWP

    using MyMouseEffect = My_Mobile.Controls.MouseEffect;
    using Mice = My_Mobile.Controls.Mice;
    
    [assembly: ExportEffect(typeof(MouseEffect), nameof(MouseEffect))]
    namespace My_Mobile.UWP.Controls {
      internal class MouseEffect : PlatformEffect {
        private MyMouseEffect.MouseHandler onWheelAction;
    
        protected override void OnAttached() {
          if (Container is FrameworkElement ctrl) {
            ctrl.PointerWheelChanged += OnWheelChanged;
    
            UpdateWheel();
          }
        }
    
        protected override void OnDetached() {
          if (Container is FrameworkElement ctrl) {
            onWheelAction = null;
            ctrl.PointerWheelChanged -= OnWheelChanged;
          }
        }
    
        protected override void OnElementPropertyChanged(PropertyChangedEventArgs args) {
          if (args.PropertyName == Mice.WheelChangedProperty.PropertyName)
            UpdateWheel();
        }
    
        private void UpdateWheel() =>
          onWheelAction = Mice.GetWheelChanged(Element);
    
        private void OnWheelChanged(object sender, PointerRoutedEventArgs e) {
          if (onWheelAction != null) {
            var point = e.GetCurrentPoint(Control);
            onWheelAction(Element, new MyMouseEffect.MouseArgs(point.Properties.MouseWheelDelta));
          }
        }
    
      }
    }
    

    Possible usage

    if (Device.RuntimePlatform == Device.UWP)
      Mice.SetWheelChanged(this, OnWheelChanged);
    
    private void OnWheelChanged(object sender, MouseEffect.MouseArgs e) {
      // use e.WheelDelta
    }
    
  • kam_arenakam_arena Member

    @djgtram said:
    Why not?

    Thanks for sharing your code, it was fine to me!

Sign In or Register to comment.