【发布时间】:2021-06-20 18:30:02
【问题描述】:
我正在尝试学习 WPF,并且有一个案例我必须将命令行参数传递给视图模型。我能够从 app.xaml 获取参数并传递给 MainWindow,但是我如何从 mainwindow 获取这个参数到 viewmodel。此外,如果我向视图模型添加构造函数,则无法在 xaml 视图的数据内容中声明它 - 如果我在代码隐藏中执行此操作,我将无法获得智能感知,这是另一个问题。
如果你能指出一个简单的例子,或者如果需要做这种事情的模式,那就太好了。
查看模型
public class RepoViewModel
{
private VcsRepo repo;
public RepoViewModel()
{
// this is where i need that parameter from mainwindow
this.repo = new VcsRepo(new Repository([my parameter]));
}
public VcsRepo Repo
{
get { return repo; }
set { repo = value; }
}
}
景色
<UserControl.DataContext>
<local:RepoViewModel/>
</UserControl.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding Repo.Files}"/>
</Grid>
</UserControl>
【问题讨论】: