我在 TextBox 中输入的数据未分配给 UWP 应用程序中的值变量

The data I enter into a TextBox is not being assigned to the value variable in a UWP app

我正在使用模板 10 开发一个简单的 UWP 应用程序。我想将货币数据输入文本框。据我了解,我应该在视图模型中使用字符串变量。因此,目前我只是确保我在 运行 应用程序时输入的数据确实有效。但事实并非如此。当 运行 或调试它时,如果我输入类似“10”的内容(不带双引号),则变量 value 分配的是“0”。这对我来说没有意义。这是 XAML:

<TextBox
    x:Name="HourlyTextBox"
    Style="{StaticResource CommonTextboxStyle}"
    Text="{x:Bind ViewModel.Hourly, Mode=TwoWay}" />

这是来自视图模型的代码:

private string hourly;
public string Hourly
{
    get => hourly;
    set
    {
         _ = Set(ref hourly, value);
    }
}

这是代码隐藏代码:

using Windows.UI.Xaml.Controls;
using SalaryConv;

namespace SalaryConversion.Views
{
    public sealed partial class MainPage : Page
    {
        private SalaryUnitsEnum lastHadFocus;

        public MainPage()
        {
            InitializeComponent();
            NavigationCacheMode = 
Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }


        #region GettingFocus events

        private void HourlyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Hourly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Hourly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void WeeklyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Weekly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Weekly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void BiWeeklyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.BiWeekly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.BiWeekly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void SemiMonthlyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.SemiMonthly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.SemiMonthly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void MonthlyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Monthly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Monthly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void AnnuallyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Annually)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Annually;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        #endregion

        #region ClearOtherMonetaryTextboxes helper method

        private void ClearOtherMonetaryTextboxes(SalaryUnitsEnum lastHadFocus)
        {
            if (lastHadFocus != SalaryUnitsEnum.Hourly)
            {
                HourlyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.Weekly)
            {
                WeeklyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.BiWeekly)
            {
                BiWeeklyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.SemiMonthly)
            {
                SemiMonthlyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.Monthly)
            {
                MonthlyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.Annually)
            {
                AnnuallyTextBox.Text = "0";
            }
        }

        #endregion
    }
}

感谢 Richard Zhang 查看我的代码隐藏的建议,我在那里发现我之前已经编写了一些代码来处理屏幕上的控件。正是这段代码将值重置为 0(间接)。我不久前写过那段代码,所以我把它全忘了。

谢谢你,理查德,提出这个建议。它帮助我了解了我所做的事情,并且在审查之后我能够轻松地解决它。