在 Caliburn Micro 中发送布尔值作为操作参数

发布于 2024-12-13 19:39:55 字数 668 浏览 5 评论 0原文

这是我的 XAML 视图(为了可读性省略了一些代码):

<Window ... xmlns:c="http://www.caliburnproject.org">
  <Button Content="Close without saving" c:Message.Attach="Close(false)" />
  <Button Content="Save and Close" c:Message.Attach="Close(true)" />
</Window>
And here's the code in the ViewModel:
public void Close(bool save) 
{
  if (save) 
  { 
    // save the data 
  }
  TryClose();
}
This doesn't work - of course - because the action parameters "true" and "false" aren't objects or object properties in the XAML. How can I make this work, and send a boolean as an Action parameter in Caliburn Micro?

This is my XAML View (some code omitted for readability):

<Window ... xmlns:c="http://www.caliburnproject.org">
  <Button Content="Close without saving" c:Message.Attach="Close(false)" />
  <Button Content="Save and Close" c:Message.Attach="Close(true)" />
</Window>

And here's the code in the ViewModel:

public void Close(bool save) 
{
  if (save) 
  { 
    // save the data 
  }
  TryClose();
}

This doesn't work - of course - because the action parameters "true" and "false" aren't objects or object properties in the XAML. How can I make this work, and send a boolean as an Action parameter in Caliburn Micro?

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

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

发布评论

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

评论(2

み格子的夏天 2024-12-20 19:39:55

如果您在参数名称两边加上单引号,它会为您正确转换。

<Button Content="Close without saving"
        c:Message.Attach="Close('false')" />
<Button Content="Save and Close"
        c:Message.Attach="Close('true')" />

If you put single quotes around the parameter name, it will properly convert for you.

<Button Content="Close without saving"
        c:Message.Attach="Close('false')" />
<Button Content="Save and Close"
        c:Message.Attach="Close('true')" />
披肩女神 2024-12-20 19:39:55

您可以尝试使用交互+触发器:

<i:Interaction.Triggers>
            <i:EventTrigger  EventName="Click">
                <cl:ActionMessage  MethodName="MyMethod" >
                    <cl:Parameter Value="True">

                    </cl:Parameter>
                </cl:ActionMessage>
            </i:EventTrigger>
        </i:Interaction.Triggers>

You can try to use interactivity + triggers:

<i:Interaction.Triggers>
            <i:EventTrigger  EventName="Click">
                <cl:ActionMessage  MethodName="MyMethod" >
                    <cl:Parameter Value="True">

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