Windows Store apps with C# and relative mouse movement

Some times we want use the mouse as a more general input device. For example, a 3-D modeler might use mouse input to orient a 3-D object by simulating a virtual trackball; or a game might use the mouse to change the direction of the viewing camera via mouse-look controls.

In WinRT  there is Pointer instead mouse because we need to think in different devices : mouse, pen/stylus and touch.

In CoreWindow there are pointer events like PointerMoved, PointerPressed, PointerReleased, etc. You can handle PointerMoved event and track de Pointer. The problem is that, in PointerEventArgs, you have the absolute position of pointer in the screen.

To accomplish our goal we need relative mouse position. MouseMoved event is good choice. MouseEventArgs has MouseDelta propertie that gets a value that indicates the change in the screen location of the mouse pointer since the last mouse event.


Windows.Devices.Input.MouseDevice.GetForCurrentView().MouseMoved += Page_MouseMoved;

So,  next step is to hide the mouse pointer to simulate a camera moving around the object.


Window.Current.CoreWindow.PointerCursor = null;

Caution, now you don’t have mouse pointer and it cannot invoke edge UI such as the charms, back stack, or app bar. Therefore, it is important to provide a mechanism to exit this particular mode, such as the commonly used Esc key.

See an example:


private void Page_Loaded(object sender, RoutedEventArgs e)

{

Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;

ActivateMouseLook();

 }

private void Page_Unloaded(object sender, RoutedEventArgs e)
{

Window.Current.CoreWindow.KeyDown -= CoreWindow_KeyDown;
DeactivateMouseLook();
}

private void ActivateMouseLook()

{

Windows.Devices.Input.MouseDevice.GetForCurrentView().MouseMoved += Page_MouseMoved;

_baseCursor = Window.Current.CoreWindow.PointerCursor;

Window.Current.CoreWindow.PointerCursor = null;

}

private void DeactivateMouseLook()

{

Window.Current.CoreWindow.PointerCursor = _baseCursor;

Windows.Devices.Input.MouseDevice.GetForCurrentView().MouseMoved -= Page_MouseMoved;                                             }

}

void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if (args.VirtualKey == VirtualKey.Escape)
  {
DeactivateMouseLook();
}
}

Windows Phone y enlaces bidireccionales y pérdida de foco

En Windows Phone a los enlaces bidireccionales existe solo dos formas de actualizar la fuente: Default y Explicit

Aquí tienes la información del MSDN : UpdateSourceTrigger Enumeration

En realidad no hay ningún problema ya que el perder el foco ya funciona correctamente. Lo que pasa es que en Windows Phone la application bar no genera perdida de foco y en el caso de que estuvieses escribiendo en un textbox el cambio no se propaga a la fuente.

Así, se puede dar el caso siguiente:

Tienes una página donde se pide escribir un texto y en la application bar un botón que al pulsar usa este texto para consultar. Lo que el usuario hará será pulsar en textbox, luego le aparece el teclado del Windows Phone, escribe y luego pulsa directamente en el botón de la application bar.

El comando se ejecuta pero la propiedad enlazada al textbox no se actualiza porque no ha habido perdida de foco.

Así que lo que tenemos que hacer es provocar nosotros mismos que se actualice. Esto es lo que escribiremos en el método Click del botón de la application bar.

object focusObj = FocusManager.GetFocusedElement();
if (focusObj != null && focusObj is TextBox)
{
var binding = (focusObj as TextBox).GetBindingExpression(TextBox.TextProperty);
  binding.UpdateSource();
}

Más información:

TextBox Binding TwoWay Doesn’t Update Until Focus Lost WP7

Original solution : Non-string parameters between pages in Windows Phone

My last post was about an original solution using Dependency properties. This time another original solution passing non-string parameters between pages in Windows Phone.

In WPF or Windows Runtime (aka Windows Store apps) you can pass parameters between pages as objects and this means that you don’t have any problem to pass parameters. But, in Windows Phone only parameters as string is possible. For example:


NavigationService.Navigate(new Uri("yourpage.xaml?parameter1=value1&parameter2=value2,UriKind.Absolute));

As you can see, navigate in Windows Phone is like web pages. Only using Uri you can pass parameters, and this parameters need to be in string format.

Yes, this is a problem, because in some cases, we are using complex objects structures in a page and we would like to pass this same complex structure to another page.

A solution is to serialize the object with DataContract attribute. But, the problem is, no all object can be serialized and the Uri is length limited.

An original solution that I found on internet is using an extension for NavigationService and a static field to save the object to pass to another page. Look an example:


public static class NavigationExtensions {

private static object _navigationData = null;

public static void Navigate(this NavigationService service, string page, object data)

{

_navigationData = data;

service.Navigate(new Uri(page, UriKind.Relative));

}

public static object GetLastNavigationData(this NavigationService service)

{

object data = _navigationData;

_navigationData = null;

return data;

}

}

Simply clever, awesome.

Then, you can call on the source page


NavigationService.Navigate("mypage.xaml", myParameter);

And on the target page in the OnNavigatedTo


var myParameter = NavigationService.GetLastNavigationData();

Source:

How do I pass non-string parameters between pages in windows phone 8?