UWP从DependencyProperty回调中指定继承的Text值会引发异常

时间:2017-03-10 15:40:49

标签: c# uwp dependency-properties

我正在处理从TextBox继承的自定义输入控件,该控件将用于输入社会保险号。最终目标是使用掩码并显示项目符号而不是实际数字,同时将实际值存储在名为ActualValue的依赖项属性中。

我正在尝试将TextBox.Text值设置为实际值(稍后将换出格式化的隐藏字符)并且我在内核级别收到异常(如果我设置为托管和本机,则没有符号可用调试)如果我在TextChanging事件上有一个事件处理程序并绑定该值。

  • 没有TextChanging事件处理程序 - 没有异常
  • 不要在回调中设置文本 - 没有异常
  • 在标记中使用文字(例如123456)而不是绑定 - 无异常

之前我曾经使用过这种模式(甚至在UWP应用程序中),但我在这个例子中看到的唯一区别是它在新项目中,最小版本和目标版本都设置为周年纪念更新版本......我我不确定这与它有什么关系。

我不是在寻找最终目标的解决方案......请帮助解释为什么会抛出异常。

这是班级:

public class SSNInputBox : TextBox
{
    private static readonly string SSNMask = "___-__-____";

    public SSNInputBox()
    {
        DefaultStyleKey = typeof(TextBox);
        TextChanging += OnTextChanging;
    }

    #region Properties

    private void OnTextChanging(TextBox sender, TextBoxTextChangingEventArgs args)
    {

    }

    public string ActualValue
    {
        get { return (string)GetValue(ActualValueProperty); }
        set { SetValue(ActualValueProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ActualValue.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ActualValueProperty =
        DependencyProperty.Register("ActualValue", typeof(string), typeof(SSNInputBox), new PropertyMetadata(string.Empty, onActualValueChanged));

    private static void onActualValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((SSNInputBox)d).OnActualValueChanged(e);
    }

    protected virtual void OnActualValueChanged(DependencyPropertyChangedEventArgs e)
    {
        Text = e.NewValue.ToString();
    }

    #endregion
}

这是标记:

<local:SSNInputBox ActualValue="{Binding Path=CurrentApplicant.SSN, Mode=TwoWay}" />

更新 如果我将SSN移动到主视图模型而不是视图模型中的INotifyPropertyChanged对象的属性,它也可以工作。

INP课程:

public class Applicant : INotifyPropertyChanged
{
    private string _ssn;
    public string SSN
    {
        get { return _ssn; }
        set { Set(ref _ssn, value); }
    }

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    protected bool Set<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (Equals(storage, value)) return false;

        storage = value;
        this.RaisePropertyChanged(propertyName);
        return true;
    }

    protected void RaisePropertyChanged([CallerMemberName] string propertyName = null)
    {
        this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

}

ViewModel(在开发过程中从mainpage的代码后面调用方法)

public class MainViewModel : ViewModelBase
{
    private Applicant _currentApplicant = new Applicant();
    public Applicant CurrentApplicant
    {
        get { return _currentApplicant; }
        set { Set(ref _currentApplicant, value); }
    }

    private string _ssn = "123-45-6789";
    public string SSN
    {
        get { return _ssn; }
        set { Set(ref _ssn, value); }
    }

    public void LoadApplicant()
    {
        CurrentApplicant.SSN = "123-45-6789";
    }

    public void ChangeSSN()
    {
        CurrentApplicant.SSN = SSN = "987-65-4321";
    }
}

0 个答案:

没有答案