C# WPF 命令和参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/151700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 15:46:14  来源:igfitidea点击:

WPF Commands and Parameters

提问by nedruod

I'm finding the WPF command parameters to be a limitation. Perhaps that's a sign that I'm using them for the wrong purpose, but I'm still giving it a try before I scrap and take a different tack.

我发现 WPF 命令参数是一个限制。也许这表明我将它们用于错误的目的,但在我放弃并采取不同的策略之前,我仍在尝试。

I put together a system for executing commands asynchronously, but it's hard to use anything that requires data input. I know one common pattern with WPF commands is to pass in this. But thiswill not work at all for asynchronous commands because all the dependency properties are then inaccessible.

我组装了一个异步执行命令的系统,但很难使用任何需要数据输入的东西。我知道 WPF 命令的一种常见模式是传入this. 但是this对于异步命令根本不起作用,因为所有依赖属性都无法访问。

I end up with 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>

and also need the InstallServiceParametersConverter class (plus InstallServiceParameters).

并且还需要 InstallServiceParametersConverter 类(加上 InstallServiceParameters)。

Anyone see an obvious way to improve upon this?

有人看到改善这一点的明显方法吗?

回答by Orion Adrian

You need something that will allow you to request the proper object. Perhaps you need an object just for storing these parameters that your parent object can expose as a property.

您需要一些可以让您请求正确对象的东西。也许您需要一个对象来存储您的父对象可以作为属性公开的这些参数。

Really what you should do is leave the commands synchronous and execute them asynchronously by throwing off a new thread or passing them to a command manager (home rolled).

你真正应该做的是让命令保持同步并通过抛出一个新线程或将它们传递给命令管理器(家庭滚动)来异步执行它们。

回答by EisenbergEffect

Let me point you to my open source project Caliburn. You can find it at here. The feature that would most help solve your problem is documented briefly here

让我向您指出我的开源项目 Caliburn。你可以在这里找到它。最有助于解决您的问题的功能在此处简要记录

回答by EisenbergEffect

Commands are for avoid tight coupling between your UI and program logic. Here, you're trying to get around that so you'll find it painful. You want to have your UI bound to some other object (that contains this data) and your command can then simply make a call to that object. Try searching for MV-V-M, or look at the PRISM example.

命令是为了避免 UI 和程序逻辑之间的紧密耦合。在这里,你试图解决这个问题,所以你会发现它很痛苦。您希望将 UI 绑定到某个其他对象(包含此数据),然后您的命令可以简单地调用该对象。尝试搜索 MV-VM,或查看 PRISM 示例。

回答by Nir

Try using something like MVVM:

尝试使用类似 MVVM 的东西:

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

创建一个类,用于存储当前“视图”(窗口、页面,任何对您的应用程序有意义)中显示的所有数据。

Bind your control to an instance of this class.

将您的控件绑定到此类的实例。

Have the class expose some ICommand properties, bind the button's Command property to the appropriate property in the data class, you don't need to set the command parameter because all the data has already been transfered to the object using normal everyday data binding.

让类暴露一些 ICommand 属性,将按钮的 Command 属性绑定到数据类中的相应属性,您不需要设置 command 参数,因为所有数据已经​​使用正常的日常数据绑定传输到对象。

Have an ICommand derived class that calls back into you object, look at this link for several implementations:

有一个 ICommand 派生类回调你的对象,看看这个链接的几个实现:

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

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

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

在命令调用的方法中,打包所有需要的数据并将其发送到后台线程。