在Caliburn中使用TagProperty.微ActionMessage

本文关键字:TagProperty ActionMessage Caliburn | 更新日期: 2023-09-27 18:13:12

在我的WPF应用程序中,我使用Caliburn.Micro。在SampleView中我有这样的代码

<Button Tag="1" Content="Execute" cal:Message.Attach="[Event Click] = [Action Execute]"/>

当然在SampleViewModel中我有这样的方法:

public void Execute() {
    //do something
}

,但现在我想从TagProperty传递到执行方法值,我发现(在xaml)的唯一解决方案是:

<Button Content="Execute" Tag="1" cal:Message.Attach="[Event Click] = [Action Execute($source})]"/>

and in ViewModel:

public void Execute(FrameworkElement elem) {
    var tag=elem.Tag;
    //do something
}

在我看来,使用FrameworkElement在ViewModel是不好的做法。也许有人知道更好的解决办法?

在Caliburn中使用TagProperty.微ActionMessage

$this。标签应该在你的Execute方法中为你工作,可以只是一个对象类型,把它转换成你需要的(字符串?)。

public void Execute(object p){
   if(p != null){ 
     var tag = (string)p;
   }
}