Ric*_*ard 1 mvvm portable-class-library windows-runtime mvvmcross
我对MVVMCross和MVVM架构比较陌生.
我试图保持我的CodeBehind尽可能干净,所以我一直Interactivity:Interaction.Behaviors用来点击一个Item时触发一个命令:
<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>
Run Code Online (Sandbox Code Playgroud)
在我的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;
}
Run Code Online (Sandbox Code Playgroud)
问题是传入的对象SelectModule是ItemClickedEventArgs我的ViewModel所在的PCL核心项目中不可用的类型.所以我无法访问该ItemClicked对象的属性.
我试过从'InvokeCommandAction'中使用它
<Core:InvokeCommandAction Command="{Binding SelectModuleCommand}" CommandParameter="{Binding Source.SelectedItem.ClickedItem, Source={StaticResource ModuleGridView}}" />
Run Code Online (Sandbox Code Playgroud)
但它没有效果,我仍然ItemClickedEventArgs作为我的命令的参数
使用InvokeCommandAction的InputConverter属性解决
<interactivity:Interaction.Behaviors>
<icore:EventTriggerBehavior EventName="ItemClick">
<icore:InvokeCommandAction Command="{Binding SelectModuleCommand}" InputConverter="{StaticResource ItemClickedConverter}" />
</icore:EventTriggerBehavior>
</interactivity:Interaction.Behaviors>
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1076 次 |
| 最近记录: |