Windows phone 7 将mvvm light CustomEvent触发器与ApplicationBarIconButton一起使用

Windows phone 7 将mvvm light CustomEvent触发器与ApplicationBarIconButton一起使用,windows-phone-7,mvvm-light,Windows Phone 7,Mvvm Light,下面的按钮示例代码将触发打开第二页 <Button x:Name="btnSelect" Content="Select" HorizontalAlignment="Right" Margin="0,8,20,6" Grid.Row="2" Width="200"> <Custom:Interaction.Triggers> <Custom:EventT

下面的按钮示例代码将触发打开第二页

            <Button x:Name="btnSelect" Content="Select" HorizontalAlignment="Right" Margin="0,8,20,6" 
                Grid.Row="2" Width="200">
            <Custom:Interaction.Triggers>
                <Custom:EventTrigger EventName="Click">
                    <GalaSoft_MvvmLight_Command:EventToCommand x:Name="btnSelectClicked" 
                                                               Command="{Binding SelectEventPageCommand, Mode=OneWay}"/>
                </Custom:EventTrigger>
            </Custom:Interaction.Triggers>
        </Button> 

    public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
    }


    private void ContentGrid_Loaded(object sender, System.Windows.RoutedEventArgs e)
    {
        Messenger.Default.Register<GoToPageMessage>(this, (action) => ReceiveMessage(action));
    }

    private object ReceiveMessage(GoToPageMessage action)
    {
        StringBuilder sb = new StringBuilder("/Views/");
        sb.Append(action.PageName);
        sb.Append(".xaml");
        NavigationService.Navigate(
           new System.Uri(sb.ToString(),
                 System.UriKind.Relative));
        return null;
    }
}

公共部分类主页:PhoneApplicationPage
{
//建造师
公共主页()
{
初始化组件();
}
已加载私有void ContentGrid_(对象发送方,System.Windows.RoutedEventArgs e)
{
Messenger.Default.Register(this,(action)=>ReceiveMessage(action));
}
私有对象接收消息(GoToPageMessage操作)
{
StringBuilder sb=新的StringBuilder(“/Views/”);
sb.Append(action.PageName);
sb.追加(“.xaml”);
导航服务,导航(
新的System.Uri(sb.ToString(),
系统(类、相对);
返回null;
}
}

有人能建议我如何使用ApplicationBarIconButton做同样的事情吗?我得到一个错误属性“触发器”不能附加到ApplicationBarIconButton类型的元素

还是我应该使用代码隐藏

    <phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="False">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1">

        </shell:ApplicationBarIconButton>

    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>

Yeap ApplicationBar与Silverlight中的其他控件略有不同。通过使用以下命令,我可以使用ICommand:

或者Silverlight toolkit,它提供了一些扩展,如本回答中所述:
但我从未使用过它。

应用程序栏是由操作系统提供的服务,也就是说,它不是框架的一部分,并且不支持您已经发现的
触发器。
。如上所述,有许多解决方案可以解决此问题


或者,您可以使用来自的
ApplicationBarButtonCommand
ApplicationBarButtonNavigation
行为。如果需要,创建
ApplicationBarMenuCommand
是一项非常简单的任务。在您的情况下,如果使用
ApplicationBar
按钮导航到某个页面,那么
ApplicationBarButtonNavigation
行为就可以了。

谢谢Matthieu,第一个链接()正好满足了我的需要。