Dispaly time current position media in TextBlock - Windows Phone 8.0 Silverlight - windows-phone-8

I develop an application in Windows Phone 8.0 Silverlight that play media with MediaElement. So, i want display time current position media in TextBlock during media is playing, How to do?
Thanks.

You can use a DispatcherTimer to update the textblock. Like this:
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, 1);
timer.Tick += timer_Tick;
timer.Start();
To get current position, in timer_Tick function, use yourPlayer.Position.ToString():
void timer_Tick(object sender, EventArgs e)
{
textBlock.Text = yourPlayer.Position.ToString();
}

Related

Replacement for Touch.FrameReported in Windows Phone 8.1 / Windows 10 Mobile

In my old Windows Phone 8.0 project i use Touch.FrameReported for Multitouch detect. But I can't find it in Windows Phone 8.1 and Windows 10 Mobile Universal App.
Is there a similar command in wp 8.1 and wp 10 ?
Use the CoreWindow.Pointer*** events. You can distinguish the fingers (touch contacts) by comparing the Pointer.PointerId property.
private void PointerPressedHandler(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerDeviceType != PointerDeviceType.Mouse)
{
var point = e.GetCurrentPoint(this);
var pointerId = point.PointerId;
// TODO
}
e.Handled = true;
}

Close popup on hardwarebutton backpressed in windows phone 8.1 application

I have a popup that comes on a click event of button..
But I am unable to close the popup..I want to close popup on hardwarebutton_backpress event..
Can anyone plzz tell me how to do so..
I am using this code to show popup
Public void showpopup_click()
{
Popup p =new Popup();
p.child= new Mypopup();
p.Isopen=true;
}
Here Mypopup is my user control..
I am using visual studio and developing a windows phone 8.1 app..
Another approach, in my opinion better, is using the IsLightDismissEnabled property where you don't need to check whether a popup is already open or not, or if you have multiple open popups, everything is handled automatically.
var popup = new Popup();
popup.IsLightDismissEnabled = true;
popup.IsOpen = true;
You have to subscribe to the Windows.Phone.UI.Input.HardwareButtons.BackPressed event to handle the back button with custom logic. If you store the Popup in a field, you can easily set IsOpen property to false.
Like this:
private Popup _popup;
public void showpopup_click()
{
_popup = new Popup();
_popup.Child = new Mypopup();
_popup.IsOpen = true;
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
}
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs args)
{
_popup.IsOpen = false;
args.Handled = true;
Windows.Phone.UI.Input.HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
}
In the event handler, you need to set the args.Handled property to true to prevent the application close on the back button pressed. You also need to unsubcrise from the event to avoid memory leak.

Assigning an event handler that runs on another thread

I'm trying to fit the windows phone 8 image capture code into a legacy model where I essentially have
var _autoResetEvent = new AutoResetEvent(true);
...
_autoResetEvent.Reset();
CameraCaptureTask cameraCaptureTask = new CameraCaptureTask();
cameraCaptureTask.Completed += new EventHandler<PhotoResult>(cameraCaptureTask_Completed);
cameraCaptureTask.Show();
_autoResetEvent.WaitOne();
...
void cameraCaptureTask_Completed(object sender, PhotoResult e) {
SaveImage(e);
}
Is there a way to get the event handler to run the completed event on another thread so that the AutoResetEvent won't be blocking it from running when it is called?

Geolocator position changed event

I am developing a running tracker/pedometer app, I am using geolocator for the same,I am keeping the movement threshold property of the geoLocator to 10 here is my piece of code.
button click event
private void StartButton_Click(object sender, RoutedEventArgs e)
{
myLocator = new Geolocator();
myLocator.DesiredAccuracy = PositionAccuracy.Default;
myLocator.MovementThreshold = 10;
myLocator.ReportInterval=500;
myLocator.PositionChanged += myGeoLocator_PositionChanged;
_startTime = System.Environment.TickCount;
_timer.Start();
}
void myGeoLocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
var coord = new GeoCoordinate(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
if (_line.Path.Count > 0)
{
var previousPoint = _line.Path.Last();
distance += coord.GetDistanceTo(previousPoint);
var millisPerKilometer = (1000.0 / distance) * (System.Environment.TickCount - _previousPositionChangeTick);
_kilometres += Math.Round(distance, 2);
distanceLabel.Text = string.Format("{0:f2} meters", _kilometres);
MessageBox.Show("Changed");
}
else
{
Map.Center = coord;
}
_line.Path.Add(coord);
_previousPositionChangeTick = System.Environment.TickCount;
});
}
The problem is that the position changed event is only getting called once, I am trying to debug the code in emulator by changing the location points but still the event do not get called. where am I doing wrong??
Your code will work on a real device. However, in order to test on the emulator, try by setting the DesiredAccuracy property to High.
From How to test apps that use location data for Windows Phone:
If your app uses the GeoCoordinateWatcher class, you have to specify a value of GeoPositionAccuracy.High in the constructor or in the DesiredAccuracy property of the class before you can test your app with the location sensor simulator. If you leave the accuracy at its default value of GeoPositionAccuracy.Default, the PositionChanged event doesn’t recognize position changes that occur in the location sensor simulator.
There is also another workaround, that consists on running the native Maps app, which seems to fix the problem:
Set a current location in the emulator.
Run your app. It reports the current location as Redmond.
Run the Maps application. It correctly goes to the location set in
step 1.
Run your app again. Now it uses the correct current location.
Source: http://social.msdn.microsoft.com/Forums/wpapps/en-US/c2cc57b1-ba1f-48fb-b285-d6cfbb8f393a/windows-phone-8-emulator-returns-microsofts-location-only

Animation using storyboard Windows phone 8

I am working on a Windows Phone 8 app. I am using a Storyboard to animate a series of images. It is working fine, but I want to call a particular method one second before the animation completes. I am using this code: Is there a way to do what I want?
var storyboard12 = new Storyboard
{
// RepeatBehavior = RepeatBehavior.Forever
};
var animation = new ObjectAnimationUsingKeyFrames();
Storyboard.SetTarget(animation, animationImage);
Storyboard.SetTargetProperty(animation, new PropertyPath("Source"));
storyboard12.Children.Add(animation);
for (int i = 1; i <=16; i++)
{
var keyframe = new DiscreteObjectKeyFrame
{
KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(300*i)),
Value = String.Format("/Images+Audio/images/animation images/2_Driving-a-car/Drive_background3 ({0}).png", i)
};
animation.KeyFrames.Add(keyframe);
}
DispatcherTimer timer11 = new DispatcherTimer();
timer11.Interval = TimeSpan.FromSeconds(4.1);
timer11.Tick += timer11_Tick;
timer11.Start();
storyboard12.Begin();
storyboard12.Completed += storyboard12_Completed;
}
void timer11_Tick(object sender, EventArgs e)
{
var timer = (DispatcherTimer)sender;
timer.Stop();
changeBackgroundImage3();
}
You can use an another Storyboard, that will change a background of your control. Such way is more appropriated by a performance reason. And DispatcherTimer Tick event won't be 100% fired after set interval, it could be fired later than 4.1, if it is important for you then it is the second reason to use a storyboard to change a background.