eventtocommand ManipulationCompleted - c#

I'm having a go at writing a small media player for my Windows Phone (more for learning purposes if anything), and am using mvvm-light.
When a video is playing, I have a timer that periodically updates the slider position (and ensures that it's in sync). When I slide the slider around, I pause the timer, so it doesn't skip about.
I use the ManipulationStarted event to pause the timer, and ManipulationComplete to resume it.
I've tried using EventToCommand, but only the ManipulationStarted event fires. When I lift my finger off the phone, ManipulationComplete doesn't fire.
This is my xaml :
<Slider x:Name="PlaybackSlider" HorizontalAlignment="Stretch" Margin="20,0" VerticalAlignment="Bottom" Maximum="{Binding MaxSteps}" Value="{Binding Offset, Mode=TwoWay}" Style="{StaticResource SliderStyle1}" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="ManipulationStarted">
<Command:EventToCommand Command="{Binding PlayOffsetBegin}"/>
</i:EventTrigger>
<i:EventTrigger EventName="ManipulationCompleted">
<Command:EventToCommand Command="{Binding PlayOffsetEnd}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseMove">
<Command:EventToCommand Command="{Binding PlayOffsetUpdate}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider>
The bindings are relay commands.
If I implement it like this, it all works fine :
<Slider x:Name="PlaybackSlider"
ManipulationStarted="PlaybackSlider_ManipulationStarted"
ManipulationCompleted="PlaybackSlider_ManipulationCompleted"
HorizontalAlignment="Stretch" Margin="20,0" VerticalAlignment="Bottom"
Maximum="{Binding MaxSteps}" Value="{Binding Offset, Mode=TwoWay}"/>

Related

Creating audio player on WPF using NAudio. Binding doesn't work

I am creating my audio player on WPF using NAudio.
I am adding a slider that will be scroll the song.
XAML:
<Slider Height="30"
Value="{Binding Path=MediaReader.Position, Mode=TwoWay}"
Maximum="{Binding Path=MediaReader.Length, Mode=OneWay}"/>
Note. MediaReader - it's a property that returns the object of type MediaFoundationReader:
MediaFoundationReader mediaReader;
public MediaFoundationReader MediaReader => mediaReader;
Problem: while the song is plaing the slider property Value doesn't change! But by scrolling the thumb of the slider the property Position of the MediaReader changes.
Why does it work so and how can I solve that?
Just take a look to this tutorial:
https://www.pluralsight.com/guides/building-a-wpf-media-player-using-naudio
The author uses also a slider control and binds the current track position, e.g.:
<Slider Grid.Column="0" Minimum="0" Maximum="{Binding CurrentTrackLenght, Mode=OneWay}" Value="{Binding CurrentTrackPosition, Mode=TwoWay}" x:Name="SeekbarControl" VerticalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDown">
<i:InvokeCommandAction Command="{Binding TrackControlMouseDownCommand}"></i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseUp">
<i:InvokeCommandAction Command="{Binding TrackControlMouseUpCommand}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Slider>
public double CurrentTrackPosition
{
get { return _currentTrackPosition; }
set
{
if (value.Equals(_currentTrackPosition)) return;
_currentTrackPosition = value;
OnPropertyChanged(nameof(CurrentTrackPosition));
}
}
I think you can get the idea behind this...

MVVM How bind MouseLeftButtonDown and MouseLeftButtonUp in Canvas

Im trying to add new child(circle) to my Canvas after user click on it. And i want to change position of circles while draging them.
I want to do it with MVVM but dont know how catch these events in ViewModel.
I need a MouseLeftButtonUp/Down
I've tried wih System.Windows.Interactivity but it didnt helped.
My only idea is to add invisible button and do easy binding Command="{Binding Method}", but it still wont solve my problem, and certainly its not a best option
This in my XAML code (part of it)
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<Canvas x:Name="DrawableCanvas" HorizontalAlignment="Left" Height="{Binding ElementName=GridContainer, Path=ActualHeight}" VerticalAlignment="Top" Width="{Binding ElementName=GridContainer, Path=ActualWidth}" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding CanvasActionCommand}" x:Name="interactifityFix2">
</i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Canvas>
Add this namespace in your XAML:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
then you can add an interaction trigger wich will merge the event with an icommand in your viemodel like so:
<Ellipse>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDown">
<i:InvokeCommandAction Command="{Binding MouseDownCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Ellipse>

WPF Grid: MouseUp event is not fired after mouse move

have discovered that the Mouse up work properly most of the time, they just do not get fired when my mouse has begun moving after the initial click. I have a pretty heavy mouse move event. So the only conclusion i can draw is that the mouse move event is some how preventing the mouse button up event from firing. Any thoughts please.
Note: I am using MVVM model.
<Grid >
<Image x:Name="DynamicJoystickWindow" RenderTransformOrigin="0.5,0.5">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseUp" >
<cmd:EventToCommand Command="{Binding JoystickMouseUp_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<!--<i:EventTrigger EventName="MouseUp" >
<cmd:EventToCommand Command="{Binding JoystickMouseUp_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>-->
<i:EventTrigger EventName="PreviewMouseDown">
<cmd:EventToCommand Command="{Binding JoystickMouseDown_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseMove" >
<cmd:EventToCommand Command="{Binding JoystickMouseMove_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<!--<i:EventTrigger EventName="MouseMove" >
<cmd:EventToCommand Command="{Binding JoystickMouseMove_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>-->
</i:Interaction.Triggers>
<Image.RenderTransform>
<ScaleTransform ScaleX="{Binding RenderScaleTransform}" ScaleY="{Binding RenderScaleTransform}"/>
</Image.RenderTransform>
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="Resources/transparent.png"/>
<Setter Property="Opacity" Value="0.3" />
</Style>
</Image.Style>
</Image>
</Grid>
I do not observe the behavior you describe, so I would suggest you to simplify your problem to understand what's going on.
For example, remove your image and work only with the grid to see if something change.
I used the following code and it works well for me:
<Grid x:Name="DynamicJoystickWindow" RenderTransformOrigin="0.5,0.5" Background="Blue">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseUp" >
<command:EventToCommand Command="{Binding JoystickMouseUp_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseDown">
<command:EventToCommand Command="{Binding JoystickMouseDown_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="PreviewMouseMove" >
<command:EventToCommand Command="{Binding JoystickMouseMove_Dynamic}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Grid>
The only case where event is not fired is when I finish my move outside of the grid.
Hope it helps.
Thanks guys. Problem is, I had the image size smaller than the grid. as #Ouarzy suggested, had grid only and tried. the mouse events works as expected.

Button inside the GridSplitter not working WPF MVVM

I put the button inside the gridsplitter. my intention is to make clickable gridsplitter as well as slideable. my problem is after putting button inside the grid splitter, totally cannot drag by mouse. how can i configure the grid splitter to clickable and slideable.
<GridSplitter BorderThickness="1" HorizontalAlignment="Stretch" Grid.Column="1" >
<GridSplitter.Template>
<ControlTemplate TargetType="{x:Type GridSplitter}">
<Grid>
<Button Name="btnSplit" Content="⁞" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding SplitterClickCommand}" CommandParameter="{Binding ElementName=btnSplit}" ></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</Grid>
</ControlTemplate>
</GridSplitter.Template>
</GridSplitter>
Best Rgds
df
Try to subscribe MouseDoubleClick event on GridSplitter:
<GridSplitter BorderThickness="1" HorizontalAlignment="Stretch" Grid.Column="1" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding SplitterClickCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</GridSplitter>
If MouseDoubleClick event is not suitable for you, you can try to subscribe to MouseDown or MouseLeftButtonDown.

Binding toggle button to two commands

I have a toggle button in my silverlight application as follows:
<ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}" />
The toggle currently is changing visual states on toggle and nothing more.
However, I need to bind each of the toggle with a different command. For eg: On button press1, run command1 and on button press again, run command2.
How can this be done?
Use Triggers for such purposes:
<ToggleButton Content="ToggleButton" Margin="0,30,0,0" Style="{StaticResource ChkToggleButton}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding FirstCommand}"
CommandParameter="{Binding SomeParameter}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding SecondCommand}"
CommandParameter="{Binding AnotherParameter}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ToggleButton>
If you don't have Interactivity you can get it by installing Expression Blend SDK or as a NuGet package.
use a single command and also keep track of the toggle state.
Have a viewmodel (preferably, or some codebehind) and let it use these two inputs to decide what actually needs to be done.

Categories