Accelerometer Shaken event in windows universal app doesn't work - c#

I try to detect a shake on my phone with the Shaken event from Accelerometer object. The accelerometer object is not null but when I shake the phone, it never go in the _accelerometer_Shaken event.
public int shakeCount = 0;
Accelerometer _accelerometer = Accelerometer.GetDefault();
public MainPage()
{
this.InitializeComponent();
if (_accelerometer != null)
{
_accelerometer.Shaken += _accelerometer_Shaken;
}
}
async private void _accelerometer_Shaken(Accelerometer sender, AccelerometerShakenEventArgs args)
{
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
shakeCount++;
tbInfo.Text = shakeCount.ToString();
});
}
I don't understand why

This feature is not supported yet.
This is an extract from the official sample code
https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/Accelerometer
Accelerometer Shake Events
When you choose the Enable button for the Shake Events option, the app
displays the cumulative number of shake events each time an event
occurs. (The app first increments the event count and then renders the
most recent value.)
Note that shake events are not supported in Windows 10 build 10240, so
the Shaken event will never be raised, but the sample demonstrates how
to handle the event when support for shake is added.
I made a test under Windows 10 10586 and it still does not work.

Related

SystemMediaControls.ButtonPressed event fires twice

I want to add a media player control to SystemMediaControls. But, I have an issue here. When you pressed it, it occurs twice. Please see code below:
using Windows.Media;
using . . .
public class Main{
public static SystemMediaTransportControls systemMediaControls;
public Main(){
this.InitializeComponent();
systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
int num = 0;
systemMediaControls.ButtonPressed += async (SystemMediaTransportControls sender, SystemMediaTransportControlsButtonPressedEventArgs e) =>
{
num++;
Debug.WriteLine($"Event number: {num}");
};
}
}
I proved it with debug logs, and the debug logs are:
Event number: 1
Event number: 1
Event number: 2
Event number: 2
Event number: 3
Event number: 3
I have no idea where's the problem is.
This is my fault. I put event on MainPage main function. The mistake is, I call MainPage more than once in the App.cs. For example:
void LaunchApp() {
new MainPage();
...
rootFrame.Navigate(typeof(MainPage));
}
So it initializes twice. That's why the event also fires twice. I have resolved my issue.
SystemMediaControls.ButtonPressed event fires twice
I have tested with official code sample here. Unfortunately, we can't reproduce this problem, Derive official tutorial, if you want to custom SystemMediaTransportControls, we need to sync playback status To MediaPlayerState at fist. and control the media player in button pressed event.
switch (mediaPlayer.PlaybackSession.PlaybackState)
{
case MediaPlaybackState.None:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Closed;
break;
case MediaPlaybackState.Opening:
// This state is when new media is being loaded to the MediaPlayer [ie.
// Source]. For this sample the design is to maintain the previous playing/pause
// state before the new media is being loaded. So we'll leave the PlaybackStatus alone
// during loading. This keeps the system UI from flickering between displaying a "Play"
// vs "Pause" software button during the transition to a new media item.
break;
case MediaPlaybackState.Buffering:
// No updates in MediaPlaybackStatus necessary--buffering is just
// a transitional state where the system is still working to get
// media to start or to continue playing.
break;
case MediaPlaybackState.Paused:
if (mediaPlayer.PlaybackSession.Position == TimeSpan.Zero)
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Stopped;
else
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Paused;
break;
case MediaPlaybackState.Playing:
systemMediaControls.PlaybackStatus = MediaPlaybackStatus.Playing;
break;
}
For more detail please refer Manual control of the System Media Transport Controls

Proximity device handler only fires once (WP8)

I made a program that handles NFC tags back in 2013, today after two years of pause on that program i continued with it, and ran into some strange problem. In the program i read NFC tags which are writen with a "window.Someid" so they are app specific.
For some reason, the program works perfectly when scanning the NFC tag, except, when i want to scan the tag for the second time, the handler doesn't fire. any idea what causes this? (i see no error message).
Here's my code:
ProximityDevice PDevice;
private long SubscriptionID = -1;
// Constructor
public MainPage()
{
InitializeComponent();
PDevice = ProximityDevice.GetDefault();
// subscribe for messages
SubscriptionID = PDevice.SubscribeForMessage("Windows.SomeMsg", ProximityMessageRcvd);
}
private void ProximityMessageRcvd(ProximityDevice sender, ProximityMessage message)
{
Debug.WriteLine("Message");
}

Event for Geolocation Changed in Windows Phone 8

Is there any event for the geolocation changed in Windows Phone 8 (C#)?
I want to trigger some event when the geolocation is changed like city from reversegeocoding changed.
If not, then is it possible to call some event manually whenever the Phone's location like City is Changed.
(suppose I have fetched the city using reverse geocoding). (for Windows Phone 8)
I can't understand your question properly, But this answer may solve your problem.
There is a event called "PositionChanged" in geolocator. This event gets triggered when geolocator position is changed.
geolocator = new Geolocator();
geolocator.PositionChanged -= geolocator_PositionChanged;
void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
LatitudeTextBlock.Text = args.Position.Coordinate.Latitude.ToString("0.00");
LongitudeTextBlock.Text = args.Position.Coordinate.Longitude.ToString("0.00");
});
}
for more info :::http://msdn.microsoft.com/en-us/library/windows/apps/jj247548(v=vs.105).aspx

call method when phone is shaken [duplicate]

This question already has answers here:
Is there an easy way to detect shake motions on Windows Phone 8?
(3 answers)
Closed 8 years ago.
I have created a windows phone app that locates a users location when a button is pressed but I want to do away with the button and make this function occur when the phone is shaken! Below is the code that I have created so far, when the application loads it will call a function called Locate_Me which initializes the Accelerometer.
private async void Locate_Me()
{
if (accelerometer == null)
{
// Instantiate the Accelerometer.
accelerometer = new Accelerometer();
accelerometer.TimeBetweenUpdates = TimeSpan.FromMilliseconds(20);
accelerometer.CurrentValueChanged +=
new EventHandler<SensorReadingEventArgs<AccelerometerReading>>(accelerometer_CurrentValueChanged);
}
try
{
statusTextBlock.Text = "starting accelerometer.";
accelerometer.Start();
}
catch (InvalidOperationException ex)
{
statusTextBlock.Text = "unable to start accelerometer.";
}
}
So how would I go about making the onShaken function?
First step: Download ShakeGestures library from microsoft site here. Add ShakeGetures.dll to your project.
Now it's a piece of cake for you to detect shake gestures. Below is the code you can use:
//constructor of page register event handler for shake
public Page1()
{
InitializeComponent();
// register shake event
ShakeGesturesHelper.Instance.ShakeGesture +=new
EventHandler<ShakeGestureEventArgs>(Instance_ShakeGesture);
// optional, set parameters
ShakeGesturesHelper.Instance.MinimumRequiredMovesForShake = 2;
// start shake detection
ShakeGesturesHelper.Instance.Active = true;
}
private void Instance_ShakeGesture(object sender, ShakeGestureEventArgs e)
{
//call your method
}
This is the minimal code you would require. Worked for me.

How to using Timer in c# to hovering button?

I want to make hovering button in my game. Because when my cursor touch the button it will go to another screen immediately. I don't like this so much. I use xna 4.0 with visual studio 2010 to make this project. (use kinect without wpf)
How to use timer in this case ? Please help me
if (Hand.contian(Button) && holdtime == targetHoldtime)
{
}
You have to manage time by yourself based in elapsed time per frame:
ft = GameTime.Elapsed.TotalSeconds; // Xna
ft= 1/30f; // 30fps
And can be done in similar way to this:
class Button {
public float Duration = 1; // One second
public Rectangle Bounds; // Button boundaries
public float Progress { get{ return Elapsed/Duration; } }
float Elapsed = 0;
public void Update(float ft) {
if (Bounds.Contains( HandPosition ))
{
if (Elapsed<Duration) {
Elapsed += ft;
if (Elapsed>Duration) {
Elapsed = Duration;
OnClick();
}
}
} else {
Elapsed = 0;
}
}
}
I would first suggest that you look through the SDK documentation and the built in KinectInteraction controls. They may provide you with what you are looking for. Most notably SDK 1.7 removed that "HoverDwell" button in favor of a "press" action, which is a more natural interaction in a gesture system. You may want to look at using that motion instead.
If you truly desire a "click on hover" type action, you can look at the code in SDK 1.6 for an example. Several examples are available online at the Kinect for Windows CodePlex repository. The specific control example you are looking for is in the "BasicInteraction-WPF" project, and is called HoverDwellButton.
The "button" is actually a ContentControl which means you can place any content in there to make it a button. It can be a simple image, or a complex Grid. It has all the hooks to fire events when the timer on your hover goes off.
There is a decent amount of complexity in this control, which is what makes it work for a wide range of applications. At the core of the interaction is a simple DispatcherTimer.
private void OnPreviewHandEnter(object sender, HandInputEventArgs args)
{
if (this.trackedHandHovers.FirstOrDefault(t => t.Hand.Equals(args.Hand)) == null)
{
// additional logic removed for answer sanity
var timer = new HandHoverTimer(DispatcherPriority.Normal, this.Dispatcher);
timer.Hand = args.Hand;
timer.Interval = TimeSpan.FromMilliseconds(Settings.Default.SelectionTime);
timer.Tick += (o, s) => { this.InvokeHoverClick(args.Hand); };
this.trackedHandHovers.Add(timer);
timer.Start();
}
args.Handled = true;
}
Notice that the Tick event is calling InvokeHoverClick, which (in part) reads as follows:
public void InvokeHoverClick(HandPosition hand)
{
// additional logic removed for answer sanity
var t = new DispatcherTimer();
t.Interval = TimeSpan.FromSeconds(0.6);
t.Tick += (o, s) =>
{
t.Stop();
var clickArgs = new HandInputEventArgs(HoverClickEvent, this, hand);
this.RaiseEvent(clickArgs);
this.IsSelected = false;
};
t.Start();
}
This now fires an event after a set amount of time. This event can be capture and acted upon to your liking.
Again, I first recommend looking at the newer interactions in SDK 1.7. If you still want a timed hover click action, check out the links above. I used the HoverDwellButton to great effect in several different areas.

Categories

Resources