Caliburn Micro void function can't be triggered inside DialogHost

烂漫一生 提交于 2021-01-29 08:33:05

问题


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

Button Name = VoidFunctionInViewModel

Button Command={Binding Path=VoidFunctionInViewModel}

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

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 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:

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

MainView.xaml:

<materialDesign:DialogHost DialogTheme="Inherit" CloseOnClickAway="True">
</materialDesign: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.

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


回答1:


Shouldnt it be this way for caliburn.micro?

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

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.

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



来源:https://stackoverflow.com/questions/58400056/caliburn-micro-void-function-cant-be-triggered-inside-dialoghost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!