Unten ist Onload-Ereignis-Trigger von meiner XAML-Ansicht.Interaction.Behaviors, EventTriggerBehavior-Befehl wird nicht aufgerufen
<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>
Befehlsimplementierung hier.
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);
}
}
Befehl wird nicht ausgeführt. Danke für die Hilfe im Voraus.
Ich habe versucht, das Viewmodel wie this.View Modell der Onload Befehl hat, die die asychronous Methode ExecuteLoadedCommandAsync nennt das Ergebnis, es zu
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();
}
}
Noch hat nicht funktioniert.
Sie halten das ViewModel innerhalb des Befehls? nicht der Befehl im ViewModel? – Mishka
Ich bin neu in UWP/WPF. Könnten Sie das bitte korrigieren? Ich habe diesen öffentlichen DelegateCommand LoadedCommand {get; } public AppointmentViewModel() { LoadedCommand = neuer DelegateCommand (async (param) => erwarten ExecuteLoadedCommandAsync()); } öffentliche async Task ExecuteLoadedCommandAsync() { var Antwort = warten AppointmentService.GetAppointments(); AppointmentRetrivalTime = JsonConvert.DeserializeObject (Antwort) .retrievalTime; } –
user2082630
Sie können Ihre Frage bearbeiten und diesen Code als neues Teil eingeben. es ist so unlesbar ... – Mishka