Reputation: 32788
I have this code in my XAML:
<ViewCell Tapped="openPicker" >
In my back end C# for the XAML I have:
public partial class SettingsPage : ContentPage
{
public SettingsPage()
{
InitializeComponent();
BindingContext = new CommandViewModel();
}
void openPicker() {
var a = 99;
}
My commandViewModel is empty right now:
namespace Japanese
{
public class CommandViewModel: ObservableProperty
{
}
}
When I try to run this it tells me that:
Error: Position 78:85. No property, bindable property, or event found for 'Tapped' (Japanese)
Is it possible to do this in the C# or should I put code in a ViewModel. If so then what should the view model look like for this?
Upvotes: 1
Views: 283
Reputation: 34013
A Grid
does not have the Tapped
event directly.
You can use the GestureRecognizers for that, especially the TapGestureRecognizer
(read more here). Each VisualElement
has the GestureRecognizers collection.
In a Grid
, you can implement it like this:
<Grid>
<Grid.GestureRecognizers>
<TapGestureRecognizer
Tapped="OnTapGestureRecognizerTapped"
NumberOfTapsRequired="1" />
</Grid.GestureRecognizers>
</Grid>
Now you can notice when this element is being tapped. You could also choose to have it require more taps to be fired.
Besides working with an event, you can also choose to implement it with a Command
.
<TapGestureRecognizer Command="{Binding TapCommand}" />
Besides the tap, you also have built-in recognizers for pinch and pan, and probably more to come in the future.
Upvotes: 4