2017-07-11 42 views
0

下面是我的XAML視圖中的onload事件觸發器。Interaction.Behaviors,EventTriggerBehavior命令未被調用

<interactivity:Interaction.Behaviors> 
    <core:EventTriggerBehavior EventName="Loaded"> 
     <core:EventTriggerBehavior.Actions> 
      <core:InvokeCommandAction Command="{Binding LoadedCommand}" /> 
     </core:EventTriggerBehavior.Actions> 
    </core:EventTriggerBehavior> 
</interactivity:Interaction.Behaviors> 
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="100" /> 
    </Grid.RowDefinitions> 
    <GridView ItemsSource="{Binding Appointments}" IsItemClickEnabled="True"> 
     <GridView.ItemTemplate> 
      <DataTemplate x:DataType="vm:Appointment"> 
       <StackPanel Orientation="Vertical" HorizontalAlignment="Center"> 
        <StackPanel Margin="20,20,0,0"> 
         <TextBlock FontSize="18" Text="{x:Bind Name}" VerticalAlignment="Bottom"></TextBlock> 
         <TextBlock FontSize="10" Text="{x:Bind Category }" VerticalAlignment="Bottom"></TextBlock> 
        </StackPanel> 
       </StackPanel> 
      </DataTemplate> 
     </GridView.ItemTemplate> 
    </GridView> 
    <TextBlock Grid.Row="1" Name="ResultTextBlock" FontSize="24" Foreground="Red" FontWeight="Bold" /> 
</Grid> 

此處執行命令。

public class DelegateCommand : ICommand 
{ 
    private readonly Predicate<object> _canExecute; 
    private readonly Action<object> _execute; 

    public event EventHandler CanExecuteChanged; 

    public DelegateCommand(Action<object> execute) 
       : this(execute, null) 
    { 
    } 

    public DelegateCommand(Action<object> execute, 
       Predicate<object> canExecute) 
    { 
     _execute = execute; 
     _canExecute = canExecute; 
    } 


    public void RaiseCanExecuteChanged() 
    { 
     if (CanExecuteChanged != null) 
     { 
      CanExecuteChanged(this, EventArgs.Empty); 
     } 
    } 

    public bool CanExecute(object parameter) 
    { 
     if (_canExecute == null) 
     { 
      return true; 
     } 

     return _canExecute(parameter); 
    } 

    public void Execute(object parameter) 
    { 
     _execute(parameter); 
    } 

} 

命令沒有得到執行。我在這裏先向您的幫助表示感謝。

我試圖像this.View模型視圖模型已經在onload命令,調用臺異步方法ExecuteLoadedCommandAsync給結果

public class AppointmentViewModel : ViewModelbase 
{  
    public List<Appointment> Appointments { get; set; } 

    public DelegateCommand LoadedCommand { get; } 

    public AppointmentViewModel() 
    { 
     LoadedCommand = new DelegateCommand(async (param) => await ExecuteLoadedCommandAsync()); 
    } 

    public async Task ExecuteLoadedCommandAsync() 
    { 
     Appointments = await AppointmentService.GetAppointments(); 
    }  
}  

仍然沒有奏效。

+0

你在命令裏面持有ViewModel嗎?不是ViewModel中的命令? – Mishka

+0

我是UWP/WPF的新手。你能糾正這個嗎?我試過這個公共的DelegateCommand LoadedCommand {get; } public AppointmentViewModel() {LoadedCommand = new DelegateCommand(async(param)=> await ExecuteLoadedCommandAsync()); } public async Task ExecuteLoadedCommandAsync() var response = await AppointmentService.GetAppointments(); AppointmentRetrivalTime = JsonConvert.DeserializeObject (response).retrievalTime; } – user2082630

+0

您可以編輯您的問題,並在此處輸入此代碼作爲新零件。其無法讀取這種方式... – Mishka

回答

0

我測試了上面的代碼片段。其實ExecuteLoadedCommandAsync()確實執行。 InvokeCommandAction的命令沒有任何問題。在ExecuteLoadedCommandAsync()方法中添加一個斷點來測試這個。

Appointments記錄無法加載到視圖。這是因爲當視圖模型新實例化時,Appointments的值爲空,並且在執行loaded事件後設置值。因此,對於動態改變屬性,你需要實現I​Notify​Property​ChangedAppointments屬性,如下所示:

public class AppointmentViewModel : INotifyPropertyChanged 
{ 
    //public List<Appointment> Appointments { get; set; } 
    private List<Appointment> appointments; 
    public List<Appointment> Appointments 
    { 
     get 
     { 
      return appointments; 
     } 
     set 
     { 
      if (appointments != value) 
      { 
       appointments = value; 
       OnPropertyChanged("Appointments"); 
      } 
     } 
    } 

    public DelegateCommand LoadedCommand { get; } 

    public AppointmentViewModel() 
    {   
     LoadedCommand = new DelegateCommand((param) => ExecuteLoadedCommand()); 
    } 

    public void ExecuteLoadedCommand() 
    { 
     Appointments = GetAppointments(); 
    } 

    public List<Appointment> GetAppointments() 
    { 
     List<Appointment> allappointments = new List<Appointment>(); 
     allappointments.Add(new Appointment() { Name = "name1", Category = "category1" }); 
     allappointments.Add(new Appointment() { Name = "name2", Category = "category4" }); 
     allappointments.Add(new Appointment() { Name = "name3", Category = "category3" }); 
     return allappointments; 
    } 
    #region INotifyPropertyChanged 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    #endregion 
} 

爲XAML查看後臺代碼:

public MainPage() 
{ 
    this.InitializeComponent(); 
    AppointmentViewModel vm = new AppointmentViewModel(); 
    this.DataContext = vm; 
} 

更多細節請參考Data binding in depth

+0

它的作品!萬分感謝!!!! – user2082630