WPF Commands and Options

I believe the WPF command options are a limitation. Perhaps this is a sign that I am using them for other purposes, but I am still trying to do this before I refuse and take a different approach.

I put together a system to execute commands asynchronously , but it's hard to use anything that requires data entry. I know that one common template with WPF commands should go into this . But this will not work at all for asynchronous commands, because all dependency properties are then unavailable.

I get code like this:

 <Button Command="{Binding ElementName=servicePage, Path=InstallServiceCommand}"> <Button.CommandParameter> <MultiBinding Converter="{StaticResource InstallServiceParameterConverter}"> <MultiBinding.Bindings> <Binding ElementName="servicePage" Path="IsInstalled"/> <Binding ElementName="localURI" Path="Text"/> <Binding ElementName="meshURI" Path="Text"/> <Binding ElementName="registerWithMesh" Path="IsChecked"/> </MultiBinding.Bindings> </MultiBinding> </Button.CommandParameter> </Button> 

as well as the InstallServiceParametersConverter class (plus InstallServiceParameters).

Does anyone see an obvious way to improve this?

+4
source share
4 answers

Let me point out my open source project Caliburn. You can find it in here . The feature that will help solve your problem is briefly described here.

+2
source

Commands are designed to prevent a hard link between your user interface and program logic. Here you are trying to get around this, so you will find it painful. You want your user interface to be attached to another object (which contains this data), and your team can simply make a call to this object. Try to find MV-VM or look at an example of PRISM.

+1
source

Try using something like MVVM:

Create a class that stores all the data displayed in the current "view" (window, page, which makes sense for your application).

Associate your control with an instance of this class.

Ask the class to set some ICommand properties, bind the Button property to the corresponding property in the data class, you do not need to set the command parameter, because all the data has already been transferred to the object using normal daily data binding.

Get a class based on ICommand that calls you into an object, look at this link for a few implementations:

http://dotnet.org.za/rudi/archive/2009/03/05/the-power-of-icommand.aspx

Inside the method called by the command, pack all the necessary data and send it to the background thread.

+1
source

You need something that will allow you to request the appropriate object. Perhaps you only need an object to store these parameters, which your parent object can set as a property.

In fact, you should leave the commands synchronous and execute them asynchronously, dropping a new thread or passing them to the command manager (home rollled).

0
source

Source: https://habr.com/ru/post/1277304/


All Articles