简化代码C#开关语句

时间:2018-08-29 00:48:52

标签: c# wpf

我已经写了很多switch case语句,但是我知道这可以简化,任何指导都会得到尊重。我对XAML相当陌生。

switch (e.Key)
{
    case Key.Escape:
        this.DialogResult = false;
        break;
    case Key.Return:
        this.DialogResult = true;
        break;
    case Key.Back:
        if (ResultValue != null && ResultValue.Length > 0)
            ResultValue = ResultValue.Remove(ResultValue.Length - 1);
        if (isUserAccess)
        {
            if (UserAccessPasswordValue != null && UserAccessPasswordValue.Length > 0)
                UserAccessPasswordValue = UserAccessPasswordValue.Remove(UserAccessPasswordValue.Length - 1);
        }
        break;
    case Key.Space:
        if (!CheckOutputLength(ResultValue)) return;
            ResultValue += " ";
        break;
    case Key.NumPad0:
    case Key.D0:             
    case Key.NumPad1:
    case Key.D1:
    case Key.NumPad2:
    case Key.D2:
    case Key.NumPad3:
    case Key.D3:
    case Key.NumPad4:
    case Key.D4:
    case Key.NumPad5:
    case Key.D5:
    case Key.NumPad6:
    case Key.D6:
    case Key.NumPad7:
    case Key.D7:
    case Key.NumPad8:
    case Key.D8:
    case Key.NumPad9:
    case Key.D9:
    case Key.A:
    case Key.B:
    case Key.C:
    case Key.D:
    case Key.E:
    case Key.F:
    case Key.G:
    case Key.H:
    case Key.I:
    case Key.J:
    case Key.K:
    case Key.L:
    case Key.M:
    case Key.N:
    case Key.O:
    case Key.P:
    case Key.Q:
    case Key.R:
    case Key.S:
    case Key.T:
    case Key.U:
    case Key.V:
    case Key.W:
    case Key.X:
    case Key.Y:
    case Key.Z:
        CheckandAddValue(e.Key.ToString());
        break;
}

我的尝试有太多错误。这是一个自定义键盘,可以检测用户输入。上面的代码有效,我能够获得所需的结果,但是我知道这可以变得更简单。

我的简化尝试

if ((e.Key >= Key.A && e.Key <= Key.Z) || (e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))

2 个答案:

答案 0 :(得分:2)

要处理的情况下要使用其他默认情况。

        switch (e.Key)
        {
            case Key.Escape:
                this.DialogResult = false;
                break;
            case Key.Return:
                this.DialogResult = true;
                break;
            case Key.Back:
                if (ResultValue != null && ResultValue.Length > 0)
                    ResultValue = ResultValue.Remove(ResultValue.Length - 1);
                if (isUserAccess)
                {
                    if (UserAccessPasswordValue != null && UserAccessPasswordValue.Length > 0)
                        UserAccessPasswordValue = UserAccessPasswordValue.Remove(UserAccessPasswordValue.Length - 1);
                }
                break;
            case Key.Space:
                if (!CheckOutputLength(ResultValue)) return;
                ResultValue += " ";
                break;
            case default :
                CheckandAddValue(e.Key.ToString());
                break;
        }

答案 1 :(得分:2)

您可以在Visual Studion 2017和2015中使用C#7模式匹配:

switch (e.Key)
{
    case Key.Escape:
        this.DialogResult = false;
        break;
    case Key.Return:
        this.DialogResult = true;
        break;
    case Key.Back:
        if (!string.IsNullOrEmpty(ResultValue))
            ResultValue = ResultValue.Remove(ResultValue.Length - 1);

        if (isUserAccess)
        {
            if (!string.IsNullOrEmpty(UserAccessPasswordValue))
                UserAccessPasswordValue = UserAccessPasswordValue.Remove(UserAccessPasswordValue.Length - 1);
        }
        break;
    case Key.Space:
        if (!CheckOutputLength(ResultValue)) return;
        ResultValue += " ";
        break;
    case var k when k >= Key.D0 && k <= Key.NumPad9
               && !(k >= Key.LWin && k <= Key.Sleep):
        CheckandAddValue(e.Key.ToString());
        break;
}