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
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());
};
}
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?