2011-01-27 39 views
0

下面的按鈕示例代碼將觸發打開第二頁。使用mvvm-light CustomEvent觸發器與ApplicationBarIconButton

  <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; 
    } 
} 

http://galasoft.ch/mvvm/getstarted/

任何人都可以建議我如何使用ApplicationBarIconButton做?我得到一個錯誤屬性「觸發器」不可附加到ApplicationBarIconButton類型的元素。

或者我應該只使用CodeBehind?

<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> 

回答

2

Yeap ApplicationBar與Silverlight中的其他控件稍有不同。我可以用這一個使用一個ICommand: http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx

或Silverlight工具包它提供了一些擴展爲這個答案說: How to add an application bar to a user control in wp7但我從來沒有使用過。

+0

感謝Matthieu,第一個鏈接(http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx)讓我知道我需要什麼。 – Declan 2011-01-28 17:57:44

0

ApplicationBar是由操作系統提供的服務,即不屬於框架的一部分,也不支持Triggers,因爲您已經發現該服務。如上所述,有許多解決方案可爲此提供解決方法問題。

或者,您可以使用Silverlight Windows Phone Toolkit中的ApplicationBarButtonCommandApplicationBarButtonNavigation行爲。如果你需要的話,創建你的ApplicationBarMenuCommand是一項簡單的任務。在你的情況下,爲了使用ApplicationBar按鈕導航到一個頁面,那麼ApplicationBarButtonNavigation行爲就可以解決問題。