Wpf 未调用Interaction.Behaviors、EventTriggerBehavior命令
下面是我的XAML视图中的onload事件触发器Wpf 未调用Interaction.Behaviors、EventTriggerBehavior命令,wpf,xaml,uwp,Wpf,Xaml,Uwp,下面是我的XAML视图中的onload事件触发器 <interactivity:Interaction.Behaviors> <core:EventTriggerBehavior EventName="Loaded"> <core:EventTriggerBehavior.Actions> <core:InvokeCommandAction Command="{Binding LoadedCommand}"
<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);
}
}
公共类DelegateCommand:ICommand
{
私有只读谓词_canExecute;
私有只读操作\u执行;
公共事件处理程序CanExecuteChanged;
公共DelegateCommand(操作执行)
:此(执行,空)
{
}
公共DelegateCommand(操作执行,
谓词(可执行)
{
_执行=执行;
_canExecute=canExecute;
}
public void raisecancecutechanged()
{
如果(CanExecuteChanged!=null)
{
CanExecuteChanged(此为EventArgs.Empty);
}
}
公共布尔CanExecute(对象参数)
{
如果(_canExecute==null)
{
返回true;
}
返回_canExecute(参数);
}
public void Execute(对象参数)
{
_执行(参数);
}
}
命令未被执行。提前谢谢你的帮助
我尝试过像这样的viewmodel。View model有Onload命令,它调用异步方法executeLoadCommandAsync来给出结果
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:ViewModelbase
{
公共列表约会{get;set;}
公共DelegateCommand LoadedCommand{get;}
公共任命视图模型()
{
LoadedCommand=new DelegateCommand(异步(参数)=>Wait ExecuteLoadedCommand());
}
公共异步任务ExecuteLoadedCommandAsync()
{
约会=等待约会服务。GetAppoints();
}
}
但它仍然不起作用。我测试了上面的代码片段。实际上,
executeLoadCommandAsync()
do执行。InvokeCommandAction
的命令没有问题。在executeLoadCommandAsync()
方法处添加断点以测试此问题
但是,约会
记录无法加载到视图中。这是因为当视图模型新实例化时,约会
的值为空,并且该值是在执行加载
事件之后设置的。因此,对于动态更改的属性,您需要为约会
属性实现以下功能:
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
}
更多详细信息请参考。您是否将ViewModel保存在命令中?不是ViewModel中的命令吗?我是UWP/WPF新手。你能纠正一下吗?我尝试了此公共DelegateCommand LoadedCommand{get;}公共任命ViewModel(){LoadedCommand=new DelegateCommand(异步(参数)=>await ExecuteLoadedCommand();}公共异步任务ExecuteLoadedCommand()){var response=await AppointmentService.getAppoints();AppointmentRetrivalTime=JsonConvert.DeserializeObject(response.retrievalTime;}你可以编辑你的问题并在那里输入新的部分代码。这样就不可读了…我对问题做了更改我想我现在明白了…?命令不需要知道ViewModel。你在VM中所做的一切都很好,但是命令不需要与VM有任何连接。你在那里也做得很好,提供了一个属性因此,另一个问题是您的VM是否实际加载并作为该视图的DataContext?
public MainPage()
{
this.InitializeComponent();
AppointmentViewModel vm = new AppointmentViewModel();
this.DataContext = vm;
}