c# - Xamarin:如何重用标签和条目以避免创建大量类
问题描述
我有一个项目有很多数学公式,我需要向用户展示:
我已经有一门课来解决基本的数学问题,比如求和、减法、除法、乘法等,然后我创建了一个字典来更改运算符,所以很好。
但是我有另一门课是我做公式的,大约 20 个公式,比如 Bhaskara、对数、几何序列等。所以我试图重用这些公式的条目和标签,因为如果我为每个公式创建公式,它会消耗很多类,或者一个类有很多行。
重复使用它的最佳方法是什么?考虑:
- 条目和标签屏幕中的空间(它们根据公式而变化)
- 要显示的条目和标签的数量,例如:Bhaskara 有 3 个条目(用于 B²、A 和 C),但立方体的体积是 1 个条目(V = s³),因此不会出现很少的条目/标签。
- 每个公式都会出现,如果我点击它出现,他们已经这样做了,例如:Solve Bhaskara 按钮将用 Bhaskara 解决一个页面,解决 Trigonometry 按钮将膨胀同一页面,但用三角来解决,等等,图片上面显示了一个例子。
这是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);
}
解决方案
推荐阅读
- swift - 要设置的字典
- windows - io.Copy 比调用 mv 命令慢很多
- ios - iOS 11 应用提交:“符号文件过多”
- css - 垂直调整窗口大小时,Div 容器消失
- python - 如何替换以 . (时期)?
- python - 从 Pycharm 解析 HDFS 文件
- python - ImportError:无法导入名称 MapTile
- flutter - Flutter 目录不是 GitHub 项目的克隆
- sql-server - TSQL 2012 服务器数据类型错误消息 varchar 转换浮点数(仅 SUM 值)
- mysql - Nginx Puma 和 Rails 5 负载测试失败