0

在 windows phone 8 中,如果我选择了手机文本框并在其中输入了一些文本,我想屏蔽此文本,如何实现?

4

2 回答 2

1

我用这段代码解决了我的问题:

在 xml 中:

<toolkit:PhoneTextBox Margin="-20,48,0,0"                              
                            InputScope="TelephoneAreaCode"
                            Style="{StaticResource PhoneTextBoxStyleGreen}"                                              
                            x:Name="PhoneNumber" Height="72" VerticalAlignment="Top" Width="200" MaxLength="10" Hint="Ceular"/>

视图模型:

private string _phoneNumber;
    public string PhoneNumber
            {
                get
                {                
                    return FormattedPhoneNumber(_phoneNumber);                
                }
                set
                {               
                    _phoneNumber = value;                       
                    NotifyOfPropertyChange(() => PhoneNumber);                
                }
            }

public string FormattedPhoneNumber(string pn)
        {
            if (pn == null)
                return string.Empty;

            switch (pn.Length)
            {
                case 5:
                    {
                        if (!pn.Contains("-"))
                        {
                            return Regex.Replace(pn, @"(\d{4})(\d{1})", "$1-$2");
                        }
                        else
                        {
                            return pn.Replace("-", String.Empty);
                        }

                    }
                case 9:
                    {
                        var sbPhoneNumber = pn.Replace("-",String.Empty);
                        return Regex.Replace(sbPhoneNumber, @"(\d{4})(\d{4})", "$1-$2");
                    }
                case 10:
                    {
                        var sbPhoneNumber = pn.Replace("-", String.Empty);
                        return Regex.Replace(sbPhoneNumber, @"(\d{5})(\d{4})", "$1-$2");                        
                    }                
                default:
                    {                        
                        return pn;
                    }
            }            
        }

我的面具:如果 8 个数字:8888-8888 如果 9 个数字:00000-0000

于 2014-10-10T19:08:52.780 回答
0

在您的 .XAML 文件中,在 TextBox 上添加 TextChanged 事件

<TextBox Name="tvCelular" TextChanged="OnTextCelularChanged" MaxLength="14" InputScope="Number"/>

创建一个类 Mask.cs

/// <summary>
/// created by MAYCON CARDOSO
/// </summary>
public class Mask {
    private bool isUpdating = false;
    private string old = "";

    public String unmask(String s) {
        return s.Replace(".", "").Replace("-", "")
                .Replace("/", "").Replace("(", "")
                .Replace(")", "");
    }

    public void performFilter(string mask, TextBox ediTxt) {
        string s = ediTxt.Text;

        String str = unmask(s);

        String mascara = "";
        if (isUpdating) {
            old = str;
            isUpdating = false;
            return;
        }
        int i = 0;
        foreach(char m in mask.ToCharArray()) {
            if (m != '#' && str.Length != old.Length) {
                mascara += m;
                continue;
            }
            try {
                mascara += str[i];
            }
            catch (Exception e) {
                break;
            }
            i++;
        }

        isUpdating = true;
        ediTxt.Text  = mascara;
        ediTxt.SelectionStart  = mascara.Length;
    }
}

在您的 cs 文件中添加此代码

 private void OnTextCelularChanged(object sender, TextChangedEventArgs e) {
        mCelularMask.performFilter("(##)####-#####",tvCelular);    
    }
于 2016-08-08T14:30:11.633 回答