question

TonyLe-6777 avatar image
0 Votes"
TonyLe-6777 asked RobCaplan edited

Xamarin Form CollectionView ItemTemplate : How to send a command from an Image.GestureRecognizers inside ContentView

Hi there,

I have a complex UI with collectionview as below and I would like to send a command from Image.GestureRecognizers inside ContentView to ViewModel of the screen which contains collectionview, but it doesn't work. ViewModel doesn't receive the command.

I also try below, but it doesn't work. It throws exception right after accept that screen.
Command="{Binding BindingContext.Command, Source={x:Reference Name=SampleViewRef}}"

Exception:
System.Reflection.TargetInvocationException
Message=Exception has been thrown by the target of an invocation.

107135-collectionview.png

107181-contentview.png




Regards,
Tony

dotnet-xamarin
collectionview.png (35.7 KiB)
contentview.png (47.6 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, @TonyLe-6777 How do you send the command to the ViewModel? Do you assign the function code to the command of the viewModel in the image's gesture event? Please make sure the 'viewModel' instance is the same as the bindingContext you set for the collectionView page.

You could create a golbal static parameter in the App class and then get the instance in the pages.

public partial class App : Application
{
    public static TestViewModel viewModel;
    public App()
    {
        InitializeComponent();

        viewModel = new TestViewModel();
        MainPage = new NavigationPage(new TestPage());
    }
}

//assign value to the command

public partial class TestPage : ContentPage
{
    public TestPage()
    {
        InitializeComponent();
        BindingContext = App.viewModel;
    }
}
0 Votes 0 ·

0 Answers