以下是来自我的XAML视图的onload事件触发器。
<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);
}
}
命令未执行。感谢您的帮助。
我尝试了像这样的viewmodel.View模型有Onload Command,它调用异步方法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 :(得分:0)
我测试了上面的代码片段。实际上class A{
public:
void foo(int a, int b);
protected:
virtual void foo(int a) = 0;
};
class B : public A{
public:
using A::foo;
protected:
void foo(int a);
};
void f()
{
B b;
b.foo(1,2); // OK
b.foo(3); // error: ‘virtual void B::foo(int)’ is protected within this context
}
已经执行了。 ExecuteLoadedCommandAsync()
的命令没有错。在InvokeCommandAction
方法中添加断点以测试此问题。
但是ExecuteLoadedCommandAsync()
记录无法加载到视图中。这是因为当视图模型new实例化时,Appointments
的值为null,并且在执行Appointments
事件之后设置该值。因此,对于动态更改的属性,您需要为loaded
属性实现INotifyPropertyChanged
,如下所示:
Appointments
XAML视图的代码隐藏:
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
}
更多详情请参阅Data binding in depth。