开发者

Class design issue in C#. Pager for control

I have implemented a custom control that takes some custom classes as items and draws them. I want to implement a paging class in order to turn pages but in a way that it can be customi开发者_如何转开发zed. So far i have defined an interface .

public interface IPager
{
    void NextPage(....);
    int CurrentPage(.....);
    void PreviousPage(...);
    int PageCount(.....);
}

My custom control takes an implementation of IPager. The problem is that the instance isnt created from within control. This isnt a problem but it would be if i add instance parameters like currentPage without having to calculate complex things.. the problem is that IPager implementation is outside custom control object but should be used only in one control..

I cannot thing other solution than having a stateless implementation of IPager and keeping instance values in custom control. Also extending custom control class isnt a solution as pager may change dynamically...

Any other solution?


Your custom control should just show a page and not be concerned with maintaining pager state. In MVC parlance it is a view. You're separating the logic of paging and maintaining state from the process of actually displaying the page. Do something like this:

interface IPageView {
    IPager Pager {get;set;}
    void ShowPage(PageData pageData);
}
// This is your custom control:
class PageViewControl : Control, IPageView {
    public IPager Pager { get; set; }
    public void ShowPage(PageData pageData) {
        // show the page data
    }
}
// You've already defined the IPager interface:
interface IPager {
    ... 
}
class Pager : IPager {
    IPageView _view;
    public void SetView(IPageView view) { _view = view; }
    ... // state members, etc
    public void NextPage() {
        // update state, find page data
        _view.ShowPage(pageData);
    }
}

Then to instantiate it, somewhere like the containing form's constructor:

var pager = new Pager(...);
pager.SetView(pageViewControl1);
pageViewControl1.Pager = pager;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜