I am working on UWP Windows 10 application using C#.
For some animation I was relying on PointerPressed and PointerReleased events. Hoping that these will be fired in pairs. And I was wrong. Check what Microsoft has to say about this:
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.pointercapturelost
Now, I am using PointerCaptureLost in place of PointerReleased and it is working fine. Only problem is if I use AddHandler for PointerCaptureLost it shows an error: "UIElement.PointerCaptureLost can only appear on the left hand side of += or -=". It is only working when added as +=.
Any thoughts why it is like this?
You should use UIElement.PointerCaptureLostEvent with AddHandler, because that is the Routed Event ID, which specifies the event. In contrast, x.PointerCaptureLost (where x is the name of the control) is the field that represents the Event Handler itself.
I had no problem using both x.AddHandler(UIElement.PointerCaptureLostEvent, ...) and x.PointerCaptureLost += ... in my code. Moreover, as this link suggests, they both translate to the same
call to AddRoutedEventHandler.
Related
I'm really confused about this Topic. None of the tutorials I found to e.g. move objects around when there's touch input works. For example they all do something like this:
Control.AddHandler(UIElement.ManipulationStartedEvent, new EventHandler
<ManipulationStartedEventArgs>(Control_ManipulationStarted), true);
But there's no ManipulationStartedEventArgs, VS2013 can't find it and there's no way to add a using directive. Are those tutorials old and did MS change the way the ManipulationDelta works?
Adding it with the EventHandler section of the Properties section again doesn't work, no Event is fired no matter what I'm trying to do.
For manipulation to work the UI Element must have ManipulationMode property set to something other than None or System to be a manipulation event source; i.e Set ManipulationMode to TranslateX if you want an event to fire on a horizontal pointer movement.
For UI manipulation in Windows Universal you have 3 events:
ManipulationStarted
ManipulationDelta
ManipulationCompleted
Each with their own EventArgs under System.Windows.Input namespace
ManipulationStartedEventArgs
ManipulationDeltaEventArgs
ManipulationCompletedEventArgs
The problem may however be the type of UI Element that you are using, not all accept/generate manipulation events.
Examples of UI Elements that don't:
WebView
(I expect Canvas but not sure, haven't tested)
Examples of UI Elements that do:
Textblock
ListView
I have a C# WinRT app that uses the WebView control. None of the events I have created event handlers for are firing:
Uri theUri = new Uri("http://bing.com/");
webViewVideoPlayer.NavigationStarting += webViewVideoPlayer_NavigationStarting;
webViewVideoPlayer.NavigationCompleted += webViewVideoPlayer_NavigationCompleted;
webViewVideoPlayer.ContentLoading += webViewVideoPlayer_ContentLoading;
webViewVideoPlayer.NavigationFailed += webViewVideoPlayer_NavigationFailed;
webBrowserVideoPlayer.Navigate(theUri);
I have set breakpoints on the first line in each of the event handler bodies. The web page renders fine, but none of the breakpoints are hit. What can I do to fix this?
The app is currently configured for Debug build, x86 platform.
[TO THE MODERATORS: This is not my best post obviously. I was tired and made a silly mistake. If you feel it is best to delete it, please do.]
You've got two different control names in your code. The events are attached to webViewVideoPlayer, yet later the Navigate is called on a different WebView called webBrowserVideoPlayer.
I did a test with a WebView and the events were called as expected.
For the last 2 month I've written application in C# in Visual Studio. Now i have to port that application to Linux by Monodevelop. Well, I already solved most of the porting errors, but there is one that i cant figure out. All double click and mouse double click events from Visual Studio stopped working - I even created small 1-form application that i ported to Mono with only form and one event (double click) - it also didnt work - so that means Monodevelop cant port double click events from Visual Studio ? I already checked WND_Proc function and Linux dont throw up any corresponding double-click event (it was 515 for in window and 3 hundred something on title bar...). Im already giving up and preparing for writing additional code to fix all double-click issue in my code but maybe someone has an answer.
Im using Ubuntu linux (if its neccesary i might tommorow check kernel version), MONO: 2.8.3, Visual Studio 2008 and project in .NET 3.5.
When implementing mouse clicks, there are two main differences between Windows and Gtk# that you should keep in mind:
Gtk# does not offer a 'double-click' signals ('Events' in Windows lingo), but only single 'click' signals. However Gdk library does implements both double-click and triple-click with its EventButton class!
Gtk# differentiates between Widgets (or 'Controls' in Windows lingo) and 'Containers' (there is no direct comparable in Windows). Most widgets placed on a Gtk# form will NOT receive mouse click events. In order to receive a mouse event you need to place the widget inside a specific container - like EventBox.
Here is how you do it Gtk#:
A. Add an EventBox containter to your form (in the example below: eventbox1). You can place it behind other Widgets or since it is not visible, unless you specifically select it to be (or change its background color). You can put a widget inside the EventBox, but you are limited to only one widget, that will also get the shape and size of the EventBox.
B. Add to this EventBox the signal 'ButtonPressEvent' out of the "Common Widget Signals" (in the example below: OnEventbox1ButtonPressEvent)
Every time a mouse button (left, middle or center or a combination) is clicked inside the EventBox, it will trigger this event and the function OnEventbox1ButtonPressEvent() will be called. If you need to identify the button that was clicked while handling this event, use the uint value in: args.Event.Button typically '1' will be the left mouse button, '2' the center button and '3' the right button ('2' might be also when both left and right buttons are clicked).
By the way, mouse motion events (without a button press) are not sent by default. So if you need to sense them you will need to add the PointMotionMask as well in the first like of the code example below.
Here is a code example of the ButtonPress Event Handler (the EventBox name is 'eventbox1') catching a double-click event using the EventButton class:
// The following line is may not be needed but is here to show how to do it
eventbox1.GdkWindow.Events = eventbox1.GdkWindow.Events | Gdk.EventMask.ButtonPressMask;
protected void OnEventbox1ButtonPressEvent (object o, ButtonPressEventArgs args)
{
if( ((Gdk.EventButton)args.Event).Type == Gdk.EventType.TwoButtonPress)
System.Media.SystemSounds.Beep.Play (); // Play a sound only if this is a double-click event
}
The order of the events received (in case of a double click) is:
Gdk.EventType.ButtonPress
Gdk.EventType.ButtonRelease
Gdk.EventType.ButtonPress
Gdk.EventType.TwoButtonPress
Gdk.EventType.ButtonRelease
Hope that helps!
GTK# treats double-click events differently than Windows Forms. You're going to have to write code to translate the events. If you're doing that, you may as well spend the time arguing against double-click as an idiom.
I need to remove event handlers from a control loaded from a dll that I don't have the code for. Since there doesn't seem to be an 'official' (i.e. supported by public methods of the .NET Framework), I was was able to create a couple extension methods that did exactly that using Reflection.
See this blog post for all the details: Removing an Event from a WinForm ListView control using reflection
Here is a code sample of how to remove the SelectedIndexChanged event (dynamically and without access to the original handler)
//for a UserControl (in fact any control that implements System.ComponentModel.Component)
var userControl = new UserControl();
//we can get the current mapped event handlers
userControl.eventHandlers();
//its signature
userControl.eventHandlers_MethodSignatures();
//remove one by using the static field name
userControl.remove_EventHandler("EVENT_SELECTEDINDEXCHANGED");
//or use this one specifically mapped to the SelectedIndexChanged event
userControl.remove_Event_SelectedIndexChanged
My question is: "is there another way?"
Although my solution works and seems stable, I'm doing internal .NET objects manipulation, so maybe there is a better solution (in 4.0 or 4.5)?
Related posts:
How would that be possible to remove all event handlers of the Click event of a Button? - uses a similar solution to mine, but I don't think their solution works
I want to implement different functionalities for single and double click on image.
I was earlier using Manipulationstarted event .What could be used now?
I am using help from click count
but (e.ClickCount) is not supported
You can do this with the gestures in the Silverlight for Windows Phone Toolkit
e.g.:
<Image Source="filename.png" >
<Controls:GestureService.GestureListener>
<Controls:GestureListener
Tap="GestureListener_Tap"
DoubleTap="GestureListener_DoubleTap" />
</Controls:GestureService.GestureListener>
</Image>
Beware that the tap event will always be called even when the double-tap event is also called. You could attempt to work round this by having a check in the tap event. (You'd need to have a short timer running on a different thread and if the double click isn't tirggered in this time then assume a double click.)
HOWEVER
From a usability point of view, you may make it easier on your users by having a tap event and a context (tap and hold) menu. This will make it much harder for the user to accidentally select the wrong option and it also gets rid of the problem above.
Having an object support both a tap (or click) and double tap event is not common because it's harder to discover or indicate to users and is easy to use the wrong action. Adding a context menu (right click menu on a PC) is the common convention for adding multiple options to users.
There is a ContextMenu also included in the Toolkit.
Shaireen,
Check out my simple blog article here: The simplest way to detect DoubleClick in Silverlight. That should give you the ability to detect both single Click's and DoubleClick's.
Good luck,
Jim McCurdy
You can use Tapped event for single click and DoubleTapped event for double click in windows phone 8.1
Why you don't have your own local counter variable.