如何解决如何使用 XAML 按钮在 NumberBox 对象中输入表达式? (C++/XAML)
我正在使用 C++ 和 XAML(Windows 10 上的 Visual Studio 2019)创建 UWP 计算器。我创建了一个 NumberBox 对象,我想让我的按钮将它们的值输入到对象中。我试图将它们关联一些加速器功能(例如按钮 1 = ASCII 1
),并将加速器 ID 设置为它们的 Click 功能。
我的(示例)代码 (MainPage.xaml.cpp):
int MainPage::Button_Click_1()
{
return CW_1; // Returns number 1
}
不幸的是,当我按下按钮时,NumberBox 对象取消选择自身,因此按钮无法插入其值。
我该如何解决这个问题?
编辑:
对于任何有同样问题的人,我找到了让它工作的方法。
这是一个丑陋的方法,但它有效:我已经创建了一些变量(分别为 double num1 = 0'
、char sym = 'N'
和 num2 = 0
)并且我创建了 <NumberBox>
对象({{1} }) 与他们一起操作。然后我创建了一个 x:Name="NumBox1"
对象 (<TextBox>
),它在成功的情况下可视化符号或在语法错误的情况下显示字符串 x:Name="SymVisualizer"
。
<Button>
这里有一个示例 int ProjectName::MainPage::Button_Click_1(Platform::Object^ sender,Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (SymVisualizer->Text == "err") // Clears the TextBox content if the prevIoUs expression gave an error
{
SymVisualizer->Text = "";
}
if (sym == 'N') // If the symbol isn't set,the value is assigned to num1
{
if (num1 != NumBox1->Value && NumBox1->Value > 0) // Syncronizes the NumBerBox value with num1
{
num1 = NumBox1->Value;
}
if (((num1 * 10) + 1) > 999999999999) // If inserting a number will create an error (due to "double" type capability),the number will not be inserted
{
}
else // If inserting a number doesn't create any problem,inserts the number
{
num1 = (num1 * 10) + 1; // Slides the digits and adds 1
}
NumBox1->Value = num1; // Shows num1 as NumberBox Value
}
if (sym == '+' || sym == '-' || sym == '*' || sym == '/') // If the symbol is set,the value is assigned to num2
{
if (num2 != NumBox1->Value && NumBox1->Value > 0) // Synchronizes the NumBerBox value with num1
{
num2 = NumBox1->Value;
}
if (((num2 * 10) + 1) > 999999999999) // If inserting a number will create an error (due to "double" type capability),inserts the number
{
num2 = (num2 * 10) + 1; // Slides the digits and adds 1
}
NumBox1->Value = num2; // Shows num1 as NumberBox Value
}
return 0;
}
代码(将符号插入框中):
<Button>
最后,计算并显示结果的 char ProjectName::MainPage::Button_Click_Plus(Platform::Object^ sender,Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (sym == 'N' && num1 != 0) // If sym value is unchanged and num1 is different from 0,sets the symbol
{
num1 = NumBox1->Value; // Synchronizes num1 with NumberBox value
sym = '+'; // Sets the symbol
SymVisualizer->Text = "+"; // Shows the symbol in the TextBox Object
NumBox1->Value = num2; // Shows num2's value (which is 0 at this point)
}
if ((sym == '+' || sym == '-' || sym == '*' || sym == '/'),num1 == 0) // If the symbol is already set,does nothing
{
}
return 0;
}
(我创建了一些函数,如 <Button>
、Addition()
、Subtraction()
和 Multiplication()
用于计算结果):
Division()
我希望这对某人有用(对不起,英语不好,这不是我的主要语言)并感谢给出的答案。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。