DatePicker crashing my application ( setBinding)

Hello, I got a DatePicker on my Edit/Create item page and the page is crashing everytime I open it.

I've got a model that has a Date on string type and I want to show it as a DatePicker. Also when I'm editing an object I want to be able to edit it by picking a DateTime on my DatePicker and convert it to string .

I've made already the Converter Class:

public class DateTimeToStringConverter : IValueConverter
{
    public object Convert (object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = value as string;

        if (s == null) {
            throw new NotImplementedException ();
        }
        else {
            DateTime myDate = DateTime.ParseExact (s, "dd-MM-yyyy",System.Globalization.CultureInfo.InvariantCulture);
            return myDate;
        }

    }

    public object ConvertBack (object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        var s = (DateTime)value;

        if (s == null) {
            throw new NotImplementedException ();
        }
        else {
            string ret = s.Date.ToString ();
            return ret;
        }
    }
}

And this is the way I set the binding to the button:

        var dateLabel = new Label { Text = "Date" };
        var dateEntry = new DatePicker ();
        dateEntry.SetBinding (Entry.TextProperty, new Binding ("Date", BindingMode.TwoWay, new DateTimeToStringConverter (), null));

For some reason its crashing my application when I try to open that page. Anyone could give me an help ?
Thanks in advance

Posts

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    It certainly works with a DateTime in your view model. So you could add a DateTime property and convert it later.

    DateTime is a struct and no class. This means that s will never be null in your ConvertBack method.

    You may also check if the BindingContext in your DatePicker is set correctly.

    Did you set breakpoints in your DateTimeToStringConverter? Are they hit and is value set to what you expect in both methods?

  • JooAlmeida.9370JooAlmeida.9370 PTMember
    edited April 2015

    @MichaelRumpler , im not using DateTime instead of string on my view model because i'm using SQLite to save that, and it would be much more easy if everythings stands as strings/ints.

    I didnt set any BindingContext to my DatePicker , I've done the same I did with my all other entries. Create the button and just setBinding method . Its my last code on the first post.

    I've set some breakpoints and looks like the Converters are returning as supposed.

  • MichaelRumplerMichaelRumpler ATMember ✭✭✭✭✭

    Then you could also try to add a try/catch around the creation of the page which fails and write the exception to the debug window. Maybe you'll see the reason then.

    try
    {
        // create your page
    }
    catch(Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex);
    }
    
  • JooAlmeida.9370JooAlmeida.9370 PTMember

    I found out the error, it was on the setBinding function for the Datetime. I copy pasted the code from the previous labels and forgot to change the Entry.TextProperty to DatePicker.DateProperty.

    dateEntry.SetBinding (DatePicker.DateProperty, new Binding ("Date", BindingMode.TwoWay, new DateTimeToStringConverter (), null));

Sign In or Register to comment.