【问题标题】:C# access string value from class inside another classC#从另一个类中的类访问字符串值
【发布时间】:2020-02-20 12:23:13
【问题描述】:

我正在尝试从第三个Class 访问通过另一个Class 访问的Class 中的string

在这种情况下,SelectedCarListView 中的 SelectedItem & SelectedModelListView 中的 SelectedItem

汽车

public class Car : ViewModelBase
{
    private string _name;
    private ObservableCollection<CarModel> _models;

    public Car()
    {

    }
    public Car(string name, ObservableCollection<CarModel> models)
    {
        Name = name;
        Models = models;
    }

    public Car(string name)
    {
        Name = name;
    }

    public static Car Create(string name, ObservableCollection<CarModel> models)
        => new Car(name, models);

    public static Car Create(string name)
        => new Car(name);

    public string Name
    {
        get => _name;
        set => SetProperty(ref _name, value);
    }

    public ObservableCollection<CarModel> Models
    {
        get => _models;
        set => SetProperty(ref _models, value);
    }
}

汽车视图模型

  public class CarViewModel : ViewModelBase, ICarViewModel
{
    private string _enteredModel;
    private string _enteredCar;

    private Car _selectedCar;
    private CarModel _selectedModel;

    private ObservableCollection<CarModel> _carModels;
    private ObservableCollection<Car> _cars;

    [DesignOnly(true)]
    public CarViewModel()
    {
        Cars = new ObservableCollection<Car>
        {
            Car.Create("Audi",
                new ObservableCollection<CarModel>()
                {
                    CarModel.Create("A1"),
                    CarModel.Create("A2"),
                    CarModel.Create("A3"),
                    CarModel.Create("A4"),
                    CarModel.Create("A5")
                }),

            Car.Create("Mercedes",
                new ObservableCollection<CarModel>()
                {
                    CarModel.Create("A-Class"),
                    CarModel.Create("B-Class"),
                    CarModel.Create("C-Class"),
                    CarModel.Create("E-Class"),
                    CarModel.Create("S-Class")
                }),

            Car.Create("BMW",
                new ObservableCollection<CarModel>()
                {
                    CarModel.Create("1-Serie"),
                    CarModel.Create("2-Serie"),
                    CarModel.Create("3-Serie"),
                    CarModel.Create("4-Serie"),
                    CarModel.Create("5-Serie")
                }),

            Car.Create("Volkswagen",
                new ObservableCollection<CarModel>()
                {
                    CarModel.Create("Golf"),
                    CarModel.Create("Passat"),
                    CarModel.Create("Arteon"),
                    CarModel.Create("T-Cross"),
                    CarModel.Create("Up!")
                }),

            Car.Create("Volvo",
                new ObservableCollection<CarModel>()
                {
                    CarModel.Create("V60"),
                    CarModel.Create("V70"),
                    CarModel.Create("XC60"),
                    CarModel.Create("XC90"),
                    CarModel.Create("S90")
                }),
        };

        DeleteModelCommand = new DelegateCommand(DeleteModelExecuted, DeleteModelCanExecute);
        DeleteCarCommand = new DelegateCommand(DeleteCarExecuted, DeleteCarCanExecute);

        AddCarCommand = new DelegateCommand(AddCarExecuted, AddCarCanExecute);
        AddModelCommand = new DelegateCommand(AddModelExecuted, AddModelCanExecute);
    }

    public string EnteredModel
    {
        get => _enteredModel;
        set => SetProperty(ref _enteredModel, value);
    }

    public string EnteredCar
    {
        get => _enteredCar;
        set => SetProperty(ref _enteredCar, value);
    }

    public Car SelectedCar
    {
        get => _selectedCar;
        set => SetProperty(ref _selectedCar, value);
    }

    public CarModel SelectedModel
    {
        get => _selectedModel;
        set => SetProperty(ref _selectedModel, value);
    }

    public ObservableCollection<Car> Cars
    {
        get => _cars;
        set => SetProperty(ref _cars, value);
    }

    public ObservableCollection<CarModel> CarModels
    {
        get => _carModels;
        set => SetProperty(ref _carModels, value);
    }
}

CarModifierViewModel

 public class CarModifierViewModel : ViewModelBase, ICarModifierViewModel
{
    private CarColorModel _selectedColor;
    private CarModifierModel _selectedYear;

    private string _previouslySelectedCar;
    private string _previouslySelectedModel;

    private ObservableCollection<CarModifierModel> _modifierModels;

    private CarViewModel _carViewModelClass;

    [DesignOnly(true)]
    public CarModifierViewModel()
    {
        ModifierModels = new ObservableCollection<CarModifierModel>
        {
           CarModifierModel.Create(2000, new ObservableCollection<CarColorModel>()
           {
               CarColorModel.Create("Silver"),
               CarColorModel.Create("Black")
           }),
           CarModifierModel.Create(2005, new ObservableCollection<CarColorModel>()
           {
               CarColorModel.Create("Silver"),
               CarColorModel.Create("Black"),
               CarColorModel.Create("White")
           }),
           CarModifierModel.Create(2010, new ObservableCollection<CarColorModel>()
           {
               CarColorModel.Create("Silver"),
               CarColorModel.Create("Black"),
               CarColorModel.Create("White"),
               CarColorModel.Create("Blue")
           }),
           CarModifierModel.Create(2015, new ObservableCollection<CarColorModel>()
           {
               CarColorModel.Create("Silver"),
               CarColorModel.Create("Black"),
               CarColorModel.Create("White"),
               CarColorModel.Create("Blue"),
               CarColorModel.Create("Red")
           }),
           CarModifierModel.Create(2020, new ObservableCollection<CarColorModel>()
           {
               CarColorModel.Create("Silver"),
               CarColorModel.Create("Black"),
               CarColorModel.Create("White"),
               CarColorModel.Create("Blue"),
               CarColorModel.Create("Red"),
               CarColorModel.Create("Purple")
           })
        };
    }

    public CarColorModel SelectedColor
    {
        get => _selectedColor;
        set => SetProperty(ref _selectedColor, value);
    }

    public CarModifierModel SelectedYear
    {
        get => _selectedYear;
        set
        {
            SetProperty(ref _selectedYear, value);
            LoadCarAndModel();
        }
    }

    public string PreviouslySelectedCar
    {
        get => _previouslySelectedCar;
        set => SetProperty(ref _previouslySelectedCar, value);
    }

    public string PreviouslySelectedModel
    {
        get => _previouslySelectedModel;
        set => SetProperty(ref _previouslySelectedModel, value);
    }

    public ObservableCollection<CarModifierModel> ModifierModels
    {
        get => _modifierModels;
        set => SetProperty(ref _modifierModels, value);
    }

    public CarViewModel CarViewModelClass
    {
        get => _carViewModelClass;
        set => SetProperty(ref _carViewModelClass, value);
    }

    public void LoadCarAndModel()
    {
        CarViewModelClass = new CarViewModel();
        CarViewModelClass.SelectedCar = new Car();
        CarViewModelClass.SelectedCar.Name = new string(CarViewModelClass.SelectedCar.Name);

        PreviouslySelectedCar = CarViewModelClass?.SelectedCar?.Name;
        PreviouslySelectedModel = CarViewModelClass.SelectedModel.Name;

    }
}

我希望我没有错过任何重要的信息,但是如何从另一个视图访问 SelectedItem?在CarModifierViewModel 中,我尝试实例化要访问的类,但它没有加载 SelectedItem..

【问题讨论】:

  • 你必须使用合成。您的问题一目了然。所以给你一个具体的解决方案并不容易,例如,组合类的关系。请澄清哪个类正在修改哪个类?
  • 截图对我来说相当混乱。请简明扼要地描述问题是什么以及您要实现的目标以及包括任何支持代码。
  • @BionicCode 我会查找作文。 Car.cs 有一个 string Name ,这是在 CarViewModel.cs 中设置的。我想从 CarModifierViewModel.cs 访问它。
  • @ColinM 该应用程序的屏幕截图显示了我想要访问的selectedItem 绑定。我正在尝试在第二个视图中访问选定的汽车和选定的模型。其中有 CarModifierViewModel 作为数据上下文。第一个视图有数据上下文 CarViewModel
  • 我可以读到类似:PreviouslySelectedCar = CarViewModelClass?.SelectedCar?.Name; 这正是您要问的,不是吗?

标签: c# wpf mvvm


【解决方案1】:

解决方案是使用组合。您通常会创建一个MainViewModel,它具有将所有其他视图模型公开给视图的属性。 MainViewModel 使用构造函数将所需的类注入到相关的视图模型中。

MainViewModel.cs

class MainViewModel
{
  public CarViewModel CarViewModel { get; set; }
  public CarModifierViewModel CarModifierViewModel { get; set; }

  public MainViewModel()
  {
    this.CarViewModel = new CarViewModel();
    this.CarModifierViewModel = new CarModifierViewModel(this.CarViewModel);
  }
}

CarViewModel.cs

public class CarViewModel
{
  ...
}

CarModifierViewModel.cs

public class CarModifierViewModel
{
  private CarViewModel CarViewModel { get; set; }

  public CarModifierViewModel(CarViewModel carViewModel)
  {
    this.CarViewModel = carViewModel;
  }

  public void LoadCarAndModel()
  {
    // Now 'CarViewModel' won't be null, 
    // as long as you don't pass null to the constructor
    // or set the 'MainViewModel.CarViewModel' property to null
    PreviouslySelectedCar = this.CarViewModel.SelectedCar.Name;
    PreviouslySelectedModel = this.CarViewModel.SelectedModel.Name;
  }
}

App.xaml

<Application>
  <Application.Resources>
    <MainViewModel />
  </Application.Resources>
</Application>

View1.xaml

<UserControl DataContext="{StaticResource MainViewModel.CarViewModel}" />

View2.xaml

<UserControl DataContext="{StaticResource MainViewModel.CarModifierViewModel}" />

【讨论】:

  • 我试过这个。但显然说我没有提供足够的信息是正确的。我实际上并没有使用 MainWindow/MainViewModel。我在 MainWindow 中只有一个 TabControl。其他一切都通过 CarViewModelCarModifierViewModel 。然后我使用ViewModelLocator 为不同的视图设置DataContext
  • 是的,您没有提供太多有价值的信息。但在这种情况下,只要您能够提取知识并将其转移到您的项目中,这并不重要。重点不在于您分配这些视图模型的方式。这是关于结构化的。班级设计。为了共享实例数据,您必须将对实际实例的引用 传递给您的视图模型。组合是实现这一目标的一种解决方案。制作相关属性static 可能是另一种但不推荐的解决方案。
  • 您所要做的就是将CarViewModel 的共享实例传递给CarModifierViewModel。它必须与另一个视图正在处理的CarViewModel 相同。这样当视图引用CarModifierViewModel 时,它就有CarViewModel 的正确共享实例来检索正确的数据。如果还是不清楚,没问题。然后请发布您的定位器的缩短版本。我将对其进行修改以使其正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多