如何测试字符串是表达式还是字符串?

时间:2013-03-09 05:23:17

标签: .net wpf c#-4.0 workflow-foundation-4

我无法尝试转换InArgument<String>以在自定义活动设计器中使用(重新托管)。我已经很好地为floatint等数据类型工作了。我们的想法是让用户以最直观的方式输入设置。例如,使用TextBox,用户可以输入文字数字(即9)或VB表达式(即变量num)。这是一个示例转换器:

public class InArgumentIntConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        ModelItem modelItem = value as ModelItem;
        if (modelItem != null)
        {
            InArgument<int> inArgument = modelItem.GetCurrentValue() as InArgument<int>;

            if (inArgument != null)
            {
                Activity<int> expression = inArgument.Expression;
                VisualBasicValue<int> vbexpression = expression as VisualBasicValue<int>;
                Literal<int> literal = expression as Literal<int>;

                if (literal != null)
                {
                    return literal.Value.ToString();
                }
                else if (vbexpression != null)
                {
                    return vbexpression.ExpressionText;
                }
            }
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string itemContent = (string)value;
        InArgument<int> inArgument = null;
        try
        {
            int literal = int.Parse(itemContent);
            inArgument = new InArgument<int>(literal);
        }
        catch (FormatException)
        {
            VisualBasicValue<int> vbArgument = new VisualBasicValue<int>(itemContent);
            inArgument = new InArgument<int>(vbArgument);
        }
        catch (OverflowException)
        {
            MessageBox.Show("Your number is too big!");
        }

        return inArgument;
    }
}

这里的关键似乎是我可以通过使用适当的Parse()方法检测文本框中的值是文字还是表达式,但似乎没有办法测试字符串是否是常规的旧字符串或表达式。

将值解析为文字会破坏表达式,但解析为表达式会强制文本框中值的引号用于文字值。

所以,我的问题是,如何测试表达式是文字字符串还是表达式?

修改

为了补充说明,我试图实现的最终结果是用户可以在Windows.Controls.TextBox中输入文字字符串或表达式,而不必使用文字值的引号。 (我试图添加图像以使其更清晰,但我想我还没有足够的声誉,所以ASCII艺术将不得不这样做。)

应该出现:

  +-------------------------+          +-----------------------------+
  | Some Literal value      |    or    | foo + "Some expression"     |
  +-------------------------+          +-----------------------------+

不应该显示为:(在解析为表达式时出现,即VisualBasicValue)

  +-------------------------+          +-----------------------------+
  | "Some Literal value"    |    or    | foo + "Some expression"     |
  +-------------------------+          +-----------------------------+

不应该显示为:(在解析为文字时发生,即文字)

  +-------------------------+          +-----------------------------+
  | Some Literal value      |    or    | "foo + "Some expression""   |
  +-------------------------+          +-----------------------------+

1 个答案:

答案 0 :(得分:0)

从您的代码中可以理解您正在做什么,但我想您在这里发明了这个轮子。

您使用的是普通的WPF TextBox吗?可以使用ExpressionTextBox来完成您所描述的内容。无需编写自定义转换器。

选中此示例:How to create a Custom Activity Designer with Windows Workflow Foundation (WF4)

相关问题