簡體   English   中英

C# 從另一個類中的類訪問字符串值

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

我正在嘗試從第三個Class訪問通過另一個Class訪問的Classstring

在這種情況下, SelectedCar處於所述的SelectedItem ListView &並且SelectedModel處於所述的SelectedItem ListView

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

汽車修改器視圖模型

 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 ..

解決方案是使用組合。 您通常會創建一個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;
  }
}

應用程序.xaml

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

查看1.xaml

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

視圖2.xaml

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM