Caliburn Micro void函数无法在DialogHost内部触发 [英] Caliburn Micro void function can't be triggered inside DialogHost

查看:69
本文介绍了Caliburn Micro void函数无法在DialogHost内部触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,您可以通过以下简单方法在View中调用void函数(来自ViewModel):

Normally you can call void function(from ViewModel) in View by simply:

按钮名称= VoidFunctionInViewModel

Button Name = VoidFunctionInViewModel

按钮命令= {绑定路径= VoidFunctionInViewModel}

Button Command={Binding Path=VoidFunctionInViewModel}

但是当在DialogHost中访问此函数时,不会触发void函数.

But when accessing this function inside a DialogHost, the void function isn't triggered.

我尝试将一个字符串字段检索到DialogHost上,它可以正常工作,但是当涉及到按钮上的命令时,它将不起作用.

I have tried retrieving a string field to the DialogHost and it works fine, but when it comes to commands on buttons it doesn't work.

MainViewModel.cs命令:

MainViewModel.cs Commands:

public async void OpenDialog()
   {
       var confirm = new ConfirmationView{DataContext = this};
       await DialogHost.Show(confirm);
   }
   public void Accept()
   {
       Console.WriteLine("It Failed!");
   }
   public string Success {get; set;} = "Success"

ConfirmationView.xaml:

ConfirmationView.xaml:

<Button Command="{Binding Path=Accept}" Content="Accept"/>
<Button Command="{x:Static materialDesign:DialogHost.CloseDialogCommand}" Content="Cancel"/>
<TextBlock Name="Success"/>

MainView.xaml:

MainView.xaml:

<materialDesign:DialogHost DialogTheme="Inherit" CloseOnClickAway="True">
</materialDesign:DialogHost>

成功"属性已成功使用,并由DialogHost显示.但是带有接受"命令的按钮接受"不是由DialogHost触发的.

The Property "Success" is successfully used and shown by the DialogHost. But the Button "Accept" with the command Accept isn't triggered by the DialogHost.

使用MaterialDesign的命令,取消"按钮可以正常工作.

The Button "Cancel" is working, with the command from materialDesign.

推荐答案

对于caliburn.micro来说应该这样吗?

Shouldnt it be this way for caliburn.micro?

<Button x:Name="Accept" Content="Accept"/>

仅在ViewModel中使用ICommand或RelayCommand才能使用Command ="语法,或者?至少到目前为止,这就是我对caliburn.micro的理解.

Using the Command="" syntax works only by using ICommand or RelayCommand in the ViewModel, or? At least that is how i understood caliburn.micro so far.

如果这不起作用,您可以尝试一下.这在抽屉主机中对我有用,在其中caliburn.micro命令绑定对我而言失败:

If that doesnt work, you could try this. This worked for me in the drawerHost, where caliburn.micro Command binding failed for me:

<Button cal:Message.Attach="[Event Click] = [Action Accept()]" Content="Accept" />

其他带有对话框示例的资源,可能会有用:

other sources with dialog examples, which might be usefull:

https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/wiki/Dialogs

https://github.com/Keboo/MaterialDesignInXaml.Examples

这篇关于Caliburn Micro void函数无法在DialogHost内部触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆