2

想象一下,我有一个 UserControl,它显示一个停车场(我最喜欢的类比),里面有不同颜色的汽车。您可以选择一辆车,并在单独的 UserControl(在单独的项目中)显示所选汽车的统计信息。

现在用户想要在汽车统计信息 UC 上的一个按钮,“下一辆相同颜色的汽车”。选中后,它应该以相同的颜色显示停车场上下一辆车的统计信息(从上到下,从左到右)。

因此,如果这是有道理的,那么问题就来了。

我目前正在使用 MVVM Lite 将包含所选汽车的消息从停车场 UC 发送到汽车统计信息 UC。一切都很好。但是现在,有了这个新功能请求,我该怎么办?统计 UC 需要从停车场 UC 请求下一辆车。

这会是使用依赖注入的好地方吗?还是有其他更好的方法?

4

2 回答 2

1

如果我说对了,那么您想要的是具有适当 CommandParameters 的命令。

  public class Car
  {
    public Car(ParkingLot lot)
    {
        _parkingLot = lot;
    }

    public string Color { get; set; }

    public ParkingLot ParkingLot
    {
        get
        {
            return _parkingLot;
        }
    }

    private ParkingLot _parkingLot;
}

public class ParkingLot : ObservableCollection<Car>
{
    public Car SelectedCar { get; set; }

    public ICommand ShowNextCarCommand {
        get
        {
            if (_showNextCar == null)
            {
                _showNextCar = new DelegateCommand(OnShowNextCar);
            }

            return _showNextCar;
        }
    }

    private void OnShowNextCar()
    {
        string currentColor = SelectedCar.Color;
        //Write proper logic to get the next Car. Here you got the currently selected car with you and the color
        SelectedCar = this.NEXT(s => s.Color == currentColor); //Write the NEXT() logic           
    }

    ICommand _showNextCar;
}

现在只需设置 Button.Command="{Binding ParkingLot.ShowNextCarCommand}" 即可控制 ParkingLot 视图模型类并找到 Next 相同颜色的汽车并将其再次设置为 SelectedCar 属性。我假设您将在所有这些属性中都有 RaisepropertyChanged。我使用简单的 Prism 的 DelegateCommand

于 2010-02-11T01:51:56.863 回答
0

我会使用控制器作为两个 ViewModel(ParkingLotViewModel 和 StatisticsViewModel)之间的中介。在您的情况下,控制器负责同步所选汽车,并负责将“选择相同颜色的下一辆车”命令传递给 ParkingLotViewModel。

WPF 应用程序框架 (WAF)的示例应用程序展示了它是如何工作的。

.

jbe

于 2010-02-14T16:54:18.637 回答