2011-04-15 1 views
2

Je tente d'ajouter des éléments à une barre d'applications avec des comportements.Comment ajouter un comportement à ApplicationBarIconButton en C#?

En XAML ils ressemblent:

<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True" 
          IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton x:Name="Save" 
             IconUri="/resources/icons/appbar.check.rest.png" 
             Text="Save" /> 
     <shell:ApplicationBarIconButton x:Name="Cancel" 
             IconUri="/resources/icons/appbar.cancel.rest.png" 
             Text="Cancel" /> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

<i:Interaction.Behaviors> 
    <Behaviors:ApplicationBarIconButtonCommand TextKey="Save" 
              CommandBinding="{Binding SaveEventSetupCommand}" /> 
    <Behaviors:ApplicationBarIconButtonCommand TextKey="Cancel" 
              CommandBinding="{Binding CancelEventSetupCommand}" /> 
</i:Interaction.Behaviors> 

Pour support multi-langue je dois ajouter quelque chose comme:

Text="{Binding Path=Localizedresources.lblCourse, Source={StaticResource LocalizedStrings}}" 

à chaque bouton. Il semblerait que cela ne puisse pas être fait dans xaml, d'où l'utilisation du code.

Le bouton est ajouté dans ce code:

ApplicationBarIconButton appBarSaveButton = new ApplicationBarIconButton(
      new Uri("/resources/icons/appbar.check.rest.png", UriKind.Relative)) 
      { Text = "Test" }; 

ApplicationBar.Buttons.Add(appBarSaveButton); 

Je ne peux pas comprendre comment ajouter le comportement. Ceci est mon point de départ:

WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand 
      ibc = new WP7Contrib.View.Controls.Behaviors.ApplicationBarIconButtonCommand 
{ TextKey = "Test" }; 

Fondamentalement, je suis à la recherche d'un échantillon de travail si quelqu'un peut obliger.

Merci

+0

Quel genre de comportement? Je suis afriad cela ne peut pas être fait, depuis ApplicationBar n'est pas le contrôle Silverlight. –

Répondre

1

Sur la base de la réponse de Matt ceci est ma solution:

Ajouter ce en haut de la page XAML:

xmlns:Preview="clr-namespace:Phone7.Fx.Preview;assembly=Phone7.Fx.Preview" 

et ceci à l'intérieur de la grille au fond:

<Preview:BindableApplicationBar x:Name="AppBar" BarOpacity="1.0" IsVisible="{Binding IsBarVisible}" > 
     <Preview:BindableApplicationBarIconButton 
      Command="{Binding SaveCommand}" 
      Text="{Binding Path=Localizedresources.lblSave, Source={StaticResource LocalizedStrings}}" 
      IconUri="/resources/icons/appbar.check.rest.png" /> 
     <Preview:BindableApplicationBarIconButton 
      Command="{Binding CancelCommand}" 
      Text="{Binding Path=Localizedresources.lblCancel, Source={StaticResource LocalizedStrings}}" 
      IconUri="/resources/icons/appbar.cancel.rest.png" /> 
    </Preview:BindableApplicationBar> 

Références:
http://blog.humann.info/post/2010/08/27/How-to-have-binding-on-the-ApplicationBar.aspx

http://www.codeproject.com/KB/windows-phone-7/CommandToAppBarWP7.aspx?display=Mobile

0

Vous ne pouvez pas spécifier le texte propriété d'un ApplicationBarIconButton à une ressource en XAML, que vous avez déjà travaillé sur. Pour créer un comportement et attaché dans le code que vous utilisez un code similaire à celui-ci (modifié à partir d'une application, je travaille en ce moment):

((ApplicationBarIconButton)this.ApplicationBar.Buttons[0].Text = Strings.NewContact; 
var newBehavior = new ApplicationBarButtonNavigation 
{ 
    ButtonText = Strings.NewContact, 
    NavigateTo = new Uri("/views/ContactView.xaml", UriKind.RelativeOrAbsolute), 
}; 
Interaction.GetBehaviors(this).Add(newBehavior);

Le principe est le même pour votre scénario: créer le comportement, puis utilisez Interaction.GetBehaviors(this).Add(yourBehavior);

REMARQUE: dans les exemples ci-dessus ce fait référence au code-behind pour la vue et n'est pas dans le modèle de vue.

Questions connexes