how to add return value in xamarin picker option

Chan Geez 1 Reputation point

hey i want to know how i can return a value in picker option.

this is my code:

   namespace App1 {   
   public partial class MainPage : ContentPage {  
       List<courses> myData = new List<courses>();  
       public MainPage()  
   // i dont want to use This its working fine  
   // Currently using this in my app but now i want when i select course name from picker option then picker should automatically select key behind the course name so i can generate key using button.  
           myData.Add(new Courses  
               Course= "ABC",  
               Key= "Custom Key"  
           MainPicker.ItemsSource = myData;  
       private void Button_Clicked(object sender, EventArgs e)  
           richbox.Text = Convert.ToBase64String(this.AES_Encrypt(Encoding.ASCII.GetBytes(hardwareid.Text + "," + MainPicker.SelectedItem), "Custom Key"));  
   //Now here picker showing course name but its not generating correct key when i click button  
       private void MainPicker_SelectedIndexChanged(object sender, EventArgs e)  
           var item = MainPicker.Items[MainPicker.SelectedIndex];  

XAML code:

   <Picker Title="Choose a Course"  
             ItemDisplayBinding="{Binding Course}"  

///Using AES256 All are working fine but stuck
on key value should be return with hardwareid.Text both when i click button, I dont mention encryption and decryption code here all other are working fine.

also use this code but no luck

// var key = ((Courses)MainPicker.SelectedItem).Key;

A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,302 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,408 questions
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,610 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,691 Reputation points Microsoft Vendor


    Now here picker showing course name but its not generating correct key when i click button

    You need to get the selectitem by myData[selectedIndex] in the MainPicker_SelectedIndexChanged, then you can get the key from the selectitem like following code.

       private void MainPicker_SelectedIndexChanged(object sender, EventArgs e)  
                   var picker = (Picker)sender;  
                   int selectedIndex = picker.SelectedIndex;  
                   if (selectedIndex != -1)  
                       Courses result = myData[selectedIndex];  
                       string keyValue=result.Key;               

    Best Regards,

    Leon Lu

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.