gpt4 book ai didi

mvvm - MVVM交叉行为和InvokeCommandAction

转载 作者:行者123 更新时间:2023-12-03 10:20:49 25 4
gpt4 key购买 nike

我对MVVMCross和MVVM体系结构比较陌生。

我试图保持我的CodeBehind尽可能整洁,因此在单击Item时一直使用Interactivity:Interaction.Behaviors触发命令:

<views:MvxStorePage.Resources>
<core:Theme x:Key="Theme"/>
<b:NameScopeBinding x:Key="ModuleGridView" Source="{Binding ElementName=ModuleGridView}" />
</views:MvxStorePage.Resources>
...
<GridView x:Name="ModuleGridView" >
...
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="SelectionChanged">
<Core:InvokeCommandAction Command="{Binding SelectModuleCommand}" CommandParameter="{Binding Source.SelectedItem, Source={StaticResource ModuleGridView}}" />
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
...
</GridView>

在我的ViewModel中:
MvxCommand<object> _selectModuleCommand;
public ICommand SelectModuleCommand
{
get
{
_selectModuleCommand = _selectModuleCommand ?? new MvxCommand<object>((obj) => SelectModule(obj));
return _selectModuleCommand;
}
}

private void SelectModule(object module)
{
var test = 1;
}

问题是传入 SelectModule的对象的类型为 ItemClickedEventArgs,这在我的ViewModel所在的PCL核心项目中不可用。所以我无法访问该对象的 ItemClicked属性。

我尝试从“InvokeCommandAction”中使用它
 <Core:InvokeCommandAction Command="{Binding SelectModuleCommand}" CommandParameter="{Binding Source.SelectedItem.ClickedItem, Source={StaticResource ModuleGridView}}" />

但这没有效果,我仍然将 ItemClickedEventArgs作为Command的参数

最佳答案

使用InvokeCommandAction的InputConverter属性解决

<interactivity:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="ItemClick">
<icore:InvokeCommandAction Command="{Binding SelectModuleCommand}" InputConverter="{StaticResource ItemClickedConverter}" />
</icore:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>

ItemClickedConverter:
public class ItemClickedConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var args = value as ItemClickEventArgs;

if (args != null)
return args.ClickedItem;

return null;
}

public object ConvertBack(object value, Type targetType, object parameter,
string language)
{
throw new NotImplementedException();
}
}

关于mvvm - MVVM交叉行为和InvokeCommandAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24032705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com