如何在XAML中引用自定义命令? [英] How do you reference a custom command in XAML?

查看:74
本文介绍了如何在XAML中引用自定义命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF中这是一个非常简单的事情,但我在Metro中找不到模拟。我的框架窗口上有几个按钮。其中一个按钮是切换,可将视图从缩略图更改为详细信息。 这些命令是应用程序
的全局命令,并且位于名为"GlobalCommands"的类中。 请注意,全局命令被其他类使用,因此无法在XAML中创建一个 命令实例。 在WPF中,按钮看起来像这样:

< AppBarButton Command =" {x:Static local:GlobalCommands.ChangeView}" 
CommandParameter =" Thumbnail"
Label =" Switch View">


但是,在Metro中,没有x:静态,没有TypeConverters,IValueConverters只能用于绑定,所以我很难过。 如何在具有非常特定(即不是从视图模型派生)命令的应用程序中放置按钮?

解决方案

我担心您无法绑定到Windows应用商店应用中的静态属性。它根本不受支持。


您可以创建一个公开静态命令属性并绑定到代理对象实例的此属性的代理类:


http://stackoverflow.com/questions/ 14186175 / bind-to-a-static-field-in-windows-8-xaml

http://stackoverflow.com/questions/4708711/how-can-i-use-the-xstatic-extension-for-phone7 -silverlight-apps


您当然必须创建代理对象的实例。没有办法解决这个问题。


请记住将有用的帖子标记为关闭线程的答案,然后在遇到新问题时启动新线程。


This is a ridiculously easy thing to do in WPF, but I can't find the analog in Metro. I have several buttons on my frame window. One of the buttons is a toggle to change the view from thumbnails to details.  The commands are global to the application and live in a class called 'GlobalCommands'.  Note that the Global Commands are consumed by other classes so there's no way that creating an instance of a command in XAML is going to work.  In WPF, the button would look like this:

<AppBarButton Command="{x:Static local:GlobalCommands.ChangeView}"
              CommandParameter="Thumbnail"
              Label="Switch View"/>

However, in Metro, there's no x:Static, no TypeConverters, and IValueConverters only work on bindings, so I'm stumped.  How do you place buttons in applications that have very specific (that is, not derived from the view model) commands?

解决方案

I am afraid you cannot bind to static properties in Windows Store Apps. It is simply not supported.

You could create a proxy class that exposes the static command property and bind to this property of an instance of the proxy object:
http://stackoverflow.com/questions/14186175/bind-to-a-static-field-in-windows-8-xaml
http://stackoverflow.com/questions/4708711/how-can-i-use-the-xstatic-extension-for-phone7-silverlight-apps

You will of course have to create an instance of the proxy object. There is no way around this.

Please remember to mark helpful posts as answer to close your threads and then start a new thread if you have a new question.


这篇关于如何在XAML中引用自定义命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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