首页 \ 问答 \ 如何以编程方式为Eclipse UI命令设置初始状态?(How to programmatically set an initial state for an Eclipse UI command?)

如何以编程方式为Eclipse UI命令设置初始状态?(How to programmatically set an initial state for an Eclipse UI command?)

我正在扩展org.eclipse.ui.commands以提供一个新的命令给我的menuContribution ,它有一个toggle样式。

现在我想设置一个初始状态,但它不应该是一个硬编码状态。 因此,提供以下状态不是一种选择:

  <state
         class="org.eclipse.ui.handlers.RegistryToggleState:true"
         id="org.eclipse.ui.commands.toggleState">
   </state>

国家应该在偏好页面中反映一个属性,不需要坚持任何东西。 通过调用插件Activator中的方法手动设置状态不起作用,因为一旦我第一次点击该命令,我就会跳入start方法,但这已经太晚了,因为上下文菜单应该已经反映了正确的偏好属性的状态。

我该如何解决这个问题?


I am extending org.eclipse.ui.commands to provide a new command to my menuContribution which has a toggle style.

Now I want to set an initial state, but it should not be a hard-coded state. Consequently, providing the following state is not an option:

  <state
         class="org.eclipse.ui.handlers.RegistryToggleState:true"
         id="org.eclipse.ui.commands.toggleState">
   </state>

The state should reflect a property in the preference page, there's no need to persist anything. Setting the state manually by invoking the methods in the plugin Activator does not work, because I will jump into the start method as soon as I click on the command for the first time, but that's too late since the context menu should already reflect the correct state of the preference property.

How can I solve this issue?


原文:https://stackoverflow.com/questions/41806379
更新时间:2023-05-27 17:05

最满意答案

问题在于,编译器期望FileInfo类型的项目,但是IList包含object

我把你的一些代码放到VS中,英文错误是...

'IList <object>'不包含'ToObservableCollection'的定义,并且最好的扩展方法重载'Extensions.ToObservableCollection <FileInfo>(IEnumerable <FileInfo>)'需要一个类型为'IEnumerable <FileInfo>'的接收器

这就是说,它能找到的最接近的扩展方法就是你写的那个,但是传递给它的类型是不正确的。 为了解决这个问题,我们只需要将你的objectFileInfo

这是一种方法(假设你的对象最初是作为FileInfos创建的)。

items.Cast<FileInfo>().ToObservableCollection()

The issue lies in the fact that the compiler is expecting items of type FileInfo but your IList contains objects.

I put some of your code into VS and the error in English is...

'IList<object>' does not contain a definition for 'ToObservableCollection' and the best extension method overload 'Extensions.ToObservableCollection<FileInfo>(IEnumerable<FileInfo>)' requires a receiver of type 'IEnumerable<FileInfo>'

This is saying that the closest extension method it could find is the one you wrote, but the type being passed to it is not right. To fix this we just need to cast your objects to FileInfos.

Here's one way to do that (assuming your objects were originally create as FileInfos).

items.Cast<FileInfo>().ToObservableCollection()

相关问答

更多