question

Julien-2587 avatar image
0 Votes"
Julien-2587 asked Julien-2587 commented

Retrieve data from a RadioButton

Hello, I am developing a small role-playing game that works very well with a single Class: Personnage but if I integrate a RadioButton to choose between 3 characters, I cannot retrieve the correct information.
I have a Class : Voleur, Guerrier and Archer which inherits from the Class: Personnage, itself inheriting from the Class: Entite.
Otherwise, if I give all the attributes to a generic Class: Personnage, there is no problem but you cannot choose your character.

My xaml :

 <StackLayout x:Name="ChoixPerso">
             <Label Text="Choisissez votre avatar."
                    Margin="20"
                    HorizontalTextAlignment="Center"
                    HorizontalOptions="CenterAndExpand"
                    FontSize="Large"/>
             <RadioButton x:Name="ChoixPerso01"
                          Value="Guerrier"
                          Content="Guerrier"
                          CheckedChanged="ChoisirGuerrier"/>
             <RadioButton x:Name="ChoixPerso02"
                          Value="Archer"
                          Content="Archer"
                          CheckedChanged="ChoisirArcher"/>
             <RadioButton x:Name="ChoixPerso03"
                          Value="Voleur"
                          Content="Voleur"
                          CheckedChanged="ChoisirVoleur"/>
             <Label x:Name="personnageLabel"
                    Text="Vous avez choisi:"/>
             </StackLayout>
             <!--Row 01-->
             <Label HorizontalTextAlignment="Start"
                    Grid.Row="1"
                    Grid.ColumnSpan="2"
                    x:Name="Text01"
                    TextColor="Black"/>

My xaml.cs :

 namespace JdrApp.Views
 {
     [XamlCompilation(XamlCompilationOptions.Compile)]
     public partial class Donjon01 : ContentPage
     {
         int tentative = 0;       
         Personnage monPerso = new Personnage("Renshaw");
    
         public Donjon01()
         {
             InitializeComponent();
         }
         public void ChoisirGuerrier(object sender, CheckedChangedEventArgs e)
         {
             RadioButton button = sender as RadioButton;
             personnageLabel.Text = $"Vous avez choisi: {button.Content}";
             monPerso = { button.Content}( new Guerrier("OlafMegabaff"));
             Text01.Text = $"Vous êtes un puissant {button.Content} nommé \n" + monPerso.Caracteristiques() + "\nVous arrivez devant le Donjon et face à vous, une porte sombre et imposante se dresse. Cela semble être la seule entrée.\nQu'allez vous décider ?";
         }
         public void ChoisirArcher(object sender, CheckedChangedEventArgs e)
         {
             RadioButton button = sender as RadioButton;
             personnageLabel.Text = $"Vous avez choisi: {button.Content}";
             monPerso = { button.Content} (new Guerrier("OlafMegabaff"));
             Text01.Text = $"Vous êtes un habile {button.Content} nommé \n" + monPerso.Caracteristiques() + "\nVous arrivez devant le Donjon et face à vous, une porte sombre et imposante se dresse. Cela semble être la seule entrée.\nQu'allez vous décider ?";
    
         }
         public void ChoisirVoleur(object sender, CheckedChangedEventArgs e)
         {
             RadioButton button = sender as RadioButton;
             personnageLabel.Text = $"Vous avez choisi: {button.Content}";
             monPerso = { button.Content} (new Guerrier("OlafMegabaff"));
             Text01.Text = $"Vous êtes un rapide {button.Content} nommé \n" + monPerso.Caracteristiques() + "\nVous arrivez devant le Donjon et face à vous, une porte sombre et imposante se dresse. Cela semble être la seule entrée.\nQu'allez vous décider ?";
    
         }

My Entite.cs

 public abstract class Entite
     {
         protected string nom;
         protected bool estMort = false;
         protected int pointsDeVie;
         protected int degatsMin;
         protected int degatsMax;
         protected int potionsSoins;
         protected int piecesOr;
         protected Random random = new Random();
    
         public Entite(string nom)
         {
             this.nom = nom;
         }

My Class : Personnage

 public class Personnage : Entite
     {
         private int niveau;
         private int experience;
    
         public Personnage(string nom) : base(nom)
         {
             this.nom = nom;
             //pointsDeVie = 150;
             //degatsMin = 15;
             //degatsMax = 20;
             niveau = 1;
             experience = 0;
             potionsSoins = 0;
         }

My Class : Voleur (same as for Guerrier.cs or Archer.cs)

 public class Voleur: Personnage
     {
         public Voleur(string nom) : base(nom)
         {
             pointsDeVie = 200;
             degatsMin = 30;
             degatsMax = 35;
         }
     }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered Julien-2587 commented

Hello,​

Welcome to our Microsoft Q&A platform!

I do not know about this monPerso = { button.Content}( new Guerrier("OlafMegabaff"));, I copy this line to my demo, it will have grammatical errors. So I change it to monPerso = new Archer(button.Content + ": test 2"); format.


Do you want to achieve the function like following screenshot? I do not know what is Archer and Voleur s name? So I give change name to test1, test 2, test 3. and you donot share Caracteristiques method, I make him to return the value of nom.

Click the Guerrier

117257-image.png

click the Archer
117218-image.png
click the Voleur
117243-image.png

Here is xaml.cs code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace RecordAudioDemo
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class Donjon01 : ContentPage
    {
        int tentative = 0;
        Personnage monPerso = new Personnage("Renshaw");

        public Donjon01()
        {
            InitializeComponent();
        }
        public void ChoisirGuerrier(object sender, CheckedChangedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            personnageLabel.Text = $"Vous avez choisi: {button.Content}";
          // monPerso = { button.Content} (new Guerrier("OlafMegabaff"));
            monPerso= new Guerrier(button.Content+": test 1");
            Text01.Text = $"Vous êtes un puissant {button.Content} nommé \n" + monPerso.Caracteristiques() + "\nVous arrivez devant le Donjon et face à vous, une porte sombre et imposante se dresse. Cela semble être la seule entrée.\nQu'allez vous décider ?";
        }
        public void ChoisirArcher(object sender, CheckedChangedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            personnageLabel.Text = $"Vous avez choisi: {button.Content}";
            //  monPerso = { button.Content } (new Archer("OlafMegabaff"));

            monPerso = new Archer(button.Content + ": test 2");
            Text01.Text = $"Vous êtes un habile {button.Content} nommé \n" + monPerso.Caracteristiques() + "\nVous arrivez devant le Donjon et face à vous, une porte sombre et imposante se dresse. Cela semble être la seule entrée.\nQu'allez vous décider ?";

        }
        public void ChoisirVoleur(object sender, CheckedChangedEventArgs e)
        {
            RadioButton button = sender as RadioButton;
            personnageLabel.Text = $"Vous avez choisi: {button.Content}";
            //  monPerso = { button.Content} (new Voleur("OlafMegabaff"));

            monPerso = new Voleur(button.Content + ": test 3");
            Text01.Text = $"Vous êtes un rapide {button.Content} nommé \n" + monPerso.Caracteristiques() + "\nVous arrivez devant le Donjon et face à vous, une porte sombre et imposante se dresse. Cela semble être la seule entrée.\nQu'allez vous décider ?";

        }
    }
}


Here is edited Personnage.cs

public class Personnage : Entite
    {
        private int niveau;
        private int experience;

        public Personnage(string nom) : base(nom)
        {
            this.nom = nom;
            //pointsDeVie = 150;
            //degatsMin = 15;
            //degatsMax = 20;
            niveau = 1;
            experience = 0;
            potionsSoins = 0;
        }

        internal string Caracteristiques()
        {
            // throw new NotImplementedException();


            return "nom: " + this.nom;
        }
    }


Best Regards,

Leon Lu



If the response is helpful, please click "Accept Answer" and upvote it.

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.



image.png (34.3 KiB)
image.png (37.7 KiB)
image.png (38.8 KiB)
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Splendid, that's exactly it. I understand my mistake, I had reversed the statements. I have been learning Xamarin Forms for 3 weeks by coding a role-playing game.
Thank you so much.

0 Votes 0 ·