我有一个像这样设置的页面
public partial class _Default : ViewBasePage<EmployeePresenter, IEmployeeView>,
IEmployeeView
{
...
}
在我的基页内
public abstract class ViewBasePage<TPresenter, TView> :
Page where TPresenter : Presenter<TView> where TView : IView
{
protected TPresenter _presenter;
public TPresenter Presenter
{
set
{
_presenter = value;
_presenter.View = GetView(); // <- Works
//_presenter.View = (TView)this; <- Doesn't work
}
}
/// <summary>
/// Gets the view. This will get the page during the ASP.NET
/// life cycle where the physical page inherits the view
/// </summary>
/// <returns></returns>
private static TView GetView()
{
return (TView) HttpContext.Current.Handler;
}
}
我需要做的是实际施放(TView)_Default,使用我的GetView()方法确实以该结果结束。在基页内我无法做到
_presenter.View = (TView)this;
因为这实际上是ViewBasePage<TPresenter,TView>
所以它不能直接转换为TView。
所以我的实际问题是有没有其他方法以一种感觉不那么黑客的方式来实现我的结果,如果这是主要的选择,那么通过以这种方式处理我的页面真的需要关注什么吗?
修改:
我要写的确切部分是
private static TView GetView()
{
return (TView) HttpContext.Current.Handler;
}
因为我觉得在这种情况下能够引用回到页面是非常严重的。
答案 0 :(得分:1)
我不知道预期(TView)this
如何运作。 this
指的是恰好是Page
的类。您无法将Page
转换为IView
。
您目前的实施并不是一蹴而就。
我错过了什么吗?
编辑:现在我了解你的情况好一点;如何让ViewBasePage继承自IView(并将其从_Default页面中删除)?
EDIT 此外,如果您希望_Default页面必须实现Interface中定义的函数,您可以让ViewBasePage类抽象地实现接口的函数。
public class _Default : ViewBasePage<Presenter<IView>, IView>
{
#region Overrides of classB
public override void test()
{
//perform the test steps.
}
#endregion
}
public abstract class ViewBasePage<TPresenter, TView> :
Page, IView
where TPresenter : Presenter<TView>
where TView : IView
{
protected TPresenter _presenter;
public TPresenter Presenter
{
set
{
_presenter = value;
_presenter.View = (TView)((IView)this); //<- Now it does work
}
}
#region Implementation of IView
public abstract void test();
#endregion
}
public interface IView
{
void test();
}
public abstract class Presenter<TView> where TView : IView
{
public TView View { get; set; }
public virtual void OnViewInitialized(){}
public virtual void OnViewLoaded(){}
}