Getting Tap even of user control in viewmodel - windows-phone-7

In my application i am using a content control to display my user control
<ContentControl Content="{Binding LayoutControl}" Grid.Row="0" Height="700" Width="450"/>
In here the binding content is a user control with the following elements.
<Grid x:Name="LayoutRoot" Background="#6C7172">
<Border BorderBrush="White" BorderThickness="7">
<Image Source="{Binding ImagePath0}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Tap">
<cmd:EventToCommand Command="{Binding Path=ImageTap}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
</Border>
</Grid>
In the above code what i am tried to implement is, while tapping on the image i need to fire the corresponding event in my view model, But through this code the tap event is not firing. But when i apply the interactivity to content control the click is working. I cannot simply add tap event to content control because some other cases i need to handle tap events of more than two Image controls . Can any one help me to solve this issue.

Give a fixed height and width to you image control and then try. It will work. If there is no image in the container means, the mouse will not get a position for tapping. Try this and if you still face the same issue give me a feedback.

Related

How to recognize swipe gestures over a TableView in .NET MAUI?

I want to allow users to close a page by swiping down.
The page consists of a Grid with a TableView inside of it.
If I add the SwipeGestureRecognizer to the Grid, it works as expected, as long as I start swiping on the grid itself (i.e. outside of the TableView).
But if I start swiping over the table, nothing happens, even if I add the same type of gesture recognizer to the TableView.
I'm guessing it has something to do with the scrollability of the TableView.
Is there any way to make a "quick swipe" down trigger the command to close the page, while still allowing the user to scroll the table?
If not, is there a way to disable the scrollability of the table and instead have the gesture recognizer pick up the swipe?
<Grid>
<Grid.GestureRecognizers>
<SwipeGestureRecognizer Direction="Down" Command="{Binding Source={RelativeSource AncestorType={x:Type pages:DetailsPage}}, Path=BackCommand}" />
</Grid.GestureRecognizers>
<TableView>
<TableView.GestureRecognizers>
<SwipeGestureRecognizer Direction="Down" Command="{Binding Source={RelativeSource AncestorType={x:Type pages:DetailsPage}}, Path=BackCommand}" />
</TableView.GestureRecognizers>
<TableRoot>
<TableSection Title="Type">
<TextCell Text="Manufacturer" Detail="{Binding Manufacturer}" />
<TextCell Text="Model" Detail="{Binding Model}" />
</TableSection>
</TableRoot>
</TableView>
</Grid>

Xamarin Picker add TagGesture

I'd like to add a tap gesture to the Picker - so when a value is selected (double tapped) I can call a method. I have tried the following. But it never fires! Any ideas?
<Picker Grid.Row="5" Margin="0,0,100,0" Title=" -Select State" SelectedIndex="{Binding StatesSelectedIndex, Mode=TwoWay}" ItemsSource="{Binding PPStates}" ItemDisplayBinding="{Binding Path=[display_name]}">
<Picker.GestureRecognizers>
<TapGestureRecognizer
Command="{Binding OnTapPicker}"
NumberOfTapsRequired="1" />
</Picker.GestureRecognizers>
</Picker>
I have tired the sample code in
https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/gestures/tap/
By adding this Tapped="OnTapGestureRecognizerTapped"
I don't know how you could do that in the way you desire, but you could fire the selectedindexchanged. If I'm right this should fire when the user selected one of the 50 states. Here you have some documentation about the picker.
https://developer.xamarin.com/api/type/Xamarin.Forms.Picker
If you want to use a picker that is bindable you should use this:
https://forums.xamarin.com/discussion/30801/xamarin-forms-bindable-picker
let me know if this helped you.
Edit:
Pickers are bindable by default nowadays

Apply animation inside DataTemplate

I have a listbox with the following structure. Data displays well and no issues there. I want to get some animation affect for the StackPanel, but since it is inside the DataTemplate, I am not able to use Blend to get my desired animation affect.
I guess I can write an event handler and code for that, but is that the best approach of achieving animation for ListBox items?
<ListBox Height="600" ItemsSource="{StaticResource learn}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Height="100" Orientation="Horizontal">
<TextBlock Width="0" Text="{Binding ID}" />
<Image Height="100" Width="100"/>
<StackPanel Orientation="Vertical" Width="319" VerticalAlignment="Center">
<TextBlock TextWrapping="Wrap" Text="{Binding Text}" Margin="6,0,0,0" FontSize="29.333" />
<TextBlock TextWrapping="Wrap" Text="{Binding Description}" Margin="6,0,0,0" FontSize="16"/>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Okay, all you need to do is to create a new user control and add all your animations to that user control and start animation where you want. In the list box data template, create an instance of that data template.
Steps to reproduce:
Create a new User Control in the project.
Give in the animations to the animations to that user control in blend.
Add the user control in data template.
Start the animations where ever you want.
Alternative: Without creating a new user control you can specify animations by creating storyboard and applying required transformations. this gives a common animation to all the list box items.

ListboxItems containing image - memory issues

I have a design issue with my listbox on my view.
Currently it has following DataTemplate as ItemTemplate:
<DataTemplate x:Key="MovieItemTemplate">
<StackPanel>
<Border BorderBrush="{StaticResource PhoneForegroundBrush}" BorderThickness="5" Margin="3" Height="215" Width="140">
<Image x:Name="MovieCover"
toolkit:TiltEffect.IsTiltEnabled="True"
Margin="0"
HorizontalAlignment="Center"
Width="140"
Height="210">
<Image.Source>
<BitmapImage UriSource="{Binding Cover}" CreateOptions="BackgroundCreation"/>
</Image.Source>
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Tap">
<gsextra:EventToCommand Command="{Binding MainViewModel.MovieItemSelectedCommand, Source={StaticResource Locator}}"
PassEventArgsToCommand="True"
CommandParameter="{Binding MovieID}"
></gsextra:EventToCommand>
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
</Image>
</Border>
</StackPanel>
</DataTemplate>
So as you can see, I have an image inside it that will download his content from the internet through an URI. Thanks to the new mango option BackgroundCreation ( cfr. http://blogs.msdn.com/b/slmperf/archive/2011/06/13/off-thread-decoding-of-images-on-mango-how-it-impacts-you-application.aspx ) it load in background.
But I have a very large collection and even though I do 'paged' binding of the Listbox Source, I notice my memory keeps on going up, until it has consumed everything and the app crashes.
Then, I noticed this http://blog.wpfwonderland.com/2011/01/17/images-and-memory-leaks-in-windows-phone-7/ so it would seem I need to cleanup the image itself, because of the image caching feature in wp7.
When I do this, everything works great in reference to memory, BUT now each time the user 'pages' through the listbox the images need to be redownloaded, resulting in an app that is almost not usable... because the user keeps on waiting for those images.
Any tips/tricks on how to go about this?
I also tried the DefferedLoadListBox

Windows Phone 7 Linking to events inside DataTemplates

I have a listbox where I create a Itemtemplate with a DataTemplate. I want to be able to write events for the checkboxes and buttons in the datatemplate but they do not seem to be firing.
Here is my xaml and basically I just tried to display a messagebox.show("worked") in the event function.
<ListBox x:Name="ListBox_Items" Margin="0,91,0,8" Foreground="#FF4BE5DB">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="700">
<CheckBox IsChecked="{Binding needPurchase}" Click="NeedPurchase_Click" Name="CheckBox_NeedPurchase"/>
<CheckBox IsChecked="False" Name="InCart"/>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding name}"/>
<TextBlock Text="{Binding storeLocation}"/>
</StackPanel>
<Button HorizontalAlignment="Right" Content="DELETE" Click="Button_Click" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Because the items are defined within a DataTemplate they are not hooked up to the code-behind for the parent class. If you want to handle events for templated items, then you should consider using commands instead. If you don't know what commands are (and therefore unlikely to know what MVVM is), then you should check out an explanation like this by Jeremy Likness.
I agree that using commands is the best approach.
However if you still want to assess controls placed inside the ItemTemplate/DataTemplate (and subscribe to some events), then you can do this by using the VisualTreeHelper.
For starters you need to remove the name from all the controls in the template. If you have 10 items in the list you will have 10 sets of controls with the same name which won't work.

Resources