2013-10-05 60 views
1

不能綁定ApplicationBar,我想:綁定應用程序任務的Windows Phone

<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 
     <shell:ApplicationBarIconButton 
      x:Name="btnTest" 
      IconUri="/Assets/AppBar/appbar.add.rest.png" 
      Text="{Binding MyBtnText}" /> 
    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

private void MainPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    btnTest.Text = AppResources.Reset; 
} 

回答

2

應用程序任務欄是不是DependencyObject的,不支持綁定。即使您只想使用AppBar本地化的綁定(而不是與ViewModel同步的動作或自動啓用),並最終將整個ApplicationBar設置移動到代碼隱藏時,也應該考慮使用一個庫,以便在xaml中爲AppBar提供更大的靈活性。參見例如BindableApplicationBarCaliburnBindableAppBar。還有其他開源項目允許AppBar綁定。

樣品:

<bar:BindableApplicationBarButton 
    Text="{Binding IconButtonText}" 
    IconUri="{Binding IconUri, FallbackValue=/Icons/Dark/appbar.add.rest.png}" 
    IsEnabled="{Binding ButtonIsEnabled}" /> 
4
private void MainPage_Loaded(object sender, RoutedEventArgs e) 
{ 
    ApplicationBarIconButton btn= ApplicationBar.Buttons[0] as ApplicationBarIconButton; 
    if (btn!= null) 
    { 
     btn.Text = AppResources.Test; 
    } 

} 

來源:http://www.diaryofaninja.com/blog/2011/07/05/solved-why-donrsquot-applicationbar-bindings-work-ndash-windows-phone-7-sdk

或建立從代碼ApplicationBar背後C#

// Build a localized ApplicationBar 
private void BuildLocalizedApplicationBar() 
{ 
    // Set the page's ApplicationBar to a new instance of ApplicationBar. 
    ApplicationBar = new ApplicationBar(); 

    // Create a new button and set the text value to the localized string from AppResources. 
    ApplicationBarIconButton appBarButton = 
     new ApplicationBarIconButton(new 
     Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative)); 
     appBarButton.Text = AppResources.Reset; 
    ApplicationBar.Buttons.Add(appBarButton); 

    // Create a new menu item with the localized string from AppResources. 
    ApplicationBarMenuItem appBarMenuItem = 
     new ApplicationBarMenuItem(AppResources.AppBarMenuItemText); 
    ApplicationBar.MenuItems.Add(appBarMenuItem); 
} 

來源:

How to build a localized app for Windows Phone

Tips for Localizing Windows Phone 8 XAML Apps - Part1

相關問題