I have implemented timers behind my pages so that if the system is inactive, it will be navigated back to my HomePage, however, one of my pages's timer isnt working.
It doesnt seem to stop ticking after I navigated to the next page. I have placed my codes below on how I wrote the timer.
OnLoadState:
private void navigationHelper_LoadState(object sender, LoadStateEventArgs e)
{
// reload the timer from MainPage shell
if (e != null)
{
if (e.NavigationParameter as MainPage != null)
{
MainPage page = e.NavigationParameter as MainPage;
this.idleTimer = page.idleTimer;
idleTimer.Interval = TimeSpan.FromSeconds(20);
idleTimer.Start();
}
}
}
OnNavigatedFrom
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
navigationHelper.OnNavigatedFrom(e);
MainPage a = new MainPage();
idleTimer.Stop();
idleTimer.Tick -= a.idleTimer_Tick;
//try
//{
// if (dialogTimer != null)
// dialogTimer.Stop();
//}
//catch (Exception ex)
//{ }
}
And my button click event to navigate to the next page.
private void WebViewContactButton_Click(object sender, RoutedEventArgs e)
{
idleTimer.Stop();
var rootFrame = (Window.Current.Content as Frame);
rootFrame.Navigate(typeof(MainPage), "SMS");
}
Related
Given a point relative to a Page, how do I hittest for a TabControl's tab? VisualTreeHelper.HitTest gives me the contents, but when I go up the visual tree I see nothing that would tell me that I have actually hit a tab. I don't even see the tab control itself.
public class ViewManipulationAgent : IDisposable
{
private const int _limit = 125;
private INavigationService _navigationService;
private FrameworkElement _container;
private FrameworkElement _element;
private TranslateTransform _translate;
private IInputElement _touchTarget;
// When I use this object,
// a_container is the main Frame control in my application.
// a_element is a page within that frame.
public ViewManipulationAgent(FrameworkElement a_container, FrameworkElement a_element)
{
_navigationService = a_navigationService;
_container = a_container;
_element = a_element;
// Since I set IsManipulationEnabled to true all touch commands are suspended
// for all commands on the page (a_element) unless I specifically cancel (see below)
_element.IsManipulationEnabled = true;
_element.PreviewTouchDown += OnElementPreviewTouchDown;
_element.ManipulationStarting += OnElementManipulationStarting;
_element.ManipulationDelta += OnElementManipulationDelta;
_element.ManipulationCompleted += OnElementManipulationCompleted;
_translate = new TranslateTransform(0.0, 0.0);
_element.RenderTransform = _translate;
}
// Since the ManipulationStarting doesn't provide position I capture the position
// here and then hit test elements to find any controls for which I want to bypass
// manipulation.
private void OnElementPreviewTouchDown(object sender, TouchEventArgs e)
{
var position = e.GetTouchPoint(_element).Position;
_touchTarget = null;
HitTestResult result = VisualTreeHelper.HitTest(_element, position);
if (result.VisualHit == null)
return;
var button = VisualTreeHelperEx.FindAncestorByType<ButtonBase>(result.VisualHit) as ButtonBase;
if (button != null)
{
_touchTarget = button;
return;
}
var slider = VisualTreeHelperEx.FindAncestorByType<Slider>(result.VisualHit) as Slider;
if (slider != null)
{
_touchTarget = slider;
return;
}
}
// Here is where I cancel manipulation if a specific touch target was found in the
// above event.
private void OnElementManipulationStarting(object sender, ManipulationStartingEventArgs e)
{
if (_touchTarget != null)
{
e.Cancel(); // <- I have to cancel manipulation or the buttons and other
// controls cannot be manipulated by the touch interface.
return;
}
e.ManipulationContainer = _container;
}
private void OnElementManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
var element = e.Source as FrameworkElement;
if (element == null)
return;
var translate = _translate.X + e.DeltaManipulation.Translation.X;
if (translate > _limit)
{
GoBack();
translate = _limit;
}
if (translate < -_limit)
{
GoForward();
translate = -_limit;
}
_translate.X = translate;
}
private void GoForward()
{
var navigationService = ServiceLocator.Current.GetInstance<INavigationService>();
navigationService.GoForward();
}
private void GoBack()
{
var navigationService = ServiceLocator.Current.GetInstance<INavigationService>();
navigationService.GoBack();
}
private void OnElementManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
_touchTarget = null;
_translate.X = 0;
}
public void Dispose()
{
_element.PreviewTouchDown -= OnElementPreviewTouchDown;
_element.ManipulationStarting -= OnElementManipulationStarting;
_element.ManipulationDelta -= OnElementManipulationDelta;
_element.ManipulationCompleted -= OnElementManipulationCompleted;
}
}
I'm trying to prevent the WPF Bing Maps control from panning when the user is dragging a pushpin. What I do is that when the user selecting the pushpin with the MouseLeftButtonDown I'm, taking over the events from the map ViewChangeStart, ViewChangeOnFrame and set the e.Handled property to true.
What I was expecting is that if I set the property to true the events are canceled and panning is disabled. However the map is still panning.
Another approach what I tried is setting the property SupportedManipulations to None. Both options don't have the expected results.
Below is the code that I'm using for my DraggablePushpin
public class DraggablePushpin : Pushpin
{
private bool isDragging = false;
protected override void OnPreviewMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
var parentLayer = this.Parent as MapLayer;
if (parentLayer != null)
{
Map parentMap = parentLayer.Tag as Map;
if (parentMap != null)
{
parentMap.ViewChangeStart += parentMap_ViewChangeStart;
parentMap.MouseLeftButtonUp += parentMap_MouseLeftButtonUp;
parentMap.MouseMove += parentMap_MouseMove;
parentMap.SupportedManipulations = System.Windows.Input.Manipulations.Manipulations2D.None;
}
}
this.isDragging = true;
base.OnPreviewMouseLeftButtonDown(e);
}
protected override void OnMouseLeftButtonDown(System.Windows.Input.MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
}
void parentMap_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
var map = sender as Map;
// Check if the user is currently dragging the Pushpin
if (this.isDragging)
{
// If so, the Move the Pushpin to where the Mouse is.
var mouseMapPosition = e.GetPosition(map);
var mouseGeocode = map.ViewportPointToLocation(mouseMapPosition);
this.Location = mouseGeocode;
}
}
void parentMap_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
(sender as Map).SupportedManipulations = System.Windows.Input.Manipulations.Manipulations2D.All;
}
protected override void OnMouseLeftButtonUp(System.Windows.Input.MouseButtonEventArgs e)
{
var parentLayer = this.Parent as MapLayer;
if (parentLayer != null)
{
Map parentMap = parentLayer.Tag as Map;
if (parentMap != null)
{
parentMap.SupportedManipulations = System.Windows.Input.Manipulations.Manipulations2D.All;
}
}
}
void parentMap_ViewChangeStart(object sender, MapEventArgs e)
{
if (this.isDragging)
{
e.Handled = true;
}
}
}
My application has a couple of windows. I want to perform a certain action once the mouse is outside of all my windows for a specified time (say half a second).
For a single window I'd start a timer in the MouseLeave event, and delete that time in the MouseEnter event, but how would I go about implementing this for multiple windows ?
All Windows and Pages have access to App. Just start and cancel a BackGroundWorker that has a built in delay. If the worker completes then do you thing. I tested this with two pages.
Register a MouseEnter and MouseLeave on all pages
private void MainWindowsMouseLeave(object sender, MouseEventArgs e)
{
// MessageBox.Show("MouseLeave");
tbMouseEnterLeave.Text = "MouseLeave";
if (App.BackgroundWorkerApp.IsBusy) App.BackgroundWorkerApp.CancelAsync();
else
{
Thread.Sleep(10);
if (App.BackgroundWorkerApp.IsBusy)App.BackgroundWorkerApp.CancelAsync();
}
if (!App.BackgroundWorkerApp.IsBusy) App.BackgroundWorkerApp.RunWorkerAsync();
}
private void MainWindowsMouseEnter(object sender, MouseEventArgs e)
{
tbMouseEnterLeave.Text = "MouseEnter";
App.BackgroundWorkerApp.CancelAsync();
}
public partial class App : Application
{
private static System.ComponentModel.BackgroundWorker backgroundWorkerApp = new BackgroundWorker();
public App()
{
backgroundWorkerApp.WorkerSupportsCancellation = true;
backgroundWorkerApp.DoWork +=
new DoWorkEventHandler(backgroundWorkerApp_DoWork);
backgroundWorkerApp.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorkerApp_RunWorkerCompleted);
}
public static System.ComponentModel.BackgroundWorker BackgroundWorkerApp { get { return backgroundWorkerApp; } }
private void backgroundWorkerApp_DoWork(object sender,
DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
e.Result = ComputeApp(worker, e);
}
// This event handler deals with the results of the
// background operation.
private void backgroundWorkerApp_RunWorkerCompleted(
object sender, RunWorkerCompletedEventArgs e)
{
// First, handle the case where an exception was thrown.
if (e.Error != null)
{
MessageBox.Show(e.Error.Message);
}
else if (e.Cancelled)
{
// Next, handle the case where the user canceled
// the operation.
// Note that due to a race condition in
// the DoWork event handler, the Cancelled
// flag may not have been set, even though
// CancelAsync was called.
// MessageBox.Show("Cancel");
}
else
{
// Finally, handle the case where the operation
// succeeded.
// this where you do that thing you want to do
MessageBox.Show("Complete");
}
}
string ComputeApp(BackgroundWorker worker, DoWorkEventArgs e)
{
// Abort the operation if the user has canceled.
// Note that a call to CancelAsync may have set
// CancellationPending to true just after the
// last invocation of this method exits, so this
// code will not have the opportunity to set the
// DoWorkEventArgs.Cancel flag to true. This means
// that RunWorkerCompletedEventArgs.Cancelled will
// not be set to true in your RunWorkerCompleted
// event handler. This is a race condition.
if (worker.CancellationPending)
{
e.Cancel = true;
return "cancelled";
}
for (int i=0; i < 10; i++)
{
Thread.Sleep(100);
if (worker.CancellationPending)
{
e.Cancel = true;
return "cancelled";
}
}
return "complete";
}
}
Hi
I'm trying to get rid of the annoying "About Silverlight" context menu that pops up whenever you right click in a Silverlight application. I've added the usual ways:
In App.xaml
rootVisual.MouseRightButtonDown += ((s, args) => args.Handled = true);
and the same for all ChildWindows.
The problem that persist is in all "pop up"-controls like comboboxes and datepicker calender popup. There I can't get rid of it. I would like to handle the right click in a style that I can make implicit for the entire application. Is this possible? Can I solve it some other smart way?
Best
Daniel
The answer was to inherit the combobox and make a custom control like this:
public class CellaComboBox : ComboBox
{
public CellaComboBox()
{
DropDownOpened += _dropDownOpened;
DropDownClosed += _dropDownClosed;
}
private static void _dropDownClosed(object sender, EventArgs e)
{
HandlePopupRightClick(sender, false);
}
private static void _dropDownOpened(object sender, EventArgs e)
{
HandlePopupRightClick(sender, true);
}
private static void HandlePopupRightClick(object sender, bool hook)
{
ComboBox box = (ComboBox)sender;
var popup = box.GetChildElement<Popup>();
if (popup != null)
{
HookPopupEvent(hook, popup);
}
}
static void HookPopupEvent(bool hook, Popup popup)
{
if (hook)
{
popup.MouseRightButtonDown += popup_MouseRightButtonDown;
popup.Child.MouseRightButtonDown += popup_MouseRightButtonDown;
}
else
{
popup.MouseRightButtonDown -= popup_MouseRightButtonDown;
popup.Child.MouseRightButtonDown -= popup_MouseRightButtonDown;
}
}
static void popup_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
e.Handled = true;
}
with the extension method for framworkelement looking like this:
public static class FrameworkElementExtensions
{
public static TType GetChildElement<TType>(this DependencyObject parent) where TType : DependencyObject
{
TType result = default(TType);
if (parent != null)
{
result = parent as TType;
if (result == null)
{
for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); ++childIndex)
{
var child = VisualTreeHelper.GetChild(parent, childIndex) as FrameworkElement;
result = GetChildElement<TType>(child) as TType;
if (result != null) return result;
}
}
}
return result;
}
}
You need to handle the DatePicker in the same way but instead of DropDownOpened and DropDownClosed you use CalenderOpened and CalenderClosed
C# Corner has an article for fixing the about popup on Silverlight 3:
Disable Context Menu in Silverlight 3 Application
Assume I have a button and I want the following behavior:
when I click on the button, it fires up an event - ok, that's easy.
Now, if I click and wait, after a few seconds it suppose to fire up another event. e.g. popup a menu...
how to do that?
Are you checking the MouseUp event?
Is what you are saying if the user holds down the mouse button for 2 seconds to display a popup menu?
What I would do is on the MouseDown event create a separate thread waiting for the 2 seconds. If the MouseUp event is triggered before it expires then do nothing, else do the event.
// This event will be used for tracking if the MouseUp has been received
private System.Threading.AutoResetEvent _stopTrigger;
private void OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (this._stopTrigger == null)
{
this._stopTrigger = new System.Threading.AutoResetEvent(false);
}
Action popupProcess = new Action(this.ShowPopupAfterTime);
// Make the Popup process on a separate thread
popupProcess.BeginInvoke(null, null);
}
private void OnMouseUp(object sender, MouseButtonEventArgs e)
{
if (this._stopTrigger != null)
{
// Sends the signal to the ShowPopupAfterTime that it should NOT display the pop up
// IIt will make WaitOne return true and not go into the if statement
this._stopTrigger.Set();
}
}
private void ShowPopupAfterTime()
{
// Will enter the if after 2 seconds
if (!this._stopTrigger.WaitOne(2000))
{
// This means it has NOT be trigged thus I can display the popup
// DISPLAY POPUP
// DON"T FORGET you are on a different thread here, NOT UI thread. You will have to use the Dispatcher to get back
// to the UI thread to display the popup
}
}
Look up Timer() and DispatcherTimer()
I would use threading like this
private void mousedownEvent(object sender, MouseButtonEventArgs e)
{
//Fire off a thread which will do the waiting in the background
new Thread(delegate()
{
//Wait for 2 seconds
Thread.Sleep(2000);
//dump a dowork() method onto the main thread
this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal, new Action(delegate()
{
doWork(sender);
}));
return;
}).Start();
}
private void doWork(object sender)
{
//if the button is still pressed
if ((sender as UIElement).IsMouseOver && Mouse.LeftButton == MouseButtonState.Pressed)
{
//continue here
}
}
it will check a mouse button is pressed and then check again after 2 seconds without stalling the main app thread. I wou't check if the button was pressed the entire time so this may or may not be important to you
Shane
You can run timer for 2 seconds under MouseDown event and on timers tick event check what you need. Afer that you can stop your timer.
DispatcherTimer PopupTimer = new DispatcherTimer();
PopupTimer.Tick += new EventHandler(PopupTimerTick);
PopupTimer.Interval = new TimeSpan(0, 0, 0, 0,5);
private void PopupTimerTick(object sender, EventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
// If still pressed showing popup
((Storyboard)Resources["ShowPopup"]).Begin();
PopupTimer.Stop();
}
}
private void ImageOnMouseDown(object sender, MouseButtonEventArgs e)
{
PopupTimer.Start();
e.Handled = true;
}
private void ImageOnMouseUp(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
if (Popup.IsOpen == false)
{
((Storyboard)Resources["ShowPopup"]).Stop();
// Here the operation that works on the click
}
}