Click here to Skip to main content
15,901,035 members
Home / Discussions / C#
   

C#

 
AnswerRe: While Loop Question [modified] Pin
Richard Andrew x6418-Apr-10 13:42
professionalRichard Andrew x6418-Apr-10 13:42 
GeneralRe: While Loop Question Pin
harold aptroot18-Apr-10 14:11
harold aptroot18-Apr-10 14:11 
GeneralRe: While Loop Question Pin
dwayne270018-Apr-10 14:47
dwayne270018-Apr-10 14:47 
QuestionClickOnce Deployment Issue Pin
Jon Braunsma18-Apr-10 10:24
Jon Braunsma18-Apr-10 10:24 
Questionservice & interactive service desktop on vista Pin
balu1234518-Apr-10 9:10
balu1234518-Apr-10 9:10 
QuestionMessage Removed Pin
18-Apr-10 4:25
mrkeivan18-Apr-10 4:25 
AnswerRe: creating a schedule similar to sunbirt/outlook .... Pin
riced18-Apr-10 6:17
riced18-Apr-10 6:17 
GeneralRe: creating a schedule similar to sunbirt/outlook .... Pin
mrkeivan18-Apr-10 9:04
mrkeivan18-Apr-10 9:04 
QuestionHow to merg two datatable Pin
linqabc18-Apr-10 4:12
linqabc18-Apr-10 4:12 
AnswerRe: How to merg two datatable Pin
Abhinav S18-Apr-10 4:28
Abhinav S18-Apr-10 4:28 
QuestionVB.Net solution to C# Pin
mehrdadc4818-Apr-10 3:24
mehrdadc4818-Apr-10 3:24 
AnswerRe: VB.Net solution to C# Pin
Abhinav S18-Apr-10 3:42
Abhinav S18-Apr-10 3:42 
AnswerRe: VB.Net solution to C# Pin
Brij18-Apr-10 6:24
mentorBrij18-Apr-10 6:24 
AnswerRe: VB.Net solution to C# Pin
Dave Doknjas18-Apr-10 10:18
Dave Doknjas18-Apr-10 10:18 
QuestionOptimising part of a code thats bulky Pin
malcomhfc18-Apr-10 1:21
malcomhfc18-Apr-10 1:21 
AnswerRe: Optimising part of a code thats bulky Pin
Gideon Engelberth18-Apr-10 16:34
Gideon Engelberth18-Apr-10 16:34 
Well, lets see what's in common between your three methods. Everything is the same except for the address you read and which ItemsControl you are filling. If you combined those two things into a separate class, you could simplify the form code.

The class would look something like this:
C#
public class FeedReader
{
    FeedReader(string address, ItemsControl target)
    {
        _address = address;
        _target = target;
    }
    
    private string _address;
    private ItemsControl _target;
    
    public void LoadFeed()
    {
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(new Uri(_address));
        request.BeginGetResponse(new AsyncCallback(ResponseHandler), request);
    }
    
    private void ResponseHandler(IAsyncResult asyncResult)
    {
        HttpWebRequest request = (HttpWebRequest)asyncResult.AsyncState;
        HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asyncResult);

        if (response.StatusCode == HttpStatusCode.OK)
        {
            XmlReader reader = XmlReader.Create(response.GetResponseStream());
            SyndicationFeed newFeed = SyndicationFeed.Load(reader);
            _target.Dispatcher.BeginInvoke(delegate
            {
                _target.ItemsSource = newFeed.Items;
            });

        }
    }
}


And the calling code could then be:
C#
public NewsFeed()
{
    InitializeComponent();
    SupportedOrientations = SupportedPageOrientation.Portrait;
    Loaded += new RoutedEventHandler(MainPage_Loaded);

    PageTransitionList.Completed += new EventHandler(PageTransitionList_Completed);

    // Set the data context of the listbox control to the sample data
    //DataContext = new MainViewModel();

    //////////////////////////////
    ///// Here's the changes /////
    //////////////////////////////
    FeedReader bbc = new FeedReader("http://www.pchelpforum.com/external.php", ListBbc);
    FeedReader dailyMail = new FeedReader("http://www.dailymail.co.uk/home/index.rss", ListDailyMail);
    FeedReader guardian = new FeedReader("http://www.guardian.co.uk/tv-and-radio/rss", ListGuardian);

    //just call this, no other routines needed for updating.
    //all updates are taken care of by the FeedReader class methods.
    bbc.LoadFeed();
    dailyMail.LoadFeed();
    guardian.LoadFeed();
    
    //////////////////////////////
    //////////////////////////////
    //////////////////////////////
}

GeneralRe: Optimising part of a code thats bulky Pin
malcomhfc21-Apr-10 10:42
malcomhfc21-Apr-10 10:42 
QuestionprogressBar with percents display [modified] Pin
igalep13218-Apr-10 0:55
igalep13218-Apr-10 0:55 
AnswerRe: progressBar with percents display [modified] Pin
Luc Pattyn18-Apr-10 1:11
sitebuilderLuc Pattyn18-Apr-10 1:11 
GeneralRe: progressBar with percents display [modified] Pin
igalep13218-Apr-10 1:21
igalep13218-Apr-10 1:21 
GeneralRe: progressBar with percents display Pin
Som Shekhar18-Apr-10 3:58
Som Shekhar18-Apr-10 3:58 
GeneralRe: progressBar with percents display Pin
igalep13218-Apr-10 4:00
igalep13218-Apr-10 4:00 
GeneralRe: progressBar with percents display Pin
Som Shekhar18-Apr-10 4:04
Som Shekhar18-Apr-10 4:04 
GeneralRe: progressBar with percents display Pin
igalep13218-Apr-10 4:08
igalep13218-Apr-10 4:08 
GeneralRe: progressBar with percents display Pin
Som Shekhar18-Apr-10 4:13
Som Shekhar18-Apr-10 4:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.