I'm using MediaCapture element for video capturing in UWP. However, I cannot find how can I embed this time element (Screenshot is available at here)
to the video. I'm using a Dispatcher to tick and show the time:
DispatcherTimer timer = new DispatcherTimer(); // Timer for clock.
timer.Interval = TimeSpan.FromSeconds(1); // timer ticks in one second.
timer.Tick += Timer_Tick; // Tick event handler.
timer.Start(); // Start the timer.
private void Timer_Tick(object sender, object e)
{
DateTime_Text.Text = DateTime.Now.ToString("G");// Printing datetime on page.
} // Show time in main screen with tick.
Are anyone knows how can I embed this Tick event to MediaCapture ?
Thanks.
Related
I want to send a toast notification to the action center when my app is in background. So I have set up a Timer, when I start the timer from the app, after 10 sec it produces a toast notification. Now when I try it in the debugger, the toast is produced, even if I press the home button or if I suspend the app using debugger suspend option. But when I deploy the app , it is not producing the toast when I start the timer and click home button. Can anyone suggest a solution for this..?
Adding the Code
private Timer stateTimer;
private void Button_Click(object sender, RoutedEventArgs e)
{
TimerCallback timerDelegate = new TimerCallback(timer_Tick);
TimeSpan delayTime = new TimeSpan(0, 0, 10);
AutoResetEvent autoEvent = new AutoResetEvent(false);
TimeSpan intervalTime = new TimeSpan(0, 0, 0, 0, 0);
Timer notification_timer = new Timer(timerDelegate, autoEvent, delayTime, intervalTime);
}
private void timer_Tick(object state)
{
ToastTemplateType toastTemplate = ToastTemplateType.ToastText02;
XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode("Download has complete"));
toastTextElements[1].AppendChild(toastXml.CreateTextNode("Download has complete"));
IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
((XmlElement)toastNode).SetAttribute("duration", "long");
((XmlElement)toastNode).SetAttribute("launch", "{\"type\":\"toast\",\"param1\":\"12345\",\"param2\":\"67890\"}");
ToastNotification toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
When you're debugging your app is never suspended until you explicitly choose suspension from the lifecycle events in Visual Studio. So when you press the home button your app actually continues running.
This is not the case when running the app without an attached debugger on an actual device. Once you hit the home button your app gets suspended and won't run until it's resumed.
You would have to use a background task to execute code in background.
Within a Windows Phone 7 app, is it possible to capture the hardware camera button pressed event in code?
Right now when I press the camera button nothing happens and I can't figure out how to hook up to the event.
Yes you can. Check this link. This is an example of the events:
// The event is fired when the shutter button receives a half press.
CameraButtons.ShutterKeyHalfPressed += OnButtonHalfPress;
// The event is fired when the shutter button receives a full press.
CameraButtons.ShutterKeyPressed += OnButtonFullPress;
// The event is fired when the shutter button is released.
CameraButtons.ShutterKeyReleased += OnButtonRelease;
// Provide auto-focus with a half button press using the hardware shutter button.
private void OnButtonHalfPress(object sender, EventArgs e)
{
if (cam != null)
{
// Focus when a capture is not in progress.
try
{
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = "Half Button Press: Auto Focus";
});
cam.Focus();
}
catch (Exception focusError)
{
// Cannot focus when a capture is in progress.
this.Dispatcher.BeginInvoke(delegate()
{
txtDebug.Text = focusError.Message;
});
}
}
}
I am using a scrollviewer having stackpanel with 10 low quality images .When flick once the image moves to the next and load the high resolution image of the viewed image in that view. The problem is when flick 6 times continuously,scroll moves 6 times and the method for add high resolution occurs 6 times and load the image in view. I need an idea to wait for the completed flick for some seconds and execute the high resolution add image method once. Is there any way to do that?
You can delay the loading of the high resolution images using a timer.
The code might look something like this:
DispatcherTimer timer = new DispatcherTimer();
public MainPage()
{
InitializeComponent();
timer.Interval = TimeSpan.FromSeconds(1); //You can adjust the delay suitable to your needs
timer.Tick += new EventHandler(timer_Tick);
}
void handle_Flick(object sender, GestureEventArgs args)
{
//If timer is not running, start the timer
//and do everything else other than loading high-resolution image.
if(timer.IsEnabled != true)
{
//start the timer
timer.Start();
}
}
void timer_Tick(object sender, EventArgs e)
{
//Stop the timer
timer.Stop();
//Load the high resolution image
}
Is there a way I can detect this? I want to keep performing an action as long as the user is holding on an icon.
Instead of using the GestureListener for this you could instead use the mouse manipulation events to detect how long to perform your action. For instance:
Listen for MouseLeftButtonDown to know when the user has touched the icon
Keep performing the action until either MouseLeftButtonUp or MouseLeave fire indicating that the user is no longer touching that icon
You may also have to play with MouseEnter for initiating the action
Today only i did the same thing in my project.I'll tell you the basic logic what i implemented(assuming it has to be done on button).Step 1: On the button _ManipulationStarted_ event start a timer with the interval after which you want to fire the repeat action.
Step 2: On the button _ManipulationCompleted_ event stop the timer.
Step 3: If the timer is fired,stop the timer and start another timer with interval = the repeat interval for your action.And inside the second timer fire handler perform your operation only if the control has focus. In this case, where the control is a button, you can check if the button.IsPressed is true then perform action.
The code will look something like:
Button forward=new Button();
DispatcherTimer forwardHoldTimer = new DispatcherTimer() { Interval = TimeSpan.FromSeconds(2) };
forward.ManipulationStarted += (a, b) => { forwardHoldTimer.Start(); };
forward.ManipulationCompleted += (c, d) => { forwardHoldTimer.Stop(); };
forwardHoldTimer.Tick+=(s1,e1)=>
{
forwardHoldTimer.Stop();
DispatcherTimer t = new DispatcherTimer() { Interval = TimeSpan.FromMilliseconds(100) };
t.Tick += (x, y) =>
{
if (forward.IsPressed)
{
//Your action logic will go here
}
else
t.Stop();
};
t.Start();
};
Hope this helps.
NOTE: Amresh Kumar was correct in suggesting using the manipulation events. Also, I was given the same advice on the Windows Phone App Hubs forums so I've edited this post to reflect the code changes.
Before, the UX was flaky because lifting my finger off the screen didn't always trigger a cancellation. Not surprisingly, the GestureCompleted code in the toolkit appears to be better geared towards touchscreens than are mouse button events.
XAML:
<iconControls:iconUpDownArrow>
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Tap="RangeUpTap" Hold="RangeUpHold" GestureCompleted="RangeUpCancel" />
</toolkit:GestureService.GestureListener>
</iconControls:iconUpDownArrow>
code:
private void RangeUpTap(object sender, GestureEventArgs e)
{
RangeIncrementUp(sender, e);
}
private readonly TimeSpan _rangeIncrementTimeSpan = new TimeSpan(1500000);
private readonly DispatcherTimer _rangeIncrementTimer = new DispatcherTimer();
private void RangeUpHold(object sender, GestureEventArgs e)
{
_rangeIncrementTimer.Interval = _rangeIncrementTimeSpan;
_rangeIncrementTimer.Tick += RangeIncrementUp;
_rangeIncrementTimer.Start();
}
private void RangeUpCancel(object sender, GestureEventArgs e)
{
_rangeIncrementTimer.Stop();
_rangeIncrementTimer.Tick -= RangeIncrementUp;
}
private void RangeIncrementUp(object sender, EventArgs e)
{
int range = Convert.ToInt32(tBoxRange.Text);
if (range < 1000)
{
range += 10;
}
tBoxRange.Text = range.ToString();
}
I'm creating a practice application that plays a sound every so often seconds and as a button is pressed it decreases the time interval for the sound to play. For example at the start it plays a sound every 15 seconds and when I press the button it should now play the sound every 10 seconds. Basically every time the user presses the button it decreases the interval by 5 seconds. So I have a DispatcherTimer at the top of my class and created its interval and Tick method when the page loads like such. I also have an int variable for the seconds in the time span.
DispatcherTimer timer = new DispatcherTimer();
int interval = 15;
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
timer.Interval = new TimeSpan(0, 0, interval);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
void btn_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
interval -= 5;
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(0, 0, interval);
}
But this makes the sound play crazily. Is there a proper way to do this?
if this is your actual code then Larry is quite right... u have to call timer.stop(); before starting the second.
also give some condition so as to disallow interval < 0
Do you cancel the first dispatcher timer before you start the second? Is it possible that after the first click you have two dispatcher timers firing?