How could I refactor this part of my windows phone app

Im not very familiar with refactoring but I feel like I need to do some changes in this code... I have 4 buttons in my app. Each one calls an method that receives data from an online xml and use this data to populate a listbox. I kept myself repeating code for all those methods, although I see they differ sometimes. Here is a little explanation of my logic: The buttons btnA_Click and btnB_Click get the same data, but from differente years. The same goes for the other 2 buttons. They get the same data(not the same as btnA and btnB) but from different years too.

Buttons:

private void btnA_Click(object sender, RoutedEventArgs e)
    {
        string webService = @"xml from the web";
        table.OpenReadAsync(new Uri(webService));
        btnA.IsEnabled = false;
        btnB.IsEnabled = false;
        progressBar1.Visibility = System.Windows.Visibility.Visible;
    }

private void btnB_Click(object sender, RoutedEventArgs e)
    {
        string webService = @"xml from the net";
        table.OpenReadAsync(new Uri(webService));
        btnA.IsEnabled = false;
        btnB.IsEnabled = false;
        progressBar1.Visibility = System.Windows.Visibility.Visible;
    }

private void btnSTA_Click(object sender, RoutedEventArgs e)
    {
        string webService = @"xml from the web;
        stats.OpenReadAsync(new Uri(webService));
        btnSTA.IsEnabled = false;
        btnSTB.IsEnabled = false;
        progressBar3.Visibility = System.Windows.Visibility.Visible;
    }

private void btnSTB_Click(object sender, RoutedEventArgs e)
    {
        string webService = @"xml from the web;
        stats.OpenReadAsync(new Uri(webService));
        btnSTA.IsEnabled = false;
        btnSTB.IsEnabled = false;
        progressBar3.Visibility = System.Windows.Visibility.Visible;
    }

Methods:

void table_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null && !e.Cancelled)
        {
            XElement xml = XElement.Load(e.Result);
            var table = LINQ Statement...
            listBox1.ItemsSource = table;
            btnClassificacaoSerieA.IsEnabled = true;
            btnClassificacaoSerieB.IsEnabled = true;
            progressBar1.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

void stats_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error == null && !e.Cancelled)
        {
            XElement xml = XElement.Load(e.Result);
            var stats = LINQ Statement...
            listBox3.ItemsSource = stats;
            btnSTA.IsEnabled = true;
            btnSTB.IsEnabled = true;
            progressBar3.Visibility = System.Windows.Visibility.Collapsed;
        }
    }

Im a little lost here. What could I do to make this code more Object Oriented? Thx!

Answers


Your click events seem to mostly be the same... Make a sub that does that stuff and call the sub from the click event. See if you can put any differences in parameters.

Also your code for hiding / showing the progress bar and enable / disable of buttons is pretty much the same. stuff that in to a sub with a param that lets you say if you want to hide or show.


Need Your Help

How to use if else condition to change doctype according to the browser in html file.?

html

We are working on :hover pseudo-class in IE 7, which works only if the docktype is

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.