2013-03-09 28 views
0

我在嘗試轉換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"" | 
    +-------------------------+   +-----------------------------+ 

回答

0

從你的代碼不完全可以理解你在做什麼,bu我想你是在這裏發明車輪。

您是否正在使用正常的WPF文本框?有一個ExpressionTextBox這正是你所描述的。無需編寫自定義轉換器。

檢查這個例子:How to create a Custom Activity Designer with Windows Workflow Foundation (WF4)

+0

感謝您的答覆@jota,但的ExpressionTextBox並沒有真正解決我試圖解決這個問題。我編輯了我的問題,試圖讓它更加清楚我正在尋找的行爲。 – Josh 2013-03-10 02:40:40

相關問題