【问题标题】:How to access activity Argument within ActivityDesigner?如何在 ActivityDesigner 中访问活动参数?
【发布时间】:2019-05-30 12:57:38
【问题描述】:

我需要在ActivityDesigner 处获取我的自定义活动的InArgument 值。

我的活动:

[Designer(typeof(ReadTextDesigner))]
public sealed class ReadText : CodeActivity
{
    public InArgument<string> ImageName { get; set; }

    protected override void Execute(CodeActivityContext context)
    {
    }
}

我的设计师:

public partial class ReadTextDesigner
{
    public ReadTextDesigner()
    {
        InitializeComponent();
        //this.ModelItem is null here.. WHY is it null?
        //How do I get Activity's ImageName here?
    }
}

我还有一个如下图所示的按钮,当您单击它时,我可以设置我的自定义 Activity 的值,如下所示:

private void BtnStart_OnClick(object sender, RoutedEventArgs e)
    {
        this.ModelItem?.Properties["ImageName"]?.SetValue(new InArgument<string>()
        {
            Expression = "some value"
        });
    }

XAML:

<sapv:ExpressionTextBox 
    Expression="{Binding Path=ModelItem.ImageName, Mode=TwoWay, Converter={StaticResource ArgumentToExpressionConverter}, ConverterParameter=In }"
    ExpressionType="s:String"
    HintText="Enter a string"
    OwnerActivity="{Binding Path=ModelItem}"
    Width="110"
    Margin="0,5"
    Grid.Row="0"
    MaxLines="1"
    x:Name="TxtImagePath"/>

<Button Grid.Column="0" Grid.Row="1" Content="Get Image" HorizontalAlignment="Center" Click="BtnStart_OnClick" x:Name="BtnStart"/>

如何获取 Activity InArgument ReadTextDesigner 构造函数?

【问题讨论】:

    标签: arguments workflow-foundation activitydesigner


    【解决方案1】:

    这很奇怪,但我找到了解决方法。虽然这是一个解决方案,但我希望有一个更好的解决方案;

    由于在构造函数中,我无法获得ModelItem,因此我在主线程之外创建了一个新的Thread。这个新线程等待 2 毫秒,然后尝试获取 ModelItem 并以某种方式成功。

    这里是新修改的ReadTextDesigner代码(注意:我只修改了ReadTextDesigner代码没有别的)

    public ReadTextDesigner()
    {
        InitializeComponent();
    
        new TaskFactory().StartNew(() => { this.Dispatcher.Invoke(() => SetImage(this)); });
    }
    
    private void SetImage(ReadTextDesigner designer)
    {
        Thread.Sleep(2);
        if (designer.ModelItem.GetCurrentValue() is ReadText readText)
        {
            var imageName = readText.ImageName?.Expression?.Convert<string>();
            if (!string.IsNullOrWhiteSpace(imageName))
            {
                //imageName has a value at this point!
            }
        }
    }
    

    ModelItem 不再为 null 并带有必要的值。 希望这会帮助某人或某人发布更好的解决方案。

    干杯!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      相关资源
      最近更新 更多