【发布时间】:2018-05-10 03:34:57
【问题描述】:
我正在努力适应 MVVM 和 WPF 一个月。我正在尝试做一些基本的事情,但我经常遇到问题。我觉得我通过在线搜索解决了大部分问题。但是现在命令出现了问题。
-
问:我看到他们正在使用 RelayCommand、DelegateCommand 或 SimpleCommand。像这样:
public ICommand DeleteCommand => new SimpleCommand(DeleteProject);
尽管我像他们一样创建了所有东西,但我仍然将=> new SimpleCommand(DeleteProject); 部分带红色下划线。
到目前为止,我正在通过为每个命令创建命令类来解决它,但这感觉不是正确的方法。
- 问:我还将发布整个项目,我想知道我是否做错了什么或者我应该改进什么。
xaml:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="380" Width="250">
<StackPanel DataContext="{Binding Source={StaticResource gallery}}" Margin="10">
<ListView DataContext="{Binding Source={StaticResource viewModel}}"
SelectedItem="{Binding SelectedGallery}"
ItemsSource="{Binding GalleryList}"
Height="150">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Header="Path" Width="100" DisplayMemberBinding="{Binding Path}"/>
</GridView>
</ListView.View>
</ListView>
<TextBlock Text="Name" Margin="0, 10, 0, 5"/>
<TextBox Text="{Binding Name}" />
<TextBlock Text="Path" Margin="0, 10, 0, 5" />
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="40"/>
</Grid.ColumnDefinitions>
<TextBox Text="{Binding Path}" Grid.Column="0"/>
<Button Command="{Binding Path=ShowFolderClick, Source={StaticResource viewModel}}"
CommandParameter="{Binding}"
Content="..." Grid.Column="1" Margin="10, 0, 0, 0"/>
</Grid>
<Grid Margin="0, 10, 0, 0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Command="{Binding Path=AddClick, Source={StaticResource viewModel}}"
CommandParameter="{Binding}" Content="Add" Grid.Column="0" Margin="15,0,0,0" />
<Button Command="{Binding Path=DeleteClick, Source={StaticResource viewModel}}"
Content="Delete" Grid.Column="2" Margin="0,0,15,0" />
</Grid>
</StackPanel>
型号:
class Gallery : INotifyPropertyChanged
{
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
OnPropertyChanged("Name");
}
}
private string _path;
public string Path
{
get
{
return _path;
}
set
{
_path = value;
OnPropertyChanged("Path");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(params string[] propertyNames)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
foreach (string propertyName in propertyNames) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
handler(this, new PropertyChangedEventArgs("HasError"));
}
}
}
模型视图:
class GalleryViewModel : INotifyPropertyChanged
{
public GalleryViewModel()
{
GalleryList = new ObservableCollection<Gallery>();
this.ShowFolderClick = new ShowFolderDialog(this);
this.AddClick = new AddGalleryCommand(this);
this.DeleteClick = new DeleteGalleryCommand(this);
}
private ObservableCollection<Gallery> _galleryList;
public ObservableCollection<Gallery> GalleryList
{
get { return _galleryList; }
set {
_galleryList = value;
OnPropertyChanged("GalleryList");
}
}
private Gallery _selectedGallery;
public Gallery SelectedGallery
{
get { return _selectedGallery; }
set {
_selectedGallery = value;
OnPropertyChanged("SelectedGallery");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(params string[] propertyNames)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
foreach (string propertyName in propertyNames) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
handler(this, new PropertyChangedEventArgs("HasError"));
}
}
public AddGalleryCommand AddClick { get; set; }
public void AddGalleryClick(Gallery gallery)
{
Gallery g = new Gallery();
g.Name = gallery.Name;
g.Path = gallery.Path;
GalleryList.Add(g);
}
public DeleteGalleryCommand DeleteClick { get; set; }
public void DeleteGalleryClick()
{
if (SelectedGallery != null)
{
GalleryList.Remove(SelectedGallery);
}
}
public ShowFolderDialog ShowFolderClick { get; set; }
public void ShowFolderDialogClick(Gallery gallery)
{
System.Windows.Forms.FolderBrowserDialog browser = new System.Windows.Forms.FolderBrowserDialog();
string tempPath = "";
if (browser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
tempPath = browser.SelectedPath; // prints path
}
gallery.Path = tempPath;
}
}
命令:
class AddGalleryCommand : ICommand
{
public GalleryViewModel _viewModel { get; set; }
public AddGalleryCommand(GalleryViewModel ViewModel)
{
this._viewModel = ViewModel;
}
public bool CanExecute(object parameter)
{
/*if (parameter == null)
return false;*/
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
this._viewModel.AddGalleryClick(parameter as Gallery);
}
}
class DeleteGalleryCommand : ICommand
{
public GalleryViewModel _viewModel { get; set; }
public DeleteGalleryCommand(GalleryViewModel ViewModel)
{
this._viewModel = ViewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
this._viewModel.DeleteGalleryClick();
}
}
class ShowFolderDialog : ICommand
{
public GalleryViewModel _viewModel { get; set; }
public ShowFolderDialog(GalleryViewModel ViewModel)
{
this._viewModel = ViewModel;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
this._viewModel.ShowFolderDialogClick(parameter as Gallery);
}
}
感谢您到目前为止阅读的时间,我将不胜感激我将获得的每一个建议。
【问题讨论】:
-
如果您的语法特别失败:
public ICommand DeleteCommand => new SimpleCommand(DeleteProject);,请尝试public ICommand DeleteCommand {get {return new SimpleCommand(DeleteProject);}}如果可行,则说明您没有运行 c#6 -
您的问题到底是什么?你的代码没有编译?命令绑定不起作用?
-
不清楚你在问什么。你实际上得到了什么错误信息?说程序语句是“红色下划线”是完全没用的;它什么也没告诉我们。您是否只是未能为
SimpleCommand提供实现?您在上面发布的代码中没有任何内容。有无数可重用ICommand实现的示例,但它们并没有神奇地出现。您必须将它们包含在您的项目中,或者通过引用包含它们的库,或者自己编写它们(不难) -
抱歉,不清楚。我没有发布带有此错误的代码,我有另一个项目。我有错误“;预期”。 zaitsman 的回答是正确的。所以这是问题,因为我使用的是 Visual Studio 2013?
-
代码审查访问Code Review Stack Exchange
;)