工作流程 4 活动设计器 IValueConverter

发布于 2024-10-11 17:31:22 字数 332 浏览 5 评论 0原文

假设我有一个与 InArgument的活动ProductId

我想在活动设计器中公开一个组合框来显示所有产品,并且用户可以选择一个产品。

我可以毫无问题地显示组合中的产品列表。但是我如何将所选产品绑定到 InArgument我的自定义活动?

我想我需要某种 ValueConverter 吗?不知道如何为这种情况编写值转换器,如果有人有想法、建议,将会有所帮助。我必须将 InArgument转换为到一个整数?并将 int 转换回 InArgument

谢谢,

Lets say i have an activity with InArgument<int> ProductId

I'd like to expose in the activity designer a combobox to show all Products and the user can select a product.

I can show the list of product in the combo no problem. But how do I bind the selected product to the InArgument<int> of my custom activity?

I suppose I need some kind of ValueConverter? Not sure how to code the value converter for this case, if anybody has an idea, suggestion, will be helpful. I have to convert the InArgument<int> to an int? and the convert back from int to InArgument<int>

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-10-18 17:31:22
public class ArgumentToInt32Converter: IValueConverter {
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        object convertedValue = null;
        if (value != null) {
            ModelItem argumentModelItem = value as ModelItem;
            if (argumentModelItem != null && argumentModelItem.Properties["Expression"] != null && argumentModelItem.Properties["Expression"].Value != null) {
                if (argumentModelItem.Properties["Expression"].ComputedValue.GetType() == typeof(Literal <Int32> )) {
                    convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as Literal <Int32> ).Value;
                } else {
                    convertedValue = null
                }
            }
        }
        return convertedValue;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        // Convert Int32 value to InArgument<Int32>
        Int32 itemContent = (Int32) value;
        VisualBasicValue <Int32> vbArgument = new VisualBasicValue <Int32> (itemContent);
        InArgument <Int32> inArgument = new InArgument <Int32> (vbArgument);
        return inArgument;
    }
}

修改自此回答

public class ArgumentToInt32Converter: IValueConverter {
    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        object convertedValue = null;
        if (value != null) {
            ModelItem argumentModelItem = value as ModelItem;
            if (argumentModelItem != null && argumentModelItem.Properties["Expression"] != null && argumentModelItem.Properties["Expression"].Value != null) {
                if (argumentModelItem.Properties["Expression"].ComputedValue.GetType() == typeof(Literal <Int32> )) {
                    convertedValue = (argumentModelItem.Properties["Expression"].ComputedValue as Literal <Int32> ).Value;
                } else {
                    convertedValue = null
                }
            }
        }
        return convertedValue;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) {
        // Convert Int32 value to InArgument<Int32>
        Int32 itemContent = (Int32) value;
        VisualBasicValue <Int32> vbArgument = new VisualBasicValue <Int32> (itemContent);
        InArgument <Int32> inArgument = new InArgument <Int32> (vbArgument);
        return inArgument;
    }
}

Modified from this answer

萝莉病 2024-10-18 17:31:22

这是我尝试为此制定一个更通用的解决方案。我有几个属性 - 一些 IEnumerable、一些字符串、一些 int,并且为每个属性创建一个值转换器似乎是错误的方法。我很想知道我在这里没有发现哪些案例,因为我对 WF 比较陌生。希望这对某人有帮助。

public class ArgumentConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object convertedValue = null;
        if(value != null)
        {
            var argumentModelItem = value as ModelItem;

            if(argumentModelItem != null)
            {
                ModelProperty argumentModelProperty = argumentModelItem.Properties["Expression"];

                if(argumentModelProperty != null && argumentModelProperty.Value != null)
                {
                    var computedValue = argumentModelProperty.ComputedValue;

                    var activity = (Activity) computedValue;
                    convertedValue = WorkflowInvoker.Invoke(activity)["Result"];
                }
            }
        }
        return convertedValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // here targetType should be InArgument<T>
        // assume a single generic argument
        Type arg0 = targetType.GetGenericArguments()[0];

        ConstructorInfo argConstructor = targetType.GetConstructor(new[] {arg0});

        var argument = argConstructor.Invoke(new[] { value });

        return argument;
    }

    #endregion
}

This is my attempt at making a more generic solution to this. I have several properties - some IEnumerable, some string, some int, and to make a value converter for each seems like the wrong approach. I'd be interested to know what cases I haven't caught here because I am relatively new to WF. Hopefully this helps someone.

public class ArgumentConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        object convertedValue = null;
        if(value != null)
        {
            var argumentModelItem = value as ModelItem;

            if(argumentModelItem != null)
            {
                ModelProperty argumentModelProperty = argumentModelItem.Properties["Expression"];

                if(argumentModelProperty != null && argumentModelProperty.Value != null)
                {
                    var computedValue = argumentModelProperty.ComputedValue;

                    var activity = (Activity) computedValue;
                    convertedValue = WorkflowInvoker.Invoke(activity)["Result"];
                }
            }
        }
        return convertedValue;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        // here targetType should be InArgument<T>
        // assume a single generic argument
        Type arg0 = targetType.GetGenericArguments()[0];

        ConstructorInfo argConstructor = targetType.GetConstructor(new[] {arg0});

        var argument = argConstructor.Invoke(new[] { value });

        return argument;
    }

    #endregion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文