Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Passing data from xaml to xaml.cs

$
0
0

I come from a asp.net background, so I have a bit of trouble understanding the whole MVVM idea.

I have a listview, which is filled with messages (it is a sort of forum app). Every message has a button, which, when pressed, should ask the user if they want to delete and then delete when it is confirmed.

I have tried using a Command, but then I get an error (Can you actually call commands to the xaml.cs instead of the ViewModel?) I also tried with an event and a commandparameter, but that also gave me errors.

With Event:
XAML(inside image)

              <Image.GestureRecognizers>
                    <TapGestureRecognizer Tapped="MessageClicked" CommandParameter="{Binding MessageId}"/>
              </Image.GestureRecognizers>

Code Behind

                async void MessageClicked(object sender, TappedEventArgs e)
                {
                    string action = "";
        #if __IOS__
                    action = await DisplayActionSheet("Message options", "Cancel", "Delete", "Edit");
        #else
                    action = await DisplayActionSheet("Message options", "Cancel", null, "Edit", "Delete");
        #endif
                    switch (action)
                    {
                        case "Delete":
                            if (await DisplayAlert("Delete message?", "Are you sure you want to delete this message?", "Yes", "No"))
                            {
                                DeleteMessage((int)(e.Parameter));
                            }
                            break;
                        case "Edit":
                            EditMessage((int)(e.Parameter));
                            break;
                    }
                }

Error: Signature (parameter 1) of EventHandler "XamarinTest.MainPage/_anonXamlCDataTemplate_0.MessageClicked" doesn't match the event type

With Command:
XAML (inside image)

    <Image.GestureRecognizers>
        <TapGestureRecognizer Command="MessageOptions" CommandParameter="{Binding MessageId}"/>
    </Image.GestureRecognizers>

Code behind:

    public Command<int> MessageOptions
            {
                get { return new Command<int>(i => MessageClickedCommand(i)); }
            }

            async void MessageClickedCommand(int id)
            {
                await DisplayAlert("Information", $"The id of this message is: {id}", "Ok");
            }

Error:
No property, bindable property, or event found for 'Command', or mismatching type between value and property.

In asp.net, I would just attach a data-attribute to the object calling the function, so that the function could retrieve the data itself.
HTML and Javascript would look like:

    <div class="functionDiv" data-id="1">
    </div>

    $(document).ready(function() { 
        $(".functionDiv").bind("click", function() {
        var $data = $(this).data("id");
        console.log($data);
      });
    });

I want to go to the backend first because there I can call Display functions.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>