1

我使用Phone7.Fx R1綁定BindableApplicationBarIconButton IsEnabled屬性來Relaycommand CanExecute的Windows Phone 7.1

以下的作品。當用戶按下按鈕時,系統不會作出反應。這意味着,如果在沒有遊戲的情況下停止遊戲被按下,則沒有反應,反之亦然。

但是該按鈕看起來很活躍。我知道我可以將IsEnabled綁定到不同的屬性,但我希望它綁定到NewGameCanExecute和StopGameCanExecute。這可能嗎?

一些XAML代碼:

<Preview:BindableApplicationBarIconButton Command="{Binding NewGame}" IconUri="/images/icons/appbar.add.rest.png" Text="New game" /> 
     <Preview:BindableApplicationBarIconButton Command="{Binding StopGame}" IconUri="/images/icons/appbar.stop.rest.png" Text="Stop game" /> 

和繼電器命令:

public RelayCommand NewGame { get; private set; } 
public RelayCommand StopGame { get; private set; } 

//Constructor 
NewGame = new RelayCommand(NewGameExecute, NewGameCanExecute); 
StopGame = new RelayCommand(StopGameExecute, StopGameCanExecute); 

void NewGameExecute() 
{ 
    _gameStarted = true; 
    _gameControlModel.StartNewGame(); 
    StopGame.RaiseCanExecuteChanged(); 
} 

bool NewGameCanExecute() 
{ 
    return !_gameStarted; 
} 

void StopGameExecute() 
{  
    _gameControlModel.StopGame(); 
    _gameStarted = false; 
    NewGame.RaiseCanExecuteChanged(); 
} 

bool StopGameCanExecute() 
{ 
    return _gameStarted; 
} 

夫婦的問題和答案:

問:您是否嘗試過以設置一個斷點CanExecute函數來查看它是否被調用?

A:是的。它確實被調用,但圖標不會變灰,即使返回false也是如此。 如果CanExecute方法返回false,則不執行Execute方法。但是我想讓圖標像普通按鈕一樣變灰。

SOLUTION

我花了一些時間,一個解決方案,它可以在這裏找到想出了: http://pastebin.com/MM75xACj

+0

您是否爲頁面設置了DataContext? –

+0

是的。它工作正常,圖標只是不變灰,因爲使用CanExecute時未設置IsEnabled –

回答

0

SOLUTION

我花一些時間和解決方案想出了和編輯applicationbariconbutton類。

namespace Phone7.Fx.Controls 
{ 
    public class BindableApplicationBarIconButton : FrameworkElement, IApplicationBarIconButton 
    { 
     public int Index { get; set; } 

     public static DependencyProperty CommandProperty = DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(BindableApplicationBarIconButton), new PropertyMetadata(null, OnCommandPropertyChanged)); 
     private static void OnCommandPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      if (e.NewValue != e.OldValue) 
      { 
       ((BindableApplicationBarIconButton)d).Command = (ICommand)e.NewValue; 
      } 
     } 

     public ICommand Command 
     { 
      get { return (ICommand)GetValue(CommandProperty); } 
      set { 
        Command.CanExecuteChanged -= ValueOnCanExecuteChanged; 
       SetValue(CommandProperty, value); 
       Command.CanExecuteChanged += ValueOnCanExecuteChanged; 
      } 
     } 

     private void ValueOnCanExecuteChanged(object sender, EventArgs eventArgs) 
     { 
      ICommand commandSender = sender as ICommand; 
      if(commandSender != null) 
      {IsEnabled = commandSender.CanExecute(null);} 
     } 

     public static readonly DependencyProperty CommandParameterProperty = 
      DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(BindableApplicationBarIconButton), new PropertyMetadata(null, OnCommandParameterPropertyChanged)); 
     private static void OnCommandParameterPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      var invokeCommand = d as BindableApplicationBarIconButton; 
      if (invokeCommand != null) 
      { 
       invokeCommand.SetValue(CommandParameterProperty, e.NewValue); 
      } 
     } 
     public object CommandParameter 
     { 
      get { return GetValue(CommandParameterProperty); } 
      set 
      { 
       SetValue(CommandParameterProperty, value); 
      } 
     } 


     public static readonly DependencyProperty CommandParameterValueProperty = 
      DependencyProperty.RegisterAttached("CommandParameterValue", typeof(object), typeof(BindableApplicationBarIconButton), null); 
     public object CommandParameterValue 
     { 
      get 
      { 
       var returnValue = GetValue(CommandParameterValueProperty); 
       return returnValue; 
      } 
      set { SetValue(CommandParameterValueProperty, value); } 
     } 

     public static readonly DependencyProperty IsEnabledProperty = 
      DependencyProperty.RegisterAttached("IsEnabled", typeof(bool), typeof(BindableApplicationBarIconButton), new PropertyMetadata(true, OnEnabledChanged)); 

     private static void OnEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      if (e.NewValue != e.OldValue) 
      { 
       ((BindableApplicationBarIconButton)d).Button.IsEnabled = (bool)e.NewValue; 
      } 
     } 

     public static readonly DependencyProperty TextProperty = 
      DependencyProperty.RegisterAttached("Text", typeof(string), typeof(BindableApplicationBarIconButton), new PropertyMetadata(OnTextChanged)); 

     public new static readonly DependencyProperty VisibilityProperty = 
      DependencyProperty.RegisterAttached("Visibility", typeof(Visibility), typeof(BindableApplicationBarIconButton), new PropertyMetadata(OnVisibilityChanged)); 

     private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      if (e.NewValue != e.OldValue) 
      { 
       var button = ((BindableApplicationBarIconButton)d); 
       BindableApplicationBar bar = button.Parent as BindableApplicationBar; 

       bar.Invalidate(); 
      } 
     } 

     public new Visibility Visibility 
     { 
      get { return (Visibility)GetValue(VisibilityProperty); } 
      set { SetValue(VisibilityProperty, value); } 
     } 

     private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      if (e.NewValue != e.OldValue) 
      { 
       ((BindableApplicationBarIconButton)d).Button.Text = e.NewValue.ToString(); 
      } 
     } 

     public ApplicationBarIconButton Button { get; set; } 

     public BindableApplicationBarIconButton() 
     { 
      Button = new ApplicationBarIconButton(); 
      Button.Text = "Text"; 
      Button.Click += ApplicationBarIconButtonClick; 
     } 

     void ApplicationBarIconButtonClick(object sender, EventArgs e) 
     { 
      if (Command != null && CommandParameter != null) 
       Command.Execute(CommandParameter); 
      else if (Command != null) 
       Command.Execute(CommandParameterValue); 
      if (Click != null) 
       Click(this, e); 
     } 

     public bool IsEnabled 
     { 
      get { return (bool)GetValue(IsEnabledProperty); } 
      set { SetValue(IsEnabledProperty, value); } 
     } 

     public string Text 
     { 
      get { return (string)GetValue(TextProperty); } 
      set { SetValue(TextProperty, value); } 
     } 

     public event EventHandler Click; 

     public Uri IconUri 
     { 
      get { return Button.IconUri; } 
      set { Button.IconUri = value; } 
     } 
    } 
1

這顯然是任何BindableApplicationBarIconButton實現您正在使用的錯誤。

請向作者尋求幫助,或自己調試第三方軟件。

+1

謝謝。由於作者沒有回覆,我花了一些時間學習了DependencyProperties,並且使其工作。 –

+2

然後我會鼓勵你在這裏發佈解決方案,以便在同一問題上搜索的人可以看到你的答案。 –

+0

以後會做:) –