TapGestureRecognizer multiple CommandParameter

GelattoGelatto Member ✭✭

Hi Guys,

i try to send multiple strings within a tapgesturerecognizer event but i dont get it.
I also tried to send a JSON String as CommandParameter and Deserialize it afterwards in the code behind, but i cant escape the quotes in xaml.
Can someone help me please?

thank you in advance, Gelatto

Answers

  • GelattoGelatto Member ✭✭

    nobody?

  • SzbestaSzbesta Member ✭✭✭

    Could you post some XAML concerning the problem otherwise its just bling guessing

  • GelattoGelatto Member ✭✭
    edited June 2018

    i have something like this:

    <Image Source="{extension:EmbeddedImage placeholder125x125.png}">
        <Image.GestureRecognizers>
            <TapGestureRecognizer Tapped="OnImageTapped" CommandParameter="HERE I WANT TO PASS 2 STRINGS OR AN OBJECT, MAYBE A JSON STRING? " />
        </Image.GestureRecognizers>
    </Image>
    

    thank you @Szbesta

  • GelattoGelatto Member ✭✭

    somehow the element gets invisible...

  • NMackayNMackay GBInsider, University mod

    You can pass an object, that's no issue. You cannot pass multiple parameters. You should post some code or a sample of what your trying to achieve.

    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/gestures/tap

  • SzbestaSzbesta Member ✭✭✭
    edited June 2018

    @Gelatto
    Alright you need to use the Command Property instead of the Tapped event. Wire up the Command property with a command in your VM:

    <TapGestureRecognizer Command="{ Binding Source=x:Reference this}"
                                                Path=MyViewModel.MyCommandToBeExecuted}             
                           CommandParameter="Binding TheObjectYouWantToPassMightAsWellBeAJSONString"/>
    

    As far as i know you can't pass 2 Command parameters in XAML but its enough to pass an object that has all your info in it.
    Hope it helped

  • GelattoGelatto Member ✭✭

    @NMackay here's my code:

    In this commandparameter i want to send two strings to my "OnImageTapped"-Event.

    Afterwards i want to pass the two strings to the constructor of my imagepage.
    thanks

  • NMackayNMackay GBInsider, University mod
    edited June 2018

    Pass an object, not multiple strings.

    Here's one approach where the page's binding context is set.
    https://github.com/xamarin/xamarin-forms-samples/tree/master/Navigation/PassingData

    This article is worth a read too.
    https://xamarinhelp.com/passing-data-navigation-xamarin-forms/

    If your getting started with forms, I'd recommend giving this blog a read too.
    http://redpillxamarin.com/

  • GelattoGelatto Member ✭✭
    edited June 2018

    my first attempt was to pass an object without having the object existing in the code behind and without working with binding. My second attempt was to create a class member in the code behind:
    List<KeyValuePair<string,string>> myList = new List<KeyValuePair<string,string>>();
    Then in the constructor i added an item and set the list as bindingcontext of the page.
    myList.Add(new KeyValuePair<string,string>>("firstString", "secondString");
    BindingContext = myList;

    Then i passed it like so:
    Tapped="OnImageTapped" CommandParameter="{Binding myList[0]}"

    In the Debugger i see that myList[0] is the KeyValuePair i created in the xaml.cs, but the TappedEventArgs of my OnImageTapped-Event is null.

    Could someone of you guys please give me a full example how to pass an object ?

    @NMackay @Szbesta
    Thank you guys.

  • SzbestaSzbesta Member ✭✭✭

    @Gelatto

    In the Debugger i see that myList[0] is the KeyValuePair i created in the xaml.cs, but the TappedEventArgs of my OnImageTapped-Event is null.

    As i have said in my previous post you shouldnt use the Tapped event but rather the Command property. The Parameter declared in the CommandParameter Property as far as I know only deal with the Command passes in the Command property(sorry if that is a bit confusing). I also don't think you can do that -> {Binding MyList[0]

  • NMackayNMackay GBInsider, University mod

    @Szbesta said:
    @Gelatto

    In the Debugger i see that myList[0] is the KeyValuePair i created in the xaml.cs, but the TappedEventArgs of my OnImageTapped-Event is null.

    As i have said in my previous post you shouldnt use the Tapped event but rather the Command property. The Parameter declared in the CommandParameter Property as far as I know only deal with the Command passes in the Command property(sorry if that is a bit confusing). I also don't think you can do that -> {Binding MyList[0]

    You can actually bind to an index instance in a collection but why would you.... generally it's a warning of a misunderstanding of data binding, it will work though.

  • SzbestaSzbesta Member ✭✭✭

    @NMackay
    Interesting I did not know that, then again i never tried to do it so....

  • GelattoGelatto Member ✭✭

    You can actually bind to an index instance in a collection but why would you.... generally it's a warning of a misunderstanding of data binding, it will work though.

    Sorry for bothering both of you but i think you're right that i dont understand this whole databinding stuff.
    I can tell you what i am trying to achieve.

    I have a OverView Page of images. The Overview contains 4 different Images with different Captions. Every Image can be Tapped and will navigate into a ImageDetailPage. Every Image uses the same ImageDetailPage.
    Now i want to have a "Next" Button in the ImageDetailPage to navigate from one picture to the next without going back to the overview again. Therefore when i tap on an Image in the Overview, i need the information about what is my currentImage and what is the Image next in the row.

    Is databinding even the right method to solve my problem?
    @NMackay @Szbesta

  • NMackayNMackay GBInsider, University mod

    @Gelatto,

    Yes, 100%, data binding is the correct approach, if you have a bound object you can pass that to a navigation event via a behavior/event handler, tap gesture etc.

    I wrote this MVVM Light a while back, that shows passing data from one page to another. You don't need an MVVM framework though, you can do it with Forms on it's own.

    https://github.com/mackayn/MVVM-Light-Sample/blob/master/XamFormsTestApp/View/StartPage.xaml

Sign In or Register to comment.