MouseLeftButtonUp does not fire all the time in WP7 - c#

In my app there is a canvas with a small image (not taking up entire space of canvas).
when user clicks on image and drags inside the canvas, it traces the path with a black line. Till here everything works. Now, I want the path to be deleted as soon as the user releases the hold on the mouse. I am using MouseLeftButtonUp on the canvas to detect the mouse release. Now problem is this is working randomly. sometimes it fires and sometimes it does not. Kindly help.
XAML:
<Canvas Height="400" HorizontalAlignment="Center" Margin="10,10,0,0" Name="canvas1" VerticalAlignment="Center" Background="Aqua" Width="400">
<Image Canvas.ZIndex="30" Canvas.Left="10" Canvas.Top="10" Height="20" Name="dot1" Stretch="Uniform" Width="20" Source="/BoxIt;component/Images/dot.png" MouseLeftButtonDown="dot_MouseLeftButtonDown" />
</Canvas>
C#:
this.canvas1.MouseMove += new MouseEventHandler(canvas1_MouseMove);
this.canvas1.MouseLeftButtonUp += new MouseButtonEventHandler(canvas1_MouseLeftButtonUp);
Eventhandler in C# :
void canvas1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// code to delete the path
}

In Windows Phone we are advised to use the Tap, Hold and Manipulation* methods instead of the Mouse related events.
Please read this for more information.

Related

C# UWP Windows 10 CustomDialog: How do you show as Modeless? I can't let it freeze the info behind.

If there is no Modeless option(?) is there another way to make a small movable informational Dialog/Window/Page on top of my page. I need to keep this up as a reference but have it be movable so the underlying information can be revealed. Visual Studio 2015, Future Store App. Thanks.
You can't make the standard dialog modeless. To achieve what you want you should use a custom panel on top of your page with manipulation events hooked up to it. For example:
<Grid x:Name="LayoutRoot">
<!-- some other content -->
<Grid x:Name="Dialog" Background="Red" Width="200" Height="100"
ManipulationMode="All" ManipulationDelta="Dialog_OnManipulationDelta">
<Grid.RenderTransform>
<CompositeTransform x:Name="DialogTransform" />
</Grid.RenderTransform>
</Grid>
</Grid>
And code behind:
private void Dialog_OnManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs args)
{
DialogTransform.TranslateX += args.Delta.Translation.X;
DialogTransform.TranslateY += args.Delta.Translation.Y;
}
Then you can build more complicated logic like show/hide animations, close buttons, etc.

WPF Copied Button Click not firing

I'm quite new to c# wpf and have a problem.
I have used the answer from this post to duplicate a Grid control. The grid control contains a button. It looks like it is being duplicated correctly.
When the original control's button is pressed, the click event is handled which calls a method in the window's code.
When the copy of the control's button is pressed, the click event is not fired and the method is not called. This is confusing me as I want it to call that same method.
Maybe the event handling data is not being copied properly? Is there a way around this?
Both the origional grid and copied grid (containing the buttons) are children of another grid.
Edit:
This is the xaml for the origional grid which contains a button:
<Grid Name="TempTab" DockPanel.Dock="Left" HorizontalAlignment="Left" Margin="5,5,5,0">
<Rectangle Fill="Black" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke ="White" Margin="0,0,-2,0">
</Rectangle>
<Grid>
<DockPanel LastChildFill="False">
<TextBlock Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="3,0,3,3">Some Text</TextBlock>
<Button Width="50" BorderBrush="{x:Null}" Foreground="{x:Null}" BorderThickness="0" Margin="3,0,0,0" Click="tabdowntest">
<Button.Background>
<ImageBrush ImageSource="TopMenuBar_Close.png" Stretch="Uniform"/>
</Button.Background>
</Button>
</DockPanel>
</Grid>
</Grid>
This grid is a child of a DockPanel with name 'TabsDock'.
It is being copied with the following code:
string gridXaml = XamlWriter.Save(TempTab);
StringReader stringReader = new StringReader(gridXaml);
XmlReader xmlReader = XmlReader.Create(stringReader);
Grid newTab = (Grid)XamlReader.Load(xmlReader);
TabsDock.Children.Add(newTab);
This is the code for the 'Click' event handler which should be called when the either the origional or the copied button's are pressed. But it is only called for the origional:
private void tabdowntest(object sender, MouseButtonEventArgs e)
{
Console.WriteLine("Button Pressed");
}
The bindigs are not set, you need to set them (comment in the orig post):
To be clear, this is only half the solution (as it stood back in 08). This will cause bindings to be evaluated and the results be serialized. If you wish to preserve bindings (as the question asked) you must either add a ExpressionConverter to the Binding type at runtime (see the second part of my question for the relevant link) or see my own answer below for how to do it in 4.0.

Kinect 2 onHover click

I am working on a Kinect application for WPF (Kinect 2). I am using the KinectRegion to trigger some buttons, but i want to trigger the buttons on hover and not on click. what is the best way to achieve this? I have tried with MouseEnter and MouseLeave with no luck. My goal is that when the user hovers on a button an animation is played and then after 2 seconds the buttons is clicked. I would appreciate any help!
<k:KinectRegion x:Name="kinectRegion" >
<Grid Background="White" Name="gridTest">
<k:KinectUserViewer Height="400" Width="300" HorizontalAlignment="Center" VerticalAlignment="Top" DefaultUserColor="#FFF93636" EngagedUserColor="#FF395913" />
<Button Name="buttonStartSpel" Width="400" Height="200" Margin="0,800,0,0" VerticalAlignment="Top" HorizontalAlignment="Center" Content="Start het spel" Style="{StaticResource KinectCustomButton}" MouseEnter="buttonStartSpel_MouseEnter" MouseLeave="buttonStartSpel_MouseLeave"></Button>
</Grid>
</k:KinectRegion>
UPDATE -
Looking for multiple ways to solve this i came up with this solution:
We executed the code on window load.
void KinectPointerPointSample_Loaded(object sender, RoutedEventArgs e)
{
// Listen to Kinect pointer events
KinectCoreWindow kinectCoreWindow = KinectCoreWindow.GetForCurrentThread();
kinectCoreWindow.PointerMoved += kinectCoreWindow_PointerMoved;
}
private void kinectCoreWindow_PointerMoved(object sender, KinectPointerEventArgs args)
{
KinectPointerPoint kinectPointerPoint = args.CurrentPoint;
bool isEngaged = kinectPointerPoint.Properties.IsEngaged;
if (isEngaged)
{
System.Drawing.Point mousePt = new System.Drawing.Point((int)(kinectPointerPoint.Position.X * kinectRegion.ActualWidth - 80), (int)(kinectPointerPoint.Position.Y * kinectRegion.ActualHeight));
System.Windows.Forms.Cursor.Position = mousePt;
}
}
Noticed that the mouse pointer does not have the exact same position of the handpointer, i have tested that with negative results. Thats why i placed the mouse pointer slightly left to the handpointer (80 pixs to be exact). You can play around depending on your project. Finally we hide the mouse pointer with the following code:
this.Cursor = Cursors.None;
Now we can use the OnMouseEnter and OnMouseLeave events...I hope this helps anyone having this problem.

Strange Behaviour With MouseEnter Event

I'm developing a windows phone 8 XAML application.
I defined MouseEnter events for several objects in a XAML page. (For example two rectangles). After triggering the MouseEnter events of those objects, I press a button on the same page. Button press does not only trigger click event of it self. It also triggers MouseEnter event of the last Rectangle entered.
The problem can be reproduced with a new project with just two Rectangles and one Button. This behaviour does not appear always, but can be observed after triggering MouseEnter event of the Rectangles a few times.
My XAML objects are as follows.
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100"
Margin="102,306,0,0" Stroke="Black"
VerticalAlignment="Top" Width="100"
MouseEnter="Rectangle_MouseEnter"/>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100"
Margin="314,306,0,0" Stroke="Black"
VerticalAlignment="Top" Width="100"
MouseEnter="Rectangle_MouseEnter"/>
<Button Content="Button" HorizontalAlignment="Left"
Margin="256,481,0,0" VerticalAlignment="Top" Click="Button_Click"/>
And code behind looks like this.
private void Rectangle_MouseEnter(object sender,
System.Windows.Input.MouseEventArgs e)
{
Debug.WriteLine("Rectangle_MouseEnter");
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Debug.WriteLine("Button_Click");
}
If I remember, in Windows pone and Windows 8 apps it's better to use the tapped event instead of mouse events. It could be the problema. Take a look on this link:
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.tapped.aspx

WPF - Capture an event from a control that is not on top

I have a WPF application that I am working on where there is a button that is obscured by a partially opaque rectangle overlay. The button is still visible, but it can not receive any events because they are all caught by the rectangle which is on top of it.
Is there any way to set a pass-through so that the event is received by the next visual item underneath? If not is there some other workaround that could be used in this situation?
Set IsHitTestVisible="false" on the opaque overlay.
You need to set IsHitTestVisible="False" for the control over your button.
This example shows that a button is covered by a border, but the border doesn't get any event since because of the IsHitTestVisible="False" condition of border:
<Grid Background="Yellow">
<Button Click="Button_Click" Width="100" Height="25"/>
<Border Background="Cyan" Opacity="0.4" Width="200" Height="200" IsHitTestVisible="False" />
</Grid>
C# code,
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("iiiiiii");
}
You could add the your own event to the rectangle event (or in the click event of the rectangle it self) and check there if it's with in buttons area
Rectangle.Click += your_click_event;
private void your_click_event(object sender, RoutedEventArgs e)
{
//check if it's coordinates are within the underlining button.
//fire button click event
}
But it would be more convient to set
IsHitTestVisible="False"
Like mentioned in other posts. UIElement.IsHitTestVisible Property
I would recommend to look in to routed events in WPF. Routed events get routed based primarily on the visual tree. Routed events support a RoutingStrategy of Bubble, Tunnel, or Direct.
Understanding Routed Events and Commands In WPF

Categories

Resources