Universal MediaElement double tapped event - c#

Someone knows, why double tap event won't fire on MediaElement, while in full window mode? Simple as that, when in FullWindow Double Tapped event won't trigger! Windows 10 Universal App!
private void MediaElement_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
mediaElement.IsFullWindow = !mediaElement.IsFullWindow;
}

Ok found a way to make this work, in place of mediaelement.DoubleTapped use
mediaElement.TransportControls.DoubleTapped += TransportControls_DoubleTapped;
private void TransportControls_DoubleTapped(object sender, Windows.UI.Xaml.Input.DoubleTappedRoutedEventArgs e)
{
mediaElement.IsFullWindow = !mediaElement.IsFullWindow;
}
But there is a problem, you can double tap on controls and it will work!

Related

c# WPF SizeChanged event fires before main window is loaded

I´m a newbie in WPF
I want to detect when main window size changes (with mouse grip), but event is fired before main window is loaded at start of the application. How come it is so?
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
MessageBox.Show("Window is Resized");
}
In first fire your previous size is 0,0 If you want to ignore it
You can try this:
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.PreviousSize != new Size())
{
MessageBox.Show("Window is Resized");
}
}
The window is initially 0 x 0. So on initial sizing, you get a notification depending on when you subscribed to the event. Do you only want to know when the window size has changed by the mouse resize? What about min / max, etc?

Windows Store App Textbox keeps losing focus

I have a simple application that has a TextBox and a Canvas. When the user touches the Canvas, I want to move the focus to the TextBox. So, on the PointerReleased event, I got:
private void canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
I also subscribed to the TextBox GotFocus and LostFocus and noticed that, immediately after the TextBox got focused, it looses the focus. Why is it loosing focus and how can I solve it?
Thanks in advance.
This will work for me.
private void Canvas_PointerReleased(object sender, PointerRoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}
private void textBoxMain_GotFocus(object sender, RoutedEventArgs e)
{
textBoxMain.Focus(Windows.UI.Xaml.FocusState.Programmatic);
}

Programatically set Focus to a TextBox in Windows Phone

In a Windows Phone app I have an TextBox and a Button. The user writes some text to the TextBox and taps the Button, the text from the TextBox is added to a list. The TextBox loses focus after the Button is tapped.
What I want to do is to set the focus back to the TextBox after the Button is tapped so the user can continue writing another text without needing to tap the TextBox.
I tried calling the Focus() method of the TextBox in the Button handler but this does not work. is there another, if any, way to do this?
When Button clicked try to add bollean flag = true. Then check this flag on event OnTextBoxLostFocus.
<TextBox x:Name="tb" Grid.Row="1" LostFocus="Tb_OnLostFocus"/>
<Button x:Name="btn" Click="Btn_OnClick" />
public partial class MainPage : PhoneApplicationPage
{
private bool flag;
public MainPage()
{
InitializeComponent();
}
private void Btn_OnClick(object sender, RoutedEventArgs e)
{
flag = true;
tb.Focus();
}
private void Tb_OnLostFocus(object sender, RoutedEventArgs e)
{
if (!flag) return;
tb.Focus();
flag = false;
}
}
Hope its help.
I have tried a lot of solutions, but this is the only one that works for me (Windows Phone 8.1 app).
First catch your TextBox's Loaded event, then call Focus(FocusState.Keyboard).
private void myTextBox_Loaded(object sender, RoutedEventArgs e)
{
myTextBox.Focus(FocusState.Keyboard);
}
Even I tried with lots of above solutions but none of them worked for me as am trying to focus on page load. Finally I got this solution and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
What happens if you call:
yourTextBox.Select(0,0)
http://msdn.microsoft.com/en-us/library/system.windows.controls.textbox.select.aspx
you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.
The way that it worked best for me on the phone was, if I wanted to focus on a particular textbox when the page loaded:
private void OnPageLoaded(object sender, RoutedEventArgs e)
{
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());
}
or if I just wanted it at a certain point. Just repeat these two lines:
Dispatcher dispatcher = Deployment.Current.Dispatcher;
dispatcher.BeginInvoke(() => EnterLocationTextBox.Focus());

How can I make an event for getting focus for double click on textbox? (Windows Phone)

I have a textBox and I call an event get focus, when click on it. The behaviour is different when I make a double click on it, how can I make an event for getting focus for double click on this textbox?
You can use the OnTap() and OnDoubleTap() methods of the TextBox. And in each method you can define the different logic and set the focus on the TextBox.
Update:
Here's a simple code structure on how to make it work:
XAML:
<TextBox x:Name="InputTextBox" Margin="0,0,0,520" />
C#
public MainPage()
{
InitializeComponent();
InputTextBox.Tap += InputTextBoxTap;
InputTextBox.DoubleTap += InputTextBoxDoubleTap;
}
private void InputTextBoxDoubleTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Double tapped!";
}
private void InputTextBoxTap(object sender, System.Windows.Input.GestureEventArgs e)
{
InputTextBox.Text = "Tapped!";
}
I tested this on both the emulator and on a device and it works in both cases!
The reason is simple.
If you have notice the arguments supplied are different
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
}
private void textBox1_Click(object sender, EventArgs e)
{
}
Yes if you want them to resemble the same you can select "MouseClick" event from the properties.
Cheers!

How do I Manually Invoke/Raise Mouse Events in Silverlight 4?

In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.
I have tried raising the event in the following fashion.
private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
MouseLeftButtonDown(sender, e);
}
However, a compiler error occurs:
"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=
What do I need to do to manually raise this event?
The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.
It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.
Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.
You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.
e.g.
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
//Handle left or right mouse buttons up event
}
In your page's init function you need to have this line of code
someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);
This is pure psuedo code but should lead you on the right track
also your method will need to look like this
void DoSomething(object sender, MouseButtonEventArgs e) { }

Categories