Scrolling a ScrollViewer without steps/gaps when using the mouse - xaml

Using mouse interaction, when dragging the ScrollBar of a ScrollViewer horizontally, the content of the ScrollViewer is scrolled in small steps. If the app is run with touch interaction scrolling is smooth without any steps / gaps. How can I scroll using the mouse without steps / gaps as it is done with touch interaction?
Desktop:
Mouse:

A workaround is to hide the ScrollBar of the ScrollViewer and use the click and drag mouse events directly. WaveformScroller is the ScrollViewer. WaveformCanvas is the scrolled control.
Vector2 lastDragPoint;
bool pointerPressed;
WaveformCanvas.PointerPressed += (sender, args) =>
{
WaveformCanvas.CapturePointer(args.Pointer);
lastDragPoint = args.GetCurrentPoint((UIElement)sender).Position.ToVector2();
pointerPressed = true;
};
WaveformCanvas.PointerMoved += (sender, args) =>
{
if (pointerPressed)
{
var newDragPoint = args.GetCurrentPoint((UIElement)sender).Position.ToVector2();
var deltaX = newDragPoint.X - lastDragPoint.X;
var newOffset = WaveformScroller.HorizontalOffset - deltaX;
WaveformScroller.HorizontalOffset = WaveformScroller.HorizontalOffset - deltaX;
}
};
WaveformCanvas.PointerReleased += (sender, args) =>
{
pointerPressed = false;
WaveformCanvas.ReleasePointerCapture(args.Pointer);
};

Related

(UWP) How to activate the "Taskbar Miniplayer" like in Groove

I use the BackgroundMediaPlayer for my App to play Audio in the Background. Now i see these buttons:
How can i activate them?
In order to make the media controls from the taskbar to work, you need to load and configure the SystemMediaTransportControls from the foreground application AND the background task. If you are doing it only from the background task, the controls will be displayed but they will remain disabled.
In your foreground application, you should have the following code:
var smtc = SystemMediaTransportControls.GetForCurrentView();
smtc.ButtonPressed += smtc_ButtonPressed;
smtc.PropertyChanged += smtc_PropertyChanged;
smtc.IsEnabled = true;
smtc.IsPauseEnabled = true;
smtc.IsPlayEnabled = true;
smtc.IsNextEnabled = true;
smtc.IsPreviousEnabled = true;
And in the background task, you should have :
smtc = BackgroundMediaPlayer.Current.SystemMediaTransportControls;
smtc.ButtonPressed += smtc_ButtonPressed;
smtc.PropertyChanged += smtc_PropertyChanged;
smtc.IsEnabled = true;
smtc.IsPauseEnabled = true;
smtc.IsPlayEnabled = true;
smtc.IsNextEnabled = true;
smtc.IsPreviousEnabled = true;
Beware that the API to get the control instance is not the same:
SystemMediaTransportControls.GetForCurrentView()
in the foreground app and BackgroundMediaPlayer.Current.SystemMediaTransportControls in the background task.
You will have to support the button pressed event in the two (foreground + background)
That's System Media Transport Controls and you should add code to handle click event.
Here is official sample:
public MainPage()
{
this.InitializeComponent();
// Hook up app to system transport controls.
systemMediaControls = SystemMediaTransportControls.GetForCurrentView();
systemMediaControls.ButtonPressed += SystemControls_ButtonPressed;
// Register to handle the following system transpot control buttons.
systemMediaControls.IsPlayEnabled = true;
systemMediaControls.IsPauseEnabled = true;
}
async void SystemControls_ButtonPressed(SystemMediaTransportControls sender,
SystemMediaTransportControlsButtonPressedEventArgs args)
{
switch (args.Button)
{
case SystemMediaTransportControlsButton.Play:
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaElement.Play();
});
break;
case SystemMediaTransportControlsButton.Pause:
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
mediaElement.Pause();
});
break;
default:
break;
}
}

Add a pushpin on Windows phone 8 map

does anyone know how to put a pushpin on Windows Phone 8 maps API?
I find only code to add a pushpin to a bing map..
I prefer only XAML code, if possible.
Thanks
This is what I use.. the commented out code will give you a round marker, the default pushpin is squarish..
<Grid x:Name="ContentPanel" Margin="12,0,12,0">
<maps:Map x:Name="MapControl">
</maps:Map>
</Grid>
private void DisplayMapPoint()
{
MapControl.Layers.Clear();
var MyGeo = new GeoCoordinate(_Latitude, _Longitude);
MapControl.Center = MyGeo;
MapControl.ZoomLevel = 14;
DrawMapMarker();
}
private void DrawMapMarker()
{
var Overlay = new MapOverlay
{
GeoCoordinate = MapControl.Center,
//Content = new Ellipse
//{
// Fill = new SolidColorBrush(Colors.Red),
// Width = 40,
// Height = 40
//}
Content = new Pushpin
{
GeoCoordinate = MapControl.Center,
Background = new SolidColorBrush(Colors.Red),
Content = _SiteName
}
};
var Layer = new MapLayer {Overlay};
MapControl.Layers.Add(Layer);
}
This link may also help:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207037(v=vs.105).aspx

Detect which child control received a Pointer* event

I have a container control that is handling PointerPressed and PointerMoved events.
The container contains a set of buttons.
At the point of handling the event, how can I determine which button actually received it?
mainPage.AddHandler(PointerPressedEvent, new PointerEventHandler(pointerPressedHandler), true);
private void pointerPressedHandler(object sender, PointerRoutedEventArgs e)
{
var p = e.GetCurrentPoint(null); // maybe can be done using position info?
var s = e.OriginalSource as Border; // OriginalSource is a Border, not the Button, and I don't seem to be able to get to the Button from the Border
// todo - determine which button was clicked
}
This works:
private void pointerPressedHandler(object sender, PointerRoutedEventArgs e)
{
var p = e.GetCurrentPoint(null);
var elements = VisualTreeHelper.FindElementsInHostCoordinates(p.Position, mainPage, false);
Button foundButton;
foreach (var item in elements)
{
foundButton = item as Button;
if (foundButton != null)
{
System.Diagnostics.Debug.WriteLine("found button: " + foundButton.Name);
}
}
}

How to prevent/disable snapped view for Windows 8 Metro UI applications

I have an application that really makes no sense for snapped view, as the content generally represents A4 pages of information and data collection.
Is there a way to disable snapped view for the application, or is the recommended approach to just show the application icon, while in snapped mode?
You cannot disable snapped view.
Simply just create a new page and navigate to that page if a snap event occurred. You can simply display an image.
Window.Current.SizeChanged += (object sender, Windows.UI.Core.WindowSizeChangedEventArgs e) =>
{
ApplicationViewState myViewState = ApplicationView.Value;
if (myViewState == ApplicationViewState.Snapped)
{
//await SaveAssets();
this.Frame.Navigate(typeof(Snapped));
Snapped = true;
}
else if (myViewState != ApplicationViewState.Snapped)
{
if (Snapped)
{
this.Frame.Navigate(typeof(MainPage));
Snapped = false;
}
}
};
I really like Robert's solution. Based upon that, you can simulate disabling snapped view:
Window.Current.SizeChanged += async (object sender, WindowSizeChangedEventArgs e) =>
{
ApplicationViewState myViewState = ApplicationView.Value;
if (myViewState == ApplicationViewState.Snapped)
{
if (!ApplicationView.TryUnsnap())
{
MessageDialog dialog = new MessageDialog("Sorry, this App is unusable in snapped mode.");
await dialog.ShowAsync();
}
}
};

Opening context menu just above item in win 8 metro app

I have a list of items defined in a ListView. When user click or tap any item, I want to show a PopupMenu just above the selected Item. How should I position the PopupMenu?
varmenu =newPopupMenu();
menu.Commands.Add(
newUICommand("Remove", (x) =>
{...
// Create the message dialog and set its content
}, 1));
var chosenCommand =awaitmenu.ShowForSelectionAsync(GetElementRect((FrameworkElement)sender));
Rect GetElementRect(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null);
Pointpoint = buttonTransform.TransformPoint(newPoint());
returnnewRect(point,newSize(element.ActualWidth, element.ActualHeight));
}
This is the code I've been using to open up a popup menu above a button in an app bar.
private async void OnOpenMenu(object sender, RoutedEventArgs e)
{
var button = (Button)sender;
var transform = button.TransformToVisual(this);
var point = transform.TransformPoint(new Point(0, 0));
var menu = new PopupMenu();
menu.Commands.Add(new UICommand("Menu Item 1"));
menu.Commands.Add(new UICommand("Menu Item 2"));
await menu.ShowAsync(point);
}