这是我的观点的代码隐藏:
using System.Windows.Controls;
namespace TestBoundTabControl.Views
{
public partial class SmartFormView : UserControl
{
public SmartFormView()
{
InitializeComponent();
}
public void BeforeLoad()
{
MainTabControl.SelectedIndex = MainTabControl.Items.Count - 1;
}
}
}
但是为什么我不能从视图的演示者那里访问方法“BeforeLoad()”?
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
}
}
}
答案 0 :(得分:1)
我的猜测是,View
属性的类型为UserControl
,而不是SmartFormView
。如果这是真的,你将不得不施放它(或改变它的类型):
((SmartFormView) View).BeforeLoad();
答案 1 :(得分:0)
View显然属于某种基类型,例如FrameworkElement。试试这段代码:
SmartFormView myView = new SmartFormView();
View = myView;
myView.BeforeLoad();
答案 2 :(得分:0)
您不显示PresenterBase类,但PresenterBase.View属性可能不是SmartFormView类型。我不确定它是什么类型,但我猜测UserControl或它的一个祖先。
以下是一些选项:
使您的基类在视图类型上具有通用性,以便在SmartFormPresenter中,View属性 可以是SmartFormView类型。 E.g:
public class PresenterBase<T> {
...
public T View { get; set; }
...