passing an object in the constructor to a clicked event

JenniferTruongJenniferTruong USMember, University

I'm pretty new to Xamarin and recently start trying to make an App using the Xamarin.Forms

XAML:

<Button Text="Quick Links" BackgroundColor="#99CCFF" Grid.Column = "2" Grid.ColumnSpan="2" TextColor="White" Clicked="OnQuickLinksButtonClicked" Command="{Binding Click}"/>

**Code Behind:
**
public partial class ProfileView : ContentPage
{
Patient globalPatient;
public ProfileView (Patient patient)
{
InitializeComponent ();
BindingContext = new ProfileViewModel (patient);
globalPatient = new Patient();
globalPatient = patient;
}

       async void OnQuickLinksButtonClicked(object sender, EventArgs args)
       {
           await Navigation.PushAsync (new BarCodeView (globalPatient.Name)); 
        }
    }

My question is that how can I pass the patient object to the OnQuickLinksButtonClicked? I test the code above and the globalPatient.Name is empty.

Thanks!

Best Answer

Answers

  • LesterMeeksLesterMeeks USMember ✭✭

    Rather than using the Clicked event, you already are binding to the Command, you could bind what you need to the CommandParam. Unless I'm missing something, i don't think you need to be using the Clicked event and the Command. They are different ways to do the same thing.

    developer.xamarin.com/guides/cross-platform/xamarin-forms/xaml-for-xamarin-forms/data_bindings_to_MVVM/

  • JenniferTruongJenniferTruong USMember, University

    Is there anyway for me to do it with Clicked event?

  • ylemsoulylemsoul RUMember ✭✭✭

    Is patient's Name property empty when you hit breakpoint in constructor?

  • JodeAndrewsJodeAndrews GBMember
    edited July 2015

    Tested your example and it works for me, the name property from a patient object is accessible in the Clicked eventhandler, you won't need 'globalPatient = new Patient();'

    A few checks..

    1. If you breakpoint on InitializeComponent (); and inspect the 'patient' object from the ProfileView constructor is the name property correct there?

    2. Is the ProfileViewModel definitely not doing anything with the 'patient' reference in it's own constructor? Breakpoint on 'globalPatient = new Patient();' and inspect the 'patient' object again to check.

    3. The Click Command will always fire BEFORE the event in the code behind, does that command in the viewmodel alter the object's properties in any way? as that will be reflected in the object that the event uses.

    Aside from that LesterMeeks is quite right, and using Command and CommandParameter is a much nicer way to separate concerns in the long run, it's much more preferable to use one or the other (not both as it's a debugging nightmare) and usually the ViewModel Command in most cases, but yes this example should work and both the event and the command will fire, albeit the command first, so something elsewhere is probably a bit skewy :)

  • JenniferTruongJenniferTruong USMember, University

    Thanks for replying guys!

    @ylemsoul the patient object is passed into the constructor with all information/value such as name, phoneNum,dob, etc...

    @JodyAndrews
    1. Yes, the name property of the patient object is accessible but the value (ex: patient.Name = "John") is not being pass on to globalPatient object.
    2. Also, the ProfileViewModel was passed the patient object to become a List for binding and displaying in the XAML.
    3. To use Command, is there a simple example of how to use it?

  • stvansolanostvansolano UMInsider, University ✭✭✭

    You could combine the ICommand and also bind the Button's CommandParameter if necessary and you will get the argument (aka ViewModel)

Sign In or Register to comment.