[英]Why can't I call a method in my View's code-behind from the view's presenter?
This is the code-behind of my view: 这是我观点的代码背后 :
using System.Windows.Controls;
namespace TestBoundTabControl.Views
{
public partial class SmartFormView : UserControl
{
public SmartFormView()
{
InitializeComponent();
}
public void BeforeLoad()
{
MainTabControl.SelectedIndex = MainTabControl.Items.Count - 1;
}
}
}
But why can't I access the method "BeforeLoad()" from the view's presenter? 但是,为什么不能从视图的演示者访问方法“ 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
}
}
}
My guess is that the property View
has type UserControl
and not SmartFormView
. 我的猜测是,属性
View
类型为UserControl
而不是SmartFormView
。 If that is true you will have to cast it (or change it's type): 如果是这样,则必须强制转换(或更改其类型):
((SmartFormView) View).BeforeLoad();
View is obviously of some base type, like FrameworkElement. 视图显然是某种基本类型,例如FrameworkElement。 Try this code:
试试这个代码:
SmartFormView myView = new SmartFormView();
View = myView;
myView.BeforeLoad();
You don't show your PresenterBase class, but the PresenterBase.View property probably isn't of type SmartFormView. 您没有显示PresenterBase类,但是PresenterBase.View属性可能不是SmartFormView类型。 I'm not sure what type it is, but I'm guessing UserControl or one of its ancestors.
我不确定它是什么类型,但是我猜是UserControl或其祖先之一。
Here are some options: 以下是一些选项:
Make your base class generic on the view type, so that in SmartFormPresenter, the View property really can be of type SmartFormView. 使您的基类对视图类型通用,以便在SmartFormPresenter中,View属性实际上可以是SmartFormView类型。 Eg:
例如:
public class PresenterBase<T> { ... public T View { get; set; } ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.