首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用RelayCommand

如何使用RelayCommand
EN

Stack Overflow用户
提问于 2014-01-16 08:39:57
回答 2查看 317关注 0票数 0

我的应用程序与模型视图ViewModel一起工作。在我的模型中,我有一个基于客户机类的列表。

代码语言:javascript
复制
public class Client
{
    public string Name { get; set; }

    public string Ip { get; set; }

    public string Mac { get; set; }
}

在我的ClientRepository中,我用我的客户机类从XML文件中生成一个列表。

代码语言:javascript
复制
    public ClientRepository()
    {
        var xml = "Clients.xml";
        if (File.Exists(xml))
        {
            _clients = new List<Client>();
            XDocument document = XDocument.Load(xml);
            foreach (XElement client in document.Root.Nodes())
            {
                string Name = client.Attribute("Name").Value;
                string Ip = client.Element("IP").Value;
                string Mac = client.Element("MAC").Value;
                _clients.Add(new Client() { Mac = Mac, Name = Name, Ip = Ip });
            }
        }
    }

在我的UI/UX中,我有3个用于MAC的Textboxes 1、1个IP和1个名称,还有一个按钮,它具有绑定到AddClientCommand的功能。

代码语言:javascript
复制
<Label Grid.Row="0" Grid.Column="0" Content="Host Name:"/>
<TextBox Grid.Row="0" Grid.Column="1" x:Name="tbHostName" Height="20" Text="{Binding Path=newClient.Name, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="1" Grid.Column="0" Content="IP Address:"/>
<TextBox Grid.Row="1" Grid.Column="1" x:Name="tbIP" Height="20" Text="{Binding Path=newClient.Ip, UpdateSourceTrigger=PropertyChanged}"/>
<Label Grid.Row="2" Grid.Column="0" Content="MAC Address"/>
<TextBox Grid.Row="2" Grid.Column="1" x:Name="tbMAC" Height="20" Text="{Binding Path=newClient.Mac, UpdateSourceTrigger=PropertyChanged}"/>
<Button Grid.Row="3" Grid.Column="0" Content="Remove" x:Name="bRemove" Margin="3 0 3 0" Click="bRemove_Click"/>
<Button Grid.Row="3" Grid.Column="1" Content="Add" x:Name="bAdd" Margin="3 0 3 0" Click="bAdd_Click" Command="{Binding AddClientCommand}"/>

为了说明我的观点:,我想知道的是实现AddClientCommand?的最佳方法是什么?

我现在拥有的--我知道它不起作用--

代码语言:javascript
复制
    public ClientViewModel()
    {
        _repository = new ClientRepository();
        _clients = _repository.GetClients();

        WireCommands();
    }

    private void WireCommands()
    {
        AddClientCommand = new RelayCommand(AddClient);
    }

    public Client newClient
    {
        get
        {
            return _newClient;
        }
        set
        {
            _newClient = value;
            OnPropertyChanged("newClient");
            AddClientCommand.isEnabled = true;
        }
    }

    public void AddClient()
    {
        _repository.AddClient(newClient);
    }

RelayCommand类:

代码语言:javascript
复制
public class RelayCommand : ICommand
{
    private readonly Action _handler;
    private bool _isEnabled;

    public RelayCommand(Action handler)
    {
        _handler = handler;
    }

    public bool isEnabled
    {
        get { return true; }
        set
        {
            if (value != isEnabled)
            {
                _isEnabled = value;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
    }

    public bool CanExecute(object parameter)
    {
        return isEnabled;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        _handler();
    }
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-16 09:17:22

我建议您使用DelegateCommands,您可以在许多MVVM框架中找到这个类:

代码语言:javascript
复制
public ICommand AddClientCommand 
{
    get
    {
        return new DelegateCommand(AddClient, CanExecuteAddClient);
    }
}

我还看到_clientsList<Client>类型的。如果要将其绑定到UI以查看客户端列表,则除非使用ObservableCollection<Client>,否则将不会通知更改。

编辑:正如有人在评论中指出的那样,您应该创建_newClient。请注意为每个添加的客户端创建一个新实例,否则您将最终一次又一次地添加相同的Client实例!

票数 1
EN

Stack Overflow用户

发布于 2014-01-16 09:08:19

你刚刚试过把你的命令放到这样的财产里吗?

代码语言:javascript
复制
public ICommand AddClientCommand
{
    get { return new RelayCommand(AddClient, CanAddClient); }
}

public bool CanAddClient()
{
    return newClient != null;
}

CanAddClient中放置任何您想要的逻辑,以启用或禁用ICommand

啊啊..。我明白了。RelayCommand的实现是错误的。您需要使用CanExecuteChanged事件处理程序的.您可以在RelayCommand.cs页面中找到GitHub上的正确实现。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21156934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档