C# 通过ICommand传递参数
如果我不从视图中传递任何内容,则以下操作有效 View.csC# 通过ICommand传递参数,c#,mvvm,mvvmcross,C#,Mvvm,Mvvmcross,如果我不从视图中传递任何内容,则以下操作有效 View.cs ViewModel.ReloadCommand.Execute(null); ViewModel.cs public ICommand ReloadCommand { get { return new MvxCommand(async () => { await RefreshStudentList(); }); } } public
ViewModel.ReloadCommand.Execute(null);
ViewModel.cs
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList();
});
}
}
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList(xxx);
});
}
}
MyViewModel vm;
.
.
protected override void OnCreate(Bundle bundle)
{
.... //other stuff
vm = ViewModel as MyViewModel;
}
但是,我需要传递一个参数,我想知道如何才能做到这一点
ViewModel.ReloadCommand.Execute(xxx);
ViewModel.cs
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList();
});
}
}
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList(xxx);
});
}
}
MyViewModel vm;
.
.
protected override void OnCreate(Bundle bundle)
{
.... //other stuff
vm = ViewModel as MyViewModel;
}
我不熟悉MvvmCross,但据我所知,它是这样的:
public ICommand ReloadCommand
{
get
{
return new MvxCommand<XXXType>(async (xxx) =>
{
await RefreshRoutesList(xxx);
});
}
}
public ICommand ReloadCommand
{
return new MvxAsyncCommand(DoAsyncStuff);
}
private Task DoAsyncStuff(MyType type)
{
}
public图标命令和重载命令
{
得到
{
返回新的MvxCommand(异步(xxx)=>
{
等待刷新路由列表(xxx);
});
}
}
我不熟悉MvvmCross,但据我所知,它是这样的:
public ICommand ReloadCommand
{
get
{
return new MvxCommand<XXXType>(async (xxx) =>
{
await RefreshRoutesList(xxx);
});
}
}
public ICommand ReloadCommand
{
return new MvxAsyncCommand(DoAsyncStuff);
}
private Task DoAsyncStuff(MyType type)
{
}
public图标命令和重载命令
{
得到
{
返回新的MvxCommand(异步(xxx)=>
{
等待刷新路由列表(xxx);
});
}
}
不要这样做,先在视图上初始化视图模型。然后,根据您的代码,ICommand
只执行RefreshRoutesList
功能,因此我将直接从视图访问RefreshRoutesList
。为了明确命名,我将使用MyView.cs
和MyViewModel.cs
MyView.cs
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList();
});
}
}
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList(xxx);
});
}
}
MyViewModel vm;
.
.
protected override void OnCreate(Bundle bundle)
{
.... //other stuff
vm = ViewModel as MyViewModel;
}
完成后,您可以使用vm变量在视图中的任何位置调用函数,即
await vm.RefreshRoutesList(aParameter);
希望这能有所帮助。与其这样做,不如先在视图上初始化视图模型。然后,根据您的代码,ICommand
只执行RefreshRoutesList
功能,因此我将直接从视图访问RefreshRoutesList
。为了明确命名,我将使用MyView.cs
和MyViewModel.cs
MyView.cs
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList();
});
}
}
public ICommand ReloadCommand
{
get
{
return new MvxCommand(async () =>
{
await RefreshStudentList(xxx);
});
}
}
MyViewModel vm;
.
.
protected override void OnCreate(Bundle bundle)
{
.... //other stuff
vm = ViewModel as MyViewModel;
}
完成后,您可以使用vm变量在视图中的任何位置调用函数,即
await vm.RefreshRoutesList(aParameter);
希望这能有所帮助。要执行异步操作,MvvmCross还有一个MvxAsyncCommand
,它还可以将一个参数作为常规的MvxCommand
它看起来像这样:
public ICommand ReloadCommand
{
get
{
return new MvxCommand<XXXType>(async (xxx) =>
{
await RefreshRoutesList(xxx);
});
}
}
public ICommand ReloadCommand
{
return new MvxAsyncCommand(DoAsyncStuff);
}
private Task DoAsyncStuff(MyType type)
{
}
可以使用以下参数执行任何命令:
ViewModel.ReloadCommand.Execute(myParameter);
要执行异步操作,MvvmCross还有一个MvxAsyncCommand
,它还可以将一个参数作为常规的MvxCommand
它看起来像这样:
public ICommand ReloadCommand
{
get
{
return new MvxCommand<XXXType>(async (xxx) =>
{
await RefreshRoutesList(xxx);
});
}
}
public ICommand ReloadCommand
{
return new MvxAsyncCommand(DoAsyncStuff);
}
private Task DoAsyncStuff(MyType type)
{
}
可以使用以下参数执行任何命令:
ViewModel.ReloadCommand.Execute(myParameter);
不是你想要的答案,但是
我对Mvvm的理解是,它反映视图的状态,并对来自视图的命令作出反应。您的参数可以被视为状态,因此它在绑定到的ViewModel上应该有自己的属性。因此,您的命令不必传递参数。这还将进一步将ViewModel与视图的实现分离。这不是您可能想要的答案,但是
我对Mvvm的理解是,它反映视图的状态,并对来自视图的命令作出反应。您的参数可以被视为状态,因此它在绑定到的ViewModel上应该有自己的属性。因此,您的命令不必传递参数。这还将进一步将ViewModel与视图的实现分离