Getting Picker selected item string to JSON

when using Condition = this.FindByName<Picker>("tyrePicker").SelectedItem.ToString() then saving it to a .json it is bring back this result appName.Condition instead of either yes,no.

Does anybody know why this is happening ?

Posts

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    Sounds normal. Many classes when sent ToString() give their type. You'd get the same behavior if you tried to do ListView.ToString()

    What is Condition? Is it a class of your design? An enum?

    Better question... Why are you having to play the game of "Find by name" to get the selected value of something? Don't you have the picker binded to a property on your ViewModel? Having to hunt through your UI like that to find a value is just... well... I don't even know a word for it. 1995-like.... Web-like...

  • jenova1628jenova1628 Member ✭✭
    edited November 2018

    @ClintStLaurent condition is currently public string Condition { get; set; } when then Condition = this.FindByName<Picker>("mirrorPicker").SelectedItem.ToString() is in list.

    my picker has a x:Name if that is what you mean or the pickers item source is set to {Binding ConditionList} ? Xamarin is new to me unfortunately.

    This is what the result of the code is in my json it produces

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    What I mean is... Why are you having to hunt through your UI by the x:name at all?
    Your Picker should have a bindings for the .Source and the .SelectedItem properties - there is no reason to go hunting like that.

    my picker has a x:Name

    As a rule 99% of all XAML UI elements never get names because they are not needed unless referenced by another UI element in the XAML. If you're referencing your control by x:name from C# the odds are you're already going down the wrong path. There are times you do that... like 1% of 1% of the time under specific conditions. But I seriously doubt you're there if you're just starting out.

    Xamarin is new to me unfortunately

    Have you worked through any of the learning material and course and tutorials first? Just jumping on the keyboard thinking "I'll work it out as I go along" never ends well.

    Free textbook
    https://blogs.msdn.microsoft.com/microsoft_press/2016/03/31/free-ebook-creating-mobile-apps-with-xamarin-forms/

    Xamarin University
    https://university.xamarin.com/

    Xamarin University has dozens of tutorial classes on YouTube if you use their search feature.

    And of course there are dozens of Xamarin bloggers out there.
    http://redpillxamarin.com/2018/03/12/2018-101-vs2017-new-solution/

  • jenova1628jenova1628 Member ✭✭
    edited November 2018

    Thanks for your advice I will get round to looking at this. I chose this way as it worked for the others.

    I do have this separate class for the picker which gets the conditions then the picker on the xaml uses ItemsSource="{Binding ConditionList}" and ItemDisplayBinding="{Binding Value}"

       `namespace newInspection
       {
        class ConditionPicker
        {
            public List<Condition> ConditionList { get; set; }
    
            public ConditionPicker()
            {
                ConditionList = GetList().OrderBy(testc => testc.Value).ToList();
    
            }
    
            public List<Condition> GetList()
            {
                var conditions = new List<Condition>()
                {
                    new Condition(){Key = 1, Value="N/A" },
                    new Condition(){Key = 2, Value="No" },
                    new Condition(){Key = 3, Value="Yes" },
                };
    
                return conditions;
            }
        }
    
        public class Condition
        {
            public int Key { get; set; }
            public string Value { get; set; }
        }
    }`
    
  • jenova1628jenova1628 Member ✭✭
    edited November 2018

    would tyrePicker.SelectedItem.ToString() be better then using FineByName ? this is currently working but still not getting the value

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    I chose this way as it worked for the others.

    I don't know who "others" are... but nobody I have every worked for would find it acceptable to do that sort of thing with strings. "Magic strings" are just a no-no in most work environments: That's what enums are for.

    Wait... You said Condition was a string.

    condition is currently public string Condition { get; set; }

    Now your code shows its a class.

        public class Condition
        {
            public int Key { get; set; }
            public string Value { get; set; }
        }
    

    And why create a class for a key an value when you can use KeyValuePair right out of the framework and get all the added work that exists with it?

    Do you have a property called Condition and a class called Condition? Don't do that. You can see the confusion it causes. You can never tell if you're referencing a property or a type. Call the property something other than the exact same name as the type
    public Condition Condition {get; set;} while syntactically legal is a huge major real-world nightmare of maintenance and understanding of the code.

    So yeah... If you take your class of Condition and as it for its ToString its going to return Condition. It has no knowledge of doing otherwise. It doesn't magically know it should return the Value property. Add an override method of ToString() so your class knows to return Value when a caller asks for its ToString(). That basic C# and not Xamarin related at all.

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    @joeeyireland said:
    this is currently working but still not getting the value

    See above. Its not getting hte value because it has no reason to know it should use the value property. You have to give your class an override ToString() method that returns the Value when asked.

    would tyrePicker.SelectedItem.ToString() be better then using FineByName ?

    You're not grasping the other comment I'm making... You are so far off base by needing to do this in code behind that I can't even imagine how the rest of this is going to be coded. Nobody I know in a first world nation would continue along this path. Its like something you would get from a Mumbai outsourcer. You seriously need to stop and regroup and use good design practices and patterns or this is going to be such a mess of spaghetti code that even you can't figure out what you've done.

Sign In or Register to comment.