Perché non posso chiamare un metodo nel code-behind della mia vista dal presentatore della vista?

StackOverflow https://stackoverflow.com/questions/1227226

  •  22-07-2019
  •  | 
  •  

Domanda

Questo è il code-behind della mia vista:

using System.Windows.Controls;

namespace TestBoundTabControl.Views
{
    public partial class SmartFormView : UserControl
    {
        public SmartFormView()
        {
            InitializeComponent();
        }

        public void BeforeLoad()
        {
            MainTabControl.SelectedIndex = MainTabControl.Items.Count - 1;
        }
    }
}

Ma perché non riesco ad accedere al metodo " BeforeLoad () " dal presentatore della vista?

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using TestBoundTabControl.Views;

namespace TestBoundTabControl.Presenters
{
    public class SmartFormPresenter : PresenterBase
    {
        #region ViewModelProperty: SmartFormAreaPresenters
        private ObservableCollection<SmartFormAreaPresenter> _smartFormAreaPresenters = new ObservableCollection<SmartFormAreaPresenter>();
        public ObservableCollection<SmartFormAreaPresenter> SmartFormAreaPresenters
        {
            get
            {
                return _smartFormAreaPresenters;
            }

            set
            {
                _smartFormAreaPresenters = value;
                OnPropertyChanged("SmartFormAreaPresenters");
            }
        }
        #endregion

          public SmartFormPresenter()
        {
            View = new SmartFormView();
            View.DataContext = this;


            for (int i = 0; i < 5; i++)
            {
                SmartFormAreaPresenters.Add(new SmartFormAreaPresenter());
            }

            View.BeforeLoad(); //method not found

        }
    }
}
È stato utile?

Soluzione

La mia ipotesi è che la proprietà View abbia il tipo UserControl e non SmartFormView . Se questo è vero, dovrai lanciarlo (o modificarne il tipo):

((SmartFormView) View).BeforeLoad();

Altri suggerimenti

La vista è ovviamente di qualche tipo base, come FrameworkElement. Prova questo codice:

SmartFormView myView = new SmartFormView();

View = myView;

myView.BeforeLoad();

Non mostri la tua classe PresenterBase, ma probabilmente la proprietà PresenterBase.View non è di tipo SmartFormView. Non sono sicuro di che tipo sia, ma sto indovinando UserControl o uno dei suoi antenati.

Ecco alcune opzioni:

  1. Crea una classe base per tutte le tue viste, metti un metodo virtuale BeforeLoad su quella classe base e rendi la tua proprietà View di PresenterBase di quel tipo.
  2. Inserisci un typecast, come ha suggerito Martin (si tratta più di un hack che di una soluzione, IMHO).
  3. Rendi generica la tua classe di base sul tipo di vista, in modo che in SmartFormPresenter, la proprietà View possa essere del tipo SmartFormView. Per esempio:.

    public class PresenterBase<T> {
        ...
        public T View { get; set; }
        ...
    
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top