下面是我的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();
}
}
仍然沒有奏效。
你在命令裏面持有ViewModel嗎?不是ViewModel中的命令? – Mishka
我是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
您可以編輯您的問題,並在此處輸入此代碼作爲新零件。其無法讀取這種方式... – Mishka