Click here to Skip to main content
15,916,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new on this world and I have created this code in Xamarin forms, however I am not able to link the answer given in the visual Xamarin with the C# code. Any suggestion, please? Thanks
This is the code in C#
public MainPage()
       {
           InitializeComponent();
           Button button = new Button
           {

               HorizontalOptions = LayoutOptions.Center,
               VerticalOptions = LayoutOptions.Center

           };

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

           };

       }
       public void Button_Click(object sender, EventArgs e)
       {

       }

   }

   namespace animal
   {
       class animaltype
       {

           public static string Name(string a)
           {
               if (a == "Dog")
               {
                   a = "oh that's cool";

                   return a;
               }
               else
                   a = " Pets are good company help to live longer";
               return a;


           }

       }
   }


This is the code in XAML
<StackLayout>
       <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0">
           <Label Text="Welcome to Namespaces!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/>
       </Frame>

       <Label x:Name="dogbreed1"  Text=" what type of dog breed do you have?" FontSize="Title" Padding="30,10,30,10"/>


       <Entry x:Name="dogbreed"  Placeholder="Enter text" />

       <Button Text="Click here to see answer" Clicked="Button_Click"
               Margin="5"
                HorizontalOptions="Center"
               VerticalOptions="Center"
               FontSize="Large"
               TextColor="Blue"/>

       <Label x:Name="question"
              Text="Answer" HorizontalTextAlignment="Center" TextColor="Blue" FontSize="36"/>

       <Label Text="" FontSize="16" Padding="30,0,30,0"/>
       <Label FontSize="16" Padding="30,24,30,0">


What I have tried:

I have changed the name of variable a by doing this way
if (Entry.dogbreed == "German Shepperd")
                {
                    Label.question = "oh that's one of the smartest ones";

                    
                }

I have tried to substitute the variable a equal to Entry.dobreed, but it doesnt work, either. Thanks
but it hasnt work
Posted
Updated 22-Nov-21 1:44am
Comments
[no name] 21-Nov-21 21:37pm    
In the constructor, you created a button that goes out of scope, so nothing happens there. In the XAML, you have a button that calls "Button_Click" but that handler does nothing. In summary, you don't have any working buttons.
Diasalva5 22-Nov-21 10:48am    
Thanks
Diasalva5 22-Nov-21 12:01pm    
Mr. Schmitz: The issue is that I am trying to create some namespaces for this code and if I place it inside the button with the namespaces, it wont work, either. Thanks

1 solution

If you would like to handle click event for a button defined in XAML code, please see: How to: Handle a Routed Event - WPF .NET Framework | Microsoft Docs[^]
If you would like to create new button and add an event handler to it, please see: How to: Add an Event Handler Using Code - WPF .NET Framework | Microsoft Docs[^]

Seems, you're very beginner. I'd suggest to start here: Windows Presentation Foundation for .NET 5 documentation | Microsoft Docs[^]
 
Share this answer
 
v2
Comments
Diasalva5 22-Nov-21 12:02pm    
Mr. Maciej Los: The issue is that I am trying to create some namespaces for this code and if I place it inside the button with the namespaces, it wont work, either. Thanks

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