如何从各种条目(每行)中检索数据,进行计算,然后能够实时显示结果?

问题描述 投票:0回答:1

我想知道一种方法,该方法能够从Grid Layout的各种条目中检索数据,并准备好在其他类/方法中计算其值。我希望结果根据您在每个成绩的分数,ScoreGot和权重中输入的内容自动更改。

<?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="GradeThisForMe.GradeCalculator"
             NavigationPage.HasNavigationBar="False"
             BackgroundImage="WallpaperBlueSkyBlurMedium.png">
    <StackLayout>
    <ScrollView>
        <Grid x:Name="tableGrid" RowSpacing="0" ColumnSpacing="0">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Label Text="Target Grade: " Style="{StaticResource GridTopLabelStyle}" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"  />
        <Entry Text="{Binding TargetGrade}" Style="{StaticResource GridTopEntryStyle}" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="1"/>

        <Label Text="Current Grade: " Style="{StaticResource GridTopLabelStyle}" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"  />
        <Label Text="{Binding AverageGrade}" Style="{StaticResource GridTopLabelStyle}" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2"/>

        <Label Text="You need: " Style="{StaticResource GridTopLabelStyle}" Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"  />
        <Label Text="{Binding GradeNeeded}" Style="{StaticResource GridTopLabelStyle}" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2"/>

        <Label Text="#" Style="{StaticResource GridBottomLabelStyle}" Grid.Row="3" Grid.Column="0"/>
        <Label Text="Score" Style="{StaticResource GridBottomLabelStyle}" Grid.Row="3" Grid.Column="1"/>
        <Label Text="Score Got" Style="{StaticResource GridBottomLabelStyle}" Grid.Row="3" Grid.Column="2"/>
        <Label Text="Weight" Style="{StaticResource GridBottomLabelStyle}" Grid.Row="3" Grid.Column="3"/>

        <Entry Text="{Binding Grade1Name}" Style="{StaticResource GridBottomEntryStyle}" Placeholder="Grade#1" Grid.Row="4" Grid.Column="0"/>
        <Entry Text="{Binding Score1}" Style="{StaticResource GridNumericEntryStyle}" Grid.Row="4" Grid.Column="1"/>
        <Entry Text="{Binding ScoreGot1}" Style="{StaticResource GridNumericEntryStyle}" Grid.Row="4" Grid.Column="2"/>
        <Entry Text="{Binding Weight1}" Style="{StaticResource GridNumericEntryStyle}" Grid.Row="4" Grid.Column="3"/>

        <Entry  Style="{StaticResource GridBottomEntryStyle}" Placeholder="Grade#2" Grid.Row="5" Grid.Column="0"/>
        <Entry  Style="{StaticResource GridNumericEntryStyle}"  Grid.Row="5" Grid.Column="1"/>
        <Entry  Style="{StaticResource GridNumericEntryStyle}"  Grid.Row="5" Grid.Column="2"/>
        <Entry  Style="{StaticResource GridNumericEntryStyle}"  Grid.Row="5" Grid.Column="3"/>

        <Entry Style="{StaticResource GridBottomEntryStyle}" Placeholder="Grade#3" Grid.Row="6" Grid.Column="0"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="6" Grid.Column="1"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="6" Grid.Column="2"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="6" Grid.Column="3"/>

        <Entry Style="{StaticResource GridBottomEntryStyle}" Placeholder="Grade#4" Grid.Row="7" Grid.Column="0"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="7" Grid.Column="1"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="7" Grid.Column="2"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="7" Grid.Column="3"/>

        <Entry Style="{StaticResource GridBottomEntryStyle}" Placeholder="Grade#5" Grid.Row="8" Grid.Column="0"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="8" Grid.Column="1"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="8" Grid.Column="2"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="8" Grid.Column="3"/>

        <Entry Style="{StaticResource GridBottomEntryStyle}" Placeholder="Grade#6" Grid.Row="9" Grid.Column="0"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="9" Grid.Column="1"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="9" Grid.Column="2"/>
        <Entry Style="{StaticResource GridNumericEntryStyle}" Grid.Row="9" Grid.Column="3"/>

            </Grid>

        </ScrollView>
        <Button Text="Calculate"
        Style="{StaticResource PrimaryButton}"/>
    </StackLayout>
</ContentPage>

出于测试目的,我的目标是在输入分数和达到#1级的分数后实时更改和显示AverageGrade的结果。要注意属性更改,该值必须是字符串。为了计算结果,在Grade1Total中,我尝试将Score1和ScoreGot1的值临时转换为double。将计算结果转换为字符串后,它永远不会显示在显示屏上。您建议我该怎么做?

[注:我知道代码中的某些部分(如“计算”按钮)不完整或不必要。我现在的首要任务是解决这种情况。

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

namespace GradeThisForMe
{
    public class AddGradesDetails: INotifyPropertyChanged
    {
        string targetGrade;

        string grade1_Name;

        string score1;
        double dbl_score1;

        string score_got1;
        double dbl_score_got1;

        string weight1;

        string total_grade1;
        double dbl_total_grade1;

        string decimal_weight1;
        string average_grade;

        string grade_needed;



        public event PropertyChangedEventHandler PropertyChanged;

        void OnPropertyChanged([CallerMemberName] string total_grade1 = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(total_grade1));
        }

        public string Grade1Name
        {
            get { return grade1_Name; }
            set
            {
                grade1_Name = value;
                OnPropertyChanged(Grade1Name);


            }


        }

        public string Score1
        {
            get { return score1; }
            set
            {

                score1 = value;
                OnPropertyChanged(Score1);
                OnPropertyChanged(nameof(AverageGrade));

            }




        }

        public string ScoreGot1
        {
            get { return score_got1; }
            set
            {
                score_got1 = value;
                OnPropertyChanged(ScoreGot1);
                OnPropertyChanged(nameof(AverageGrade));



            }



    } 

        public string Weight1
        {
            get { return weight1; }
            set
            {
                weight1 = value;
                OnPropertyChanged(Weight1);
                OnPropertyChanged(nameof(AverageGrade));

            }



        }

        public string Grade1Total
        {
            get { return total_grade1; }
            set
            {


                dbl_score1 = Convert.ToDouble(Score1);
                dbl_score_got1 = Convert.ToDouble(ScoreGot1);
                dbl_total_grade1 = ((dbl_score_got1 / dbl_score1)*100);

                total_grade1 = Convert.ToString(dbl_score1);
                OnPropertyChanged(Grade1Total);
                OnPropertyChanged(nameof(AverageGrade));

            }


        }


        public string AverageGrade
        {
            get { return $"{Grade1Total}"; }
            set
            {


                OnPropertyChanged(nameof(AverageGrade));
            }



        }
        public string TargetGrade
        {
            get { return targetGrade; }
            set
            {
                targetGrade = value;
                OnPropertyChanged(TargetGrade);
            }


        }

        public string GradeNeeded
        {
            get { return grade_needed; }
            set
            {

            }
        }


    }




}


xamarin mvvm xamarin.forms xamarin.android
1个回答
0
投票

只需更新绑定模型中的值。单击计算按钮时,将detailsModel设置为BindingContext并更新detailsModel.Result的值。这是一个简单的示例:

public partial class MainPage : ContentPage
{
    AddGradesDetails detailsModel;

    public MainPage()
    {
        InitializeComponent();

        detailsModel = new AddGradesDetails();
        detailsModel.Score1 = "1";
        detailsModel.ScoreGot1 = "2";
        detailsModel.Result = "";

        BindingContext = detailsModel;
    }

    private void Button_Clicked(object sender, EventArgs e)
    {
        double result = Convert.ToDouble(detailsModel.Score1) + Convert.ToDouble(detailsModel.ScoreGot1);

        detailsModel.Result = result.ToString();
    }
}

在xaml中:

<StackLayout>
    <ScrollView>
        <Grid x:Name="tableGrid" RowSpacing="0" ColumnSpacing="0">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Label Text="Target Grade: " Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2"  />
            <Entry Text="{Binding TargetGrade}" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="1"/>

            <Label Text="Current Grade: " Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"  />
            <Label Text="{Binding AverageGrade}" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2"/>

            <Label Text="You need: " Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2"  />
            <Label Text="{Binding GradeNeeded}" Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="2"/>

            <Label Text="#" Grid.Row="3" Grid.Column="0"/>
            <Label Text="Score" Grid.Row="3" Grid.Column="1"/>
            <Label Text="Score Got" Grid.Row="3" Grid.Column="2"/>
            <Label Text="Weight" Grid.Row="3" Grid.Column="3"/>

            <Entry Text="{Binding Grade1Name}" Placeholder="Grade#1" Grid.Row="4" Grid.Column="0"/>
            <Entry Text="{Binding Score1}" Grid.Row="4" Grid.Column="1"/>
            <Entry Text="{Binding ScoreGot1}" Grid.Row="4" Grid.Column="2"/>
            <Entry Text="{Binding Result}" Grid.Row="4" Grid.Column="3"/>

        </Grid>

    </ScrollView>
    <Button Text="Calculate" Clicked="Button_Clicked"/>
</StackLayout>

模型:

public class AddGradesDetails : INotifyPropertyChanged
{
    string grade1_Name { get; set; }
    string score1 { get; set; }
    string score_got1 { get; set; }
    string result { get; set; }

    public event PropertyChangedEventHandler PropertyChanged;

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

    public string Grade1Name
    {
        get { return grade1_Name; }
        set
        {
            grade1_Name = value;
            OnPropertyChanged("Grade1Name");
        }
    }

    public string Score1
    {
        get { return score1; }
        set
        {
            if (value != score1)
            {
                score1 = value;
                OnPropertyChanged("Score1");
            }               
        }
    }

    public string ScoreGot1
    {
        get { return score_got1; }
        set
        {
            score_got1 = value;
            OnPropertyChanged("ScoreGot1");
        }
    }

    public string Result
    {
        get { return result; }
        set
        {
            result = value;
            OnPropertyChanged("Result");
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.