简体   繁体   English

C# 从另一个类中的类访问字符串值

[英]C# access string value from class inside another class

I am trying to access a string that's in a Class accessed through another Class , from a third Class .我正在尝试从第三个Class访问通过另一个Class访问的Classstring

In this case the SelectedCar is the SelectedItem in a ListView & and the SelectedModel is the SelectedItem in a ListView在这种情况下, SelectedCar处于所述的SelectedItem ListView &并且SelectedModel处于所述的SelectedItem ListView

Car

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);
    }
}

CarViewModel汽车视图模型

  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;

    }
}

I hope I did not miss anything crucial, but how do I access the SelectedItem from another view?我希望我没有错过任何重要的东西,但是如何从另一个视图访问SelectedItem In CarModifierViewModel I tried instantiating the class to access but it does not load the SelectedItem ..CarModifierViewModel我尝试实例化要访问的类,但它没有加载SelectedItem ..

The solution is to use composition.解决方案是使用组合。 You usually create a MainViewModel which has properties to expose all other view models to the views.您通常会创建一个MainViewModel ,它具有将所有其他视图模型公开给视图的属性。 The MainViewModel injects the required classes into the related view models using a constructor. MainViewModel使用构造函数将所需的类注入到相关的视图模型中。

MainViewModel.cs主视图模型.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 CarViewModel.cs

public class CarViewModel
{
  ...
}

CarModifierViewModel.cs 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应用程序.xaml

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

View1.xaml查看1.xaml

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

View2.xaml视图2.xaml

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM