首页 > 解决方案 > Xamarin:如何重用标签和条目以避免创建大量类

问题描述

我有一个项目有很多数学公式,我需要向用户展示:

我已经有一门课来解决基本的数学问题,比如求和、减法、除法、乘法等,然后我创建了一个字典来更改运算符,所以很好。

但是我有另一门课是我做公式的,大约 20 个公式,比如 Bhaskara、对数、几何序列等。所以我试图重用这些公式的条目和标签,因为如果我为每个公式创建公式,它会消耗很多类,或者一个类有很多行。

重复使用它的最佳方法是什么?考虑:

这是Bhaskara在手机上出现的图片(它只出现在第一行,所以如果我点击按钮解决,它会解决并出现这条彩色线解决Bhaskara),以及Bhaskara的代码:

void Bhaskara()
        {
            StandardImgExample();

            Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridInputs.Children.Add(lblDelta, 0, 0);

            Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridInputs.Children.Add(lblEqual, 1, 0);

            firstEntry = new Entry() { IsVisible = true, TextColor = Color.Black, Keyboard = Keyboard.Numeric, PlaceholderColor = Color.DarkGray, Placeholder = "\t b\u00b2 \t", HorizontalTextAlignment = TextAlignment.Center, HorizontalOptions = LayoutOptions.FillAndExpand };
            firstEntry.Completed += (s, e) =>
            {
                if (!firstEntry.Text.Contains("\u00b2"))
                {
                    firstEntry.Text += "\u00b2";                    
                }   
            };
            firstEntry.Focus();

            gridInputs.Children.Add(firstEntry, 2, 0);

            Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridInputs.Children.Add(lblMinus, 3, 0);

            Label lblFour = new Label() { Text = "4", HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridInputs.Children.Add(lblFour, 4, 0);

            Label lblMult = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
            gridInputs.Children.Add(lblMult, 5, 0);

            midEntry = new Entry() { IsVisible = true, MaxLength = 9999999, TextColor = Color.Black, Keyboard = Keyboard.Numeric, PlaceholderColor = Color.DarkGray, Placeholder = "\t a \t", HorizontalTextAlignment = TextAlignment.Center };
            gridInputs.Children.Add(midEntry, 6, 0);

            Label lblMult2 = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
            gridInputs.Children.Add(lblMult2, 7, 0);

            lastEntry = new Entry() { IsVisible = true, MaxLength = 9999999, TextColor = Color.Black, Keyboard = Keyboard.Numeric, PlaceholderColor = Color.DarkGray, Placeholder = "\t c \t", HorizontalTextAlignment = TextAlignment.Center };
            gridInputs.Children.Add(lastEntry, 8, 0);

            firstEntry.Completed += (s, e) => { midEntry.Focus(); };
            midEntry.Completed += (s, e) => { lastEntry.Focus(); };

            Button btnSolve = new Button()
            {
                VerticalOptions = LayoutOptions.EndAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
                Text = "Solve",
            };
            btnSolve.Clicked += async (s, e) =>
            {
                if (String.IsNullOrEmpty(firstEntry.Text))
                {
                    firstEntry.PlaceholderColor = Color.Red;
                    return;
                }
                else if (String.IsNullOrEmpty(midEntry.Text))
                {
                    midEntry.PlaceholderColor = Color.Red;
                    return;
                }
                else if (String.IsNullOrEmpty(lastEntry.Text))
                {
                    lastEntry.PlaceholderColor = Color.Red;
                    return;
                }

                frameFormula.IsVisible = true;
                frameFormula.IsEnabled = true;

                if(!isFirst)
                    await frameFormula.FadeTo(0, 400);

                gridFrame.Children.Clear();

                DisplayFirstLineResult();
                DisplaySecondLineResult();
                DisplayThirdLineResult();
                DisplayDeltaResult();
                isFirst = false;

                await frameFormula.FadeTo(100, 200);
            };
            stackLayoutMain.Children.Add(btnSolve);
        }

        void DisplayFirstLineResult()
        {

            Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblDelta, 0, 1);

            Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblEqual, 1, 1);

            firstEntryResult = StringToNumeric(RemovePow(firstEntry.Text));
            firstEntryResult = Math.Pow(firstEntryResult, 2);

            Label lblFirstEntry = new Label() { Text = firstEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblFirstEntry, 2, 1);

            Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblMinus, 3, 1);

            Label lblFour = new Label() { Text = "4", HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblFour, 4, 1);

            Label lblMult = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
            gridFrame.Children.Add(lblMult, 5, 1);

            Label lblMidEntry = new Label() { Text = midEntry.Text, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblMidEntry, 6, 1);

            Label lblMult2 = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
            gridFrame.Children.Add(lblMult2, 7, 1);

            Label lblLastEntry = new Label() { Text = lastEntry.Text, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Orange, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblLastEntry, 8, 1);

        }

        void DisplaySecondLineResult()
        {
            Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblDelta, 0, 2);

            Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblEqual, 1, 2);

            Label lblFirstEntry = new Label() { Text = firstEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblFirstEntry, 2, 2);

            Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblMinus, 3, 2);

            midEntryResult = StringToNumeric(midEntry.Text);
            midEntryResult = 4 * midEntryResult;
            
            Label lblMidEntry = new Label() { Text = midEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblMidEntry, 4, 2);

            Label lblMult2 = new Label() { Text = "*", TextColor = Color.Black, HorizontalTextAlignment = TextAlignment.Center };
            gridFrame.Children.Add(lblMult2, 5, 2);

            Label lblLastEntry = new Label() { Text = lastEntry.Text, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Orange, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblLastEntry, 6, 2);
        }

        void DisplayThirdLineResult()
        {
            Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblDelta, 0, 3);

            Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblEqual, 1, 3);

            Label lblFirstEntry = new Label() { Text = firstEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblFirstEntry, 2, 3);

            Label lblMinus = new Label() { Text = minus, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblMinus, 3, 3);

            lastEntryResult = StringToNumeric(lastEntry.Text);
            midAndlastEntryResult = midEntryResult * lastEntryResult;

            Label lblMidEntry = new Label() { Text = midAndlastEntryResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Purple, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblMidEntry, 4, 3);
        }

        void DisplayDeltaResult()
        {
            Label lblDelta = new Label() { Text = delta, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Blue, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblDelta, 0, 4);

            Label lblEqual = new Label() { Text = equal, HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Black, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblEqual, 1, 4);

            deltaResult = firstEntryResult - midAndlastEntryResult;

            Label lblFirstEntry = new Label() { Text = deltaResult.ToString(), HorizontalTextAlignment = TextAlignment.Center, TextColor = Color.Green, HorizontalOptions = LayoutOptions.Center };
            gridFrame.Children.Add(lblFirstEntry, 2, 4);
        }

标签: c#xamarinxamarin.formsreusability

解决方案


推荐阅读