简体   繁体   English

我输入到TextBox中的数据未分配给UWP应用中的value变量

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

I'm working on a simple UWP app, using Template 10. I want to enter monetary data into a TextBox. 我正在使用模板10开发一个简单的UWP应用。我想将货币数据输入到TextBox中。 It's my understanding that I should use a string variable in the View-Model. 据我了解,我应该在视图模型中使用字符串变量。 So, for the moment I'm just making sure that the data I enter, when running the app, actually works. 因此,目前,我只是确保在运行应用程序时输入的数据确实有效。 But it doesn't. 但事实并非如此。 When running or debugging it, and if I enter something like "10" (without the double quotes), what the variable value is assigned is "0". 在运行或调试它时,如果我输入类似“ 10”的内容(不带双引号),则分配的变量值为 “ 0”。 Which doesn't make sense to me. 这对我来说没有意义。 Here's the XAML: 这是XAML:

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

And here's the code from the View-Model: 这是视图模型中的代码:

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

Here's the Code-behind code: 这是代码隐藏代码:

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
    }
}

Thanks to Richard Zhang's suggestion of looking at my code-behind, I discovered there that I had previously written some code to handle the controls on the screen. 感谢Richard Zhang提出的查看我的代码背后的建议,我发现在那里我以前已经编写了一些代码来处理屏幕上的控件。 It was this code which was resetting the values to 0 (indirectly). 正是此代码将值重置为0(间接)。 I had written that code a while ago, so I'd forgotten all about it. 我之前写过该代码,所以我全都忘了。

Thank you, Richard, for making that suggestion. 理查德,谢谢你的建议。 It helped me see what I had done and after reviewing it I was able to easily resolve it. 它帮助我了解所做的事情,并且在查看后可以轻松解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM