Is there a way to detect when a user pluggs or unpluggs the headphones on wp7?
The problem is that when a song is playing on a background AudioPlayerAgent and the user plugs on unplugs the phones, the protected override void OnUserAction method of the AudioPlayerAgent receives a pause UserAction and pauses the music.
I need to detect the plug/unplug on the UI thread so I can update the GUI to reflect a paused state.
You need to subscribe to the PlayStateChanged event of the BackgroundAudioPlayer within your page:
public void MainPage()
{
InitializeComponent();
BackgroundAudioPlayer.Instance.PlayStateChanged += InstanceOnPlayStateChanged;
}
private void InstanceOnPlayStateChanged(object sender, EventArgs eventArgs)
{
// Update UI
}
I don't believe there currently is any API to query the current state of the head phones.
You might be able to hack a solution to your problem using the Media element. The Media element will raise an CurrentStateChanged event whenever the headphones are unplugged, so you might be able to hook up on this event to change the state of your GUI. It's not the most elegant solution, but it might but the only way at the moment.
Related
For example: a uwp pivot automatically handles Ctrl+Tab. I want to create a custom app-wide keyboard shortcut that uses Ctrl+Tab. However, if my app is currently focused on a pivot control, then the pivot steals the shortcut keydown event.
What I have tried:
For testing purposes, I created a singleton class that handles CoreWindow.KeyDown events. I register some handlers. When I press Ctrl+Tab without focusing on any element, then the singleton class handles the event. But if I focus on a pivot control in my app and press Ctrl+Tab, then the pivot steals the event and the singleton class does not. How do I make it so the singleton class picks up the event and not the focused element?
I tried to register the KeyDown event on CoreWindow. After testing, Pivot receives keyboard events earlier than CoreWindow.KeyDown events, so you can try to disable the corresponding keyboard accelerator in Pivot:
private void Pivot_ProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{
if(args.Key==Windows.System.VirtualKey.Tab && args.Modifiers == Windows.System.VirtualKeyModifiers.Control)
{
args.Handled = true;
}
}
By listening to the Pivot.ProcessKeyboardAccelerators event, we can prevent the default behavior when Ctrl+Tab is triggered (args.Handled = True).
However, it should be noted that because the event is prevented from further bubbling, the CoreWindow.KeyDown event will still not be triggered at this time.
You mentioned in the question description that you used a singleton to handle the event triggered by the shortcut. Then you need to directly call your processing method in the singleton in the code.
...
args.Handled = true;
CtrlAndTabHandle();
Thanks.
i have a Xamarin application in UWP that has multiple screens which build up while being used. on each screen there are multiple controls in which they can enter information. what i am trying to do is - if the screen has not had any interaction in the way of a touch / click from the user then to time out and go back to the beginning. i can of course capture the event of each control that has been touched and reset but i was wondering if there is like a 'Global' touch event that will fire when the screen is touched.
if there is like a 'Global' touch event that will fire when the screen is touched.
Sure, For UWP you could add PointerEntered event handler for current CoreWindow and it is Global on the top level. Please refer the following steps.
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.PointerEntered += CoreWindow_PointerEntered;
}
private void CoreWindow_PointerEntered(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.PointerEventArgs args)
{
// do some stuff
}
Is there any method is called when the device screen is rotated? Whether The data is deleted and restored as in Android?
When the screen is rotated Android application is destroyed and restarted. To keep its status during the rotation, you need to call:
onSaveInstanceState()
onRestoreInstanceState()
Is Windows 10 Universal App has a store session state during device rotation?
I know this is old, you've probably moved onto something else. For anyone else that comes across this question (like I did), the answer is: Yes, there is a method. Yes, it looks like the data is stored and retrieved, the system handles your 'Bundle' for you.
In your App.xaml.cs file there is a method:
/// <summary>
/// Invoked when application execution is being suspended. Application state is saved
/// without knowing whether the application will be terminated or resumed with the contents
/// of memory still intact.
/// </summary>
/// <param name="sender">The source of the suspend request.</param>
/// <param name="e">Details about the suspend request.</param>
private void OnSuspending(object sender, SuspendingEventArgs e)
{
var deferral = e.SuspendingOperation.GetDeferral();
//TODO: Save application state and stop any background activity
deferral.Complete();
}
This saves the state for you. This makes sense, my Windows phone will back button to the last time I hit the main screen. If I go from app to app, the back button goes back through each one, keeping the place I left off.
When I put a TextBlock on the window and change the text with a button the text remains changed after rotation. It actually remains changed after I 'back button' or 'home' out of the app. Keeping state doesn't seem to be a problem, losing it may be. (I'll remember to put a 'reset' button or method from now on!)
OnSuspending() is defined in sealed partial class App : Application, sealed means you can't extend it so you can't override OnSuspending(), either.
I am developing windows phone app.In this I want to set image slider.like image moving form right to left sides with some time interval.I have tried lot of examples but all are showing manually slide the image.but I want to slide the image automatically. please help me.
I've once created such an image carousel with a timer.
The basis to get this done is available on this post http://social.technet.microsoft.com/wiki/contents/articles/27766.windows-phone-how-to-create-a-simple-image-carousel-using-basic-windows-phone-controls.aspx
Only thing to get this triggered automatically, instead of using buttons like on the blog post, is by adding a timer to the page and on the tick event of the timer, trigger the storyboard animation.
So add a DispatcherTimer _imageTimer = new DispatcherTimer(); on your page and in the constructorset the timing and what method to call
_imageTimer.Interval = TimeSpan.FromSeconds(2.5);
_imageTimer.Tick += OnTimerTick;
No in the method that get's called when the timer goes off trigger the animation.
private void OnTimerTick(object sender, EventArgs e)
{
_scrollAnimation.From = 0;
_scrollAnimation.To = 480;
_scrollViewerStoryboard.Begin();
}
My question is, what's the different between event preview and event handler in GWT.
There is a callback function boolean onEventPreview(Event event) for event preview and a callback function void onBrowserEvent(Event event) as well. They are pretty similar, so what's the different between them? Especially when should I use the event preview at all when the event handler works perfect?
thanks
DOM.addEventPreview(EventPreview preview) lets you place an event preview on top of the event stack, which is called before any onBrowserEvent(Event event) is fired. This way you can place some logic before the event firing takes place. You can even prevent the event from firing by returning false. For example below example prevents the browser from reacting to mousemove and mousedown events.(Click and drag an image, browser won't drag an outline of image)
DOM.addEventPreview(new EventPreview() {
#Override
public boolean onEventPreview(Event event) {
switch (DOM.eventGetType(event)){
case Event.ONMOUSEDOWN:
case Event.ONMOUSEMOVE:
event.preventDefault();
}
return true;
}
});
Just a reminder, adding eventPreviews this way is depreciated. Correct way to do it is to use Event.addNativePreviewHandler(NativePreviewHandler handler)
From the javadoc:
As long as this preview remains on the top of the stack, it will receive all events before they are fired to their listeners. Note that the event preview will receive all events, including those received due to bubbling, whereas normal event handlers only receive explicitly sunk events.
You can return false from onEventPreview to cancel the event, in which case the event handlers will not be fired.