Click here to Skip to main content
15,890,186 members
Home / Discussions / C#
   

C#

 
AnswerRe: How to write a class, where one of its attributes is an Array? Pin
#realJSOP30-Mar-11 7:16
mve#realJSOP30-Mar-11 7:16 
AnswerRe: How to write a class, where one of its attributes is an Array? Pin
davidnz30-Mar-11 10:21
davidnz30-Mar-11 10:21 
GeneralRe: How to write a class, where one of its attributes is an Array? Pin
nstk2-Apr-11 4:40
nstk2-Apr-11 4:40 
QuestionHttpWebRequest load image from rss with UI freezing (windows phone) Pin
peter_piper30-Mar-11 2:09
peter_piper30-Mar-11 2:09 
AnswerRe: HttpWebRequest load image from rss with UI freezing (windows phone) Pin
Luc Pattyn30-Mar-11 3:18
sitebuilderLuc Pattyn30-Mar-11 3:18 
GeneralRe: HttpWebRequest load image from rss with UI freezing (windows phone) Pin
peter_piper30-Mar-11 3:42
peter_piper30-Mar-11 3:42 
GeneralRe: HttpWebRequest load image from rss with UI freezing (windows phone) Pin
Luc Pattyn30-Mar-11 3:54
sitebuilderLuc Pattyn30-Mar-11 3:54 
GeneralRe: HttpWebRequest load image from rss with UI freezing (windows phone) Pin
peter_piper4-Apr-11 3:03
peter_piper4-Apr-11 3:03 
thank you Luc so far, but I think synchronous HttpWebRequest / WebClient methods aren't supportet for WP7. So it was unfortunately impossible for me to use BackGroundWorker with synchronous HttpWebRequest-methods.

So I write it completely new but it's still freezing the UI.
How Luc told me I tried to avoid any use of nested asyn mehtods/threads.

My Idea was to use HttpWebRequest to download it asynyhronous (without other threads... because HttpWebRequest is already asynchronous)

hope the code is better structured then the code above Smile | :)

C#
   public class ComicsViewModel : INotifyPropertyChanged
    {
        public ComicsViewModel()
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                this.ComicList = new ObservableCollection<ComicViewModel>();

                /**** load with WebRequest ***********/
                this.IsReading = true;
                LoadComicsRss2();
                /************************************/
            }
        }

        private void LoadComicsRss2() 
        {
            Uri uri = new Uri("http://schabus.knor.net/rss", UriKind.Absolute);
            HttpWebRequest req = HttpWebRequest.CreateHttp(uri);

            req.BeginGetResponse(HandleGetComicsResponse, req);
        }

        private void HandleGetComicsResponse(IAsyncResult result)
        {
            HttpWebRequest state =  (HttpWebRequest)result.AsyncState;
            using (var response = state.EndGetResponse(result))
            {
                using (var streamReader =  new StreamReader(response.GetResponseStream()))
                {
                    var data = streamReader.ReadToEnd();

                    Deployment.Current.Dispatcher.BeginInvoke(() => { this.ComicList.Clear(); });

                    XElement xel = XElement.Parse(data);

                    var items = xel.Descendants(XName.Get("item"));

                    foreach (var item in items)
                    {
                        var desc = item.Descendants(XName.Get("encoded",
                            "http://purl.org/rss/1.0/modules/content/")).SingleOrDefault();

                        var title = item.Descendants(XName.Get("title")).SingleOrDefault();

                        if (desc != null && title != null)
                        {
                            string url = desc.Value.Substring(desc.Value.IndexOf("src=\"") + 5);
                            string localUrl = url.Substring(0, url.IndexOf("\""));
                            string absoluteUrl = "http://schabus.knor.net" + localUrl;

                            // Add Comic to list
                            ComicViewModel vm = new ComicViewModel();
                            vm.Title = title.Value;
                            vm.Uri = new Uri(absoluteUrl, UriKind.Absolute);

                            Deployment.Current.Dispatcher.BeginInvoke(() => { this.ComicList.Add(vm);});
                        }
                    }
                }
            }
            Deployment.Current.Dispatcher.BeginInvoke(() => { this.IsReading = false; });
        }

}


    <phone:PhoneApplicationPage.Resources>

        <DataTemplate x:Key="ComicViewModelTemplate">
    		<StackPanel>
    			<TextBlock Text="{Binding Title}" HorizontalAlignment="Center"/>
    			<Image Source="{Binding Uri}"/>
    		</StackPanel>
    	</DataTemplate>
           
    </phone:PhoneApplicationPage.Resources>

<Grid x:Name="LayoutRoot" Background="Transparent" DataContext="{Binding Source={StaticResource ComicsViewModelDataSource}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

            <ListBox Margin="10,0" ItemTemplate="{StaticResource ComicViewModelTemplate}" ItemsSource="{Binding ComicList}" SelectedItem="{Binding SelectedComic, Mode=TwoWay}">
            	<Custom:Interaction.Triggers>
            		<Custom:EventTrigger EventName="SelectionChanged">
            			<ic:NavigateToPageAction TargetPage="/ComicPage.xaml"/>
            		</Custom:EventTrigger>
            	</Custom:Interaction.Triggers>
            </ListBox>

        </Grid>

</Grid>

AnswerRe: HttpWebRequest load image from rss with UI freezing (windows phone) Pin
Luc Pattyn4-Apr-11 4:57
sitebuilderLuc Pattyn4-Apr-11 4:57 
GeneralRe: HttpWebRequest load image from rss with UI freezing (windows phone) Pin
peter_piper12-Apr-11 4:12
peter_piper12-Apr-11 4:12 
Questionsort generic list collection Pin
arkiboys29-Mar-11 23:41
arkiboys29-Mar-11 23:41 
AnswerRe: sort generic list collection Pin
Pete O'Hanlon29-Mar-11 23:56
mvePete O'Hanlon29-Mar-11 23:56 
GeneralRe: sort generic list collection Pin
arkiboys30-Mar-11 0:18
arkiboys30-Mar-11 0:18 
GeneralRe: sort generic list collection Pin
Shameel30-Mar-11 0:28
professionalShameel30-Mar-11 0:28 
GeneralRe: sort generic list collection Pin
arkiboys30-Mar-11 0:39
arkiboys30-Mar-11 0:39 
GeneralRe: sort generic list collection Pin
Pete O'Hanlon30-Mar-11 1:01
mvePete O'Hanlon30-Mar-11 1:01 
GeneralRe: sort generic list collection Pin
arkiboys30-Mar-11 1:04
arkiboys30-Mar-11 1:04 
AnswerRe: sort generic list collection Pin
davidnz30-Mar-11 0:48
davidnz30-Mar-11 0:48 
GeneralRe: sort generic list collection Pin
davidnz30-Mar-11 1:02
davidnz30-Mar-11 1:02 
AnswerRe: sort generic list collection Pin
Luc Pattyn30-Mar-11 1:19
sitebuilderLuc Pattyn30-Mar-11 1:19 
GeneralRe: sort generic list collection Pin
Pete O'Hanlon30-Mar-11 1:55
mvePete O'Hanlon30-Mar-11 1:55 
GeneralRe: sort generic list collection Pin
Luc Pattyn30-Mar-11 2:27
sitebuilderLuc Pattyn30-Mar-11 2:27 
QuestionHow to export Datagrid view data to PDF or Excel in C#? Pin
Manandhar Boy29-Mar-11 23:00
Manandhar Boy29-Mar-11 23:00 
AnswerRe: How to export Datagrid view data to PDF or Excel in C#? Pin
Wayne Gaylard29-Mar-11 23:12
professionalWayne Gaylard29-Mar-11 23:12 
AnswerRe: How to export Datagrid view data to PDF or Excel in C#? Pin
BobJanova30-Mar-11 23:40
BobJanova30-Mar-11 23:40 

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.