Click here to Skip to main content
15,886,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a dictionary of 4 keywords on xaml visual studio 2019: int, bool, etc. Each keywords has its own value: hello, hello1, etc. However, when pressing these keywords, instead of answer its own values, they always answer the last one of the last keyword, hello3, not its corresponding one, Why? Any idea, please? Thanks

  <?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="Dictionary.MainPage">
    <StackLayout Orientation="Horizontal">
        <Frame BackgroundColor="Cyan" Padding="0" CornerRadius="0">
            <Label Text="Welcome to KeyWords in C#!" 
                  
                  VerticalTextAlignment="Start" 
                   TextColor="Blue" 
                   FontSize="20"/>
        </Frame>
        <Frame BackgroundColor="#2196F3" Padding="0" CornerRadius="0">
            <Label Text="Click on Buttons!" 
                  
                   HorizontalTextAlignment="Center" 
                   TextColor="DarkRed" 
                   FontSize="20"/>
        </Frame>
        <Label x:Name="valuelabel"
               Text="myInventory!"                  
               HorizontalTextAlignment="Center" 
               TextColor="DarkRed" 
               
                   FontSize="Medium"/>

        <Button Text="int"
               
               Clicked="Button_Clicked"
                 Margin="3"              
            
                VerticalOptions="Center"
                FontSize="Large"
              
                TextColor="Blue"/>
        
        <Button Text="if"
                   
                Clicked="Button_Clicked"
                 Margin="2"              
                 HorizontalOptions="Start"
               
                FontSize="Large"
                TextColor="Blue"/>
        
        <Button Text="bool"
                   
                Clicked="Button_Clicked"
                 Margin="3"              
                 HorizontalOptions="Start"
                
                FontSize="Large"
                TextColor="Blue"/>
        
        <Button Text="class"
                   
                Clicked="Button_Clicked"
                 Margin="4"              
                 HorizontalOptions="Center"
              
                FontSize="Large"
                TextColor="Blue"/>
        
        

    </StackLayout>
    
    
    
</ContentPage>



C# Version
C#
using System.Threading.Tasks;
using Xamarin.Forms;


namespace Dictionary
{
    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            Button button = new Button
            {

                HorizontalOptions = LayoutOptions.Center,
                VerticalOptions = LayoutOptions.Center

            };

            button.Clicked += async (sender, args) =>
            {
                await Navigation.PushAsync(new MainPage());

            };

           
            
            //Content = button;

        }
        
        private void Button_Clicked(object sender, EventArgs args)
        {
            Dictionary<string, string=""> myInventory = new Dictionary<string, string="">();
            myInventory.Add("int", "Hello ");
            myInventory.Add("if", "hello1");
            myInventory.Add("bool", "hello2");
            myInventory.Add("class", "hello3");
            valuelabel.Text =($"{myInventory["int"]}");
            valuelabel.Text = ($"{myInventory["if"]}");
            valuelabel.Text = ($"{myInventory["bool"]}");
            valuelabel.Text = ($"{myInventory["class"]}");
   
          
            
        }
    }
    
}


What I have tried:

I have tried to create click handler for each one of the buttons and I have tried to change the names for each one of the buttons, and it doesn't work. Any ideas, please?
Posted
Updated 30-Oct-21 9:40am
v4

1 solution

You lost your focus at some point.

Button_Clicked sender is a button:
var btn = sender as Button;

Button has the (dictionary) "key" you want in .Text:
var key = btn.Text 

Use "one" dictionary get:
valuelabel.Text = myInventory[ key ];   // The $ string substitution is redundant.
 
Share this answer
 
Comments
Diasalva5 30-Oct-21 16:47pm    
Dear Mr. Gerry Schmitz, Thank you, it worked as expected with your advise. You are awesome!
One question, please. Could you recommend me some good books to learn Xamarin, please?
God bless you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900