Tuesday, February 8, 2011

Silverlight 2 and the MVP Pattern

Any ideas on how i get MVP working with Silverlight? How Do I get around the fact there is no load event raised?

This the view I have:

    public partial class Person: IPersonView
    {
        public event RoutedEventHandler Loaded;

        public Person()
        {
            new PersonPresenter(this);

            InitializeComponent();
        }

        public Person Person
        {
            set { Person.ItemsSource = value; }
        }
    }

And my presenter:

 public class PersonPresenter
    {
        private readonly IPersonView _view;
        private readonly ServiceContractClient _client;

        public PersonPresenter(IPersonView view)
        {
            _client = new ServiceContractClient();

            _view = view;
            WireUpEvents();
        }

        private void WireUpEvents()
        {
            _view.Loaded += Load;
        }

        private void Load(object sender, EventArgs e)
        {
            _client.GetPersonCompleted += Client_GetPerson;
            _client.GetPersonAsync();
        }

        private void Client_GetPerson(object sender, GetPersonCompletedEventArgs e)
        {
            _view.Person= e.Result;
        }
    }

Nothing happened for me as the Loaded event dont seem to get called, how do i get around this?

  • I believe the loaded event gets called when the control has been initialized, loaded, rendered and ready for use. This means that as long as you don't place it inside a visible container (so that it is rendered), the loaded event won't be risen.

  • Tim Ross has a good introduction to Silverlight MVP implementation, with source code.

    Dan : Yeah that what i ended up using in the end
    From ANaimi
  • You may consider using MVC# - a Model View Presenter framework with Silverlight 2.0 support.

    Oleg Zhukov

0 comments:

Post a Comment