question

Borisoprit-6789 avatar image
0 Votes"
Borisoprit-6789 asked Borisoprit-6789 commented

Sum of 10 entries

Want to make a scoreboard of a game , and want also a total of all

I have about 10 entries and want to add then up to a total .

How do i make this work ?

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 Borisoprit-6789 commented

Hello,​

Welcome to our Microsoft Q&A platform!

I have about 10 entries and want to add then up to a total .

Do you want to achieve the result like following screenshot?

63791-image.png

Here is background code. If any of entry's value will be changed, the value of sum will be changed at the same time.

using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace SumOfEntrys
{
    public partial class MainPage : ContentPage, INotifyPropertyChanged
    {
        int _entry1 = 0;
        public int Entry1
        {
            get
            {
                return _entry1;
            }

            set
            {
                if (_entry1 != value)
                {
                    _entry1 = value;
                    OnPropertyChanged("Entry1");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry2 = 0;
        public int Entry2
        {
            get
            {
                return _entry2;
            }

            set
            {
                if (_entry2 != value)
                {
                    _entry2 = value;
                    OnPropertyChanged("Entry2");
                    TextChangedCommand.Execute(null);
                }
            }

        }


        int _entry3 = 0;
        public int Entry3
        {
            get
            {
                return _entry3;
            }

            set
            {
                if (_entry3 != value)
                {
                    _entry3 = value;
                    OnPropertyChanged("Entry3");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry4 = 0;
        public int Entry4
        {
            get
            {
                return _entry4;
            }

            set
            {
                if (_entry4 != value)
                {
                    _entry4 = value;
                    OnPropertyChanged("Entry4");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry5 = 0;
        public int Entry5
        {
            get
            {
                return _entry5;
            }

            set
            {
                if (_entry5 != value)
                {
                    _entry5 = value;
                    OnPropertyChanged("Entry5");
                    TextChangedCommand.Execute(null);
                }
            }

        }
        int _entry6 = 0;
        public int Entry6
        {
            get
            {
                return _entry6;
            }

            set
            {
                if (_entry6 != value)
                {
                    _entry6 = value;
                    OnPropertyChanged("Entry6");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry7 = 0;
        public int Entry7
        {
            get
            {
                return _entry7;
            }

            set
            {
                if (_entry7 != value)
                {
                    _entry7 = value;
                    OnPropertyChanged("Entry7");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry8 = 0;
        public int Entry8
        {
            get
            {
                return _entry8;
            }

            set
            {
                if (_entry8 != value)
                {
                    _entry8 = value;
                    OnPropertyChanged("Entry8");
                    TextChangedCommand.Execute(null);
                }
            }

        }
        int _entry9 = 0;
        public int Entry9
        {
            get
            {
                return _entry9;
            }

            set
            {
                if (_entry9 != value)
                {
                    _entry9 = value;
                    OnPropertyChanged("Entry9");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry10 = 0;
        public int Entry10
        {
            get
            {
                return _entry10;
            }

            set
            {
                if (_entry10 != value)
                {
                    _entry10 = value;
                    OnPropertyChanged("Entry10");
                    TextChangedCommand.Execute(null);
                }
            }

        }
        int _sum = 0;
        public int Sum
        {
            get
            {
                return _sum;
            }

            set
            {
                if (_sum != value)
                {
                    _sum = value;
                    OnPropertyChanged("Sum");

                }
            }
        }

        int GetAllOfValue()
        {
            return Entry3 + Entry2 + Entry1+ Entry4 + Entry5 + Entry6+ Entry7 + Entry8 + Entry9+ Entry10;
        }

        public Command TextChangedCommand => new Command(() => TextChanged());

        private void TextChanged()
        {
            Sum = GetAllOfValue();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        public MainPage()
        {

           
            BindingContext = this;
            InitializeComponent();
        }
    }
}


Here is layout code.

<StackLayout>
        <Entry Text="{Binding Entry1}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry2}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry3}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry4}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry5}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry6}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry7}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry8}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry9}" Keyboard="Numeric"></Entry>
        <Entry Text="{Binding Entry10}" Keyboard="Numeric"></Entry>
        <Label Text="Sum:" FontSize="Body"></Label>
        <Label Text="{Binding Sum}" FontSize="Header" ></Label>
    </StackLayout>


==========================Update================================

Agree with JCmanke, ContentPage already implements INotifyPropertyChanged via inheritance from BindableObject so there is no need to reimplement it here. Just delete those methods. please refer to following code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace SumOfEntrys
{
    public partial class MainPage : ContentPage
    {
        int _entry1 = 0;
        public int Entry1
        {
            get
            {
                return _entry1;
            }

            set
            {
                if (_entry1 != value)
                {
                    _entry1 = value;
                    OnPropertyChanged("Entry1");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry2 = 0;
        public int Entry2
        {
            get
            {
                return _entry2;
            }

            set
            {
                if (_entry2 != value)
                {
                    _entry2 = value;
                    OnPropertyChanged("Entry2");
                    TextChangedCommand.Execute(null);
                }
            }

        }


        int _entry3 = 0;
        public int Entry3
        {
            get
            {
                return _entry3;
            }

            set
            {
                if (_entry3 != value)
                {
                    _entry3 = value;
                    OnPropertyChanged("Entry3");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry4 = 0;
        public int Entry4
        {
            get
            {
                return _entry4;
            }

            set
            {
                if (_entry4 != value)
                {
                    _entry4 = value;
                    OnPropertyChanged("Entry4");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry5 = 0;
        public int Entry5
        {
            get
            {
                return _entry5;
            }

            set
            {
                if (_entry5 != value)
                {
                    _entry5 = value;
                    OnPropertyChanged("Entry5");
                    TextChangedCommand.Execute(null);
                }
            }

        }
        int _entry6 = 0;
        public int Entry6
        {
            get
            {
                return _entry6;
            }

            set
            {
                if (_entry6 != value)
                {
                    _entry6 = value;
                    OnPropertyChanged("Entry6");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry7 = 0;
        public int Entry7
        {
            get
            {
                return _entry7;
            }

            set
            {
                if (_entry7 != value)
                {
                    _entry7 = value;
                    OnPropertyChanged("Entry7");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry8 = 0;
        public int Entry8
        {
            get
            {
                return _entry8;
            }

            set
            {
                if (_entry8 != value)
                {
                    _entry8 = value;
                    OnPropertyChanged("Entry8");
                    TextChangedCommand.Execute(null);
                }
            }

        }
        int _entry9 = 0;
        public int Entry9
        {
            get
            {
                return _entry9;
            }

            set
            {
                if (_entry9 != value)
                {
                    _entry9 = value;
                    OnPropertyChanged("Entry9");
                    TextChangedCommand.Execute(null);
                }
            }

        }

        int _entry10 = 0;
        public int Entry10
        {
            get
            {
                return _entry10;
            }

            set
            {
                if (_entry10 != value)
                {
                    _entry10 = value;
                    OnPropertyChanged("Entry10");
                    TextChangedCommand.Execute(null);
                }
            }

        }
        int _sum = 0;
        public int Sum
        {
            get
            {
                return _sum;
            }

            set
            {
                if (_sum != value)
                {
                    _sum = value;
                    OnPropertyChanged("Sum");

                }
            }
        }

        int GetAllOfValue()
        {
            return Entry3 + Entry2 + Entry1+ Entry4 + Entry5 + Entry6+ Entry7 + Entry8 + Entry9+ Entry10;
        }

        public Command TextChangedCommand => new Command(() => TextChanged());

        private void TextChanged()
        {
            Sum = GetAllOfValue();
        }

        
        public MainPage()
        {

           
            BindingContext = this;
            InitializeComponent();
        }

       
    }
}


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 (149.1 KiB)
· 8
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.

Thanks for the reply LeonLu ,

That works but i have 2 warnings.

64011-alarm1.png


64022-alarm2.png


0 Votes 0 ·
alarm1.png (22.1 KiB)
alarm2.png (24.0 KiB)

ContentPage already implements INotifyPropertyChanged via inheritance from BindableObject so there is no need to reimplement it here. Just delete those methods.

0 Votes 0 ·

Thanks jcmanke , i deleted it .

0 Votes 0 ·

Please see my updated answer.

0 Votes 0 ·

Thanks LeonLu ,


Works great

0 Votes 0 ·

Hello LeonLu,

I thought i was ready now .

But i have a small issue , when i use this in a TabView there is a problem.

In the first Tab everything is working , but when i use the 2e TabView it is going back to the first TabView.

I think it is becaure of this

public Command TextChangedCommand => new Command(() => TextChanged());
private void TextChanged()



Can i make a Sub of the code ? so i call it when i use a Entry .

And when it works one for TabView 2 and TabView 3 only


0 Votes 0 ·

Above code works in divided entry, it cannot used in nested view or combine view(such as listview that contains sub-items).

And you mentioned TabView, Is this plugin?https://github.com/chaosifier/TabView, Could you open a new thread for this new questions(keep one thread for one question, and please post more details about your issue, for example, this question, you ask I have about 10 entries and want to add then up to a total . without any tabview or others your needs, it will cost lots of time to other direction.)?


0 Votes 0 ·