how to add return value in xamarin picker option

Chan Geez 1 Reputation point
2022-10-08T13:57:45.853+00:00

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()  
       {  
     
           InitializeComponent();  
     
     
   // i dont want to use This its working fine  
     
         MainPicker.Items.Add("String");  
             
   // 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}"  
     
             SelectedIndexChanged="MainPicker_SelectedIndexChanged"  
               x:Name="MainPicker">  
                  
           </Picker>  

///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;

Developer technologies .NET Xamarin
Developer technologies VB
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2022-10-10T02:24:48.277+00:00

    Hello,

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.