|
![Go to Parent](https://www.codeproject.com/App_Themes/CodeProject/Img/arrow-up24.png) 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
public class ComicsViewModel : INotifyPropertyChanged
{
public ComicsViewModel()
{
if (!DesignerProperties.IsInDesignTool)
{
this.ComicList = new ObservableCollection<ComicViewModel>();
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;
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>
|
|
|
|
|
Hi Peter,
1.
I'm not familiar with WP7, and am unaware of what .NET parts are missing over there, sorry.
2.
"it's still freezing the UI" isn't informative enough: is it just dead for a while and in the end produces all the output hoped for? does it do anything at all? or is it just dead permanently right from the start?
3.
I still see a foreach loop with a BeginInvoke inside. That could go wrong in at least two ways: either the BeginInvoke for some reason is blocking, turning the whole loop in a long operation, hence freezing the GUI for a long while. Or all those Invokes get queued or even launched somehow, possibly overloading the system.
Suggestions:
- add logging with timestamps, so you KNOW what is happening;
- reduce the loop to just one or two iterations.
In general: when in trouble, simplify, improve observability, get the facts.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
2. I meant the UI wasn't responsive for some time minute
luckily I found my answer in those two Links here:
http://tinyurl.com/28zb9x3
http://tinyurl.com/636qz5z
anyways thank you Luc!
|
|
|
|
|
A generic list i.e. mResults has several fields and records...
Fields are such as field1, field2, field3, etc...
How can I sort this list by Field2? Is it easier with LINQ?
Thanks
|
|
|
|
|
Well, the LINQ approach is fairly easy:
var sortedList = from res in mResults
order by res.Field2
select res;
|
|
|
|
|
This is what I am using but there is an error:
var sortedList = from res in mResults
order by res.Field2
select res;
return (List<ClassName>)sortedList
Error is:
Unable to cast object of type 'System.Linq.OrderedEnumerable`2[ProjectName.ClassName,System.String]' to type 'System.Collections.Generic.List`1[ProjectName.ClassName]'.
|
|
|
|
|
Try this code, I didn't test it though
return sortedList.ToList<ClassName>();
|
|
|
|
|
The error has gone but:
at present I have the mResult being returned in a method.
i.e. return mResult;
Now I am using the linq code you sent i.e.
internal static List<Classname> mResult
private void MethodName()
{
...
...
var sortedList = from res in mResult
orderby res.field2
select res;
return sortedList.ToList<ClassName>();
}
Questioin:
How can I declare the var sortedList to be something like:
internal static var sortedList
and then in the LINQ code I do not use the var to declare?
Thanks
|
|
|
|
|
You don't use the implicit var. You declare the sortedList as a List<ClassName> and then your query becomes:
sortedList = (from res in mResult
orderby res.field2
select res).ToList();
|
|
|
|
|
|
If you mean you already have an instance of List<mResults>, assigned to a variable say list...
list.Sort((i, j) => { return i.Field2.CompareTo(j.Field2); });
should work.
|
|
|
|
|
Actually, I think you mean your list is called mResults, so in that case I would do...
mResults.Sort((i, j) => { return i.Field2.CompareTo(j.Field2); });
|
|
|
|
|
This article[^] tells you how to do it without LINQ.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Nah. It's crap. Only kidding - it's a good article.
A small critique - the plural of criterion is criteria, so you have s singular of "the criterion is...", where the plural would be "Multiple sort criteria are easy to implement". A criterium is actually a bike race.
|
|
|
|
|
Thanks Pete.
In Dutch we use the Latin word criterium for both; English apparently has borrowed the Greek word κριτήριο; all fixed now.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I am confused with the process to export datagridview data to excel or pdf format. Could somebody help me on this please?
Thank you
|
|
|
|
|
I am sure there is no need to shout. To export data to excel look up
ADO.Net or
Excel Interop . to export data to pdf try iTextSharp library. In future you don't need to use bold letters. Most people in these forums can read quite fine.
|
|
|
|
|
For Excel, just put it on the clipboard. (DataGridView.GetClipboardContent.)
For PDF there is no obvious meaning for what you want, never mind a method to do it. PDF is a display format not a data table one. If you want to create a PDF which has within it some text and lines that makes it look like a table, you can do that yourself (or find a library that does it).
|
|
|
|
|
Hi,
I have two classes say Class A and Class B. I am trying to find how
do I get reference the loaded instance of Class A in Class B.
I do not want to get a new instance of Class A. I want to be able to
reference to the loaded instance and use the loaded methods and properties.
Class A
{
...
Class A myClass = New Class A();
...
...
Class B clsb = New Class B();
clsb.DoSomething(int a, int b)
}
Class B
{
...
public void DoSomething(int a, int b)
{
....
//I want to be able to reference to the loaded instance which is 'myClass' of the Class A. (not a new instance)
}
}
Can some one please help me with this.
Thanks,
L
.
|
|
|
|
|
|
ClassA should provide a static method or a static property which returns the instance created inside it. This method/property will help others access that instance.
|
|
|
|
|
If having a static reference (or the Singleton pattern, basically the same thing) is inappropriate, you could pass it in in the constructor or as a method parameter on class B.
I'm surprised this works, though - maybe it's the pseudo-code you've presented, but this:
public class ClassA()
{
private ClassA myClass = new ClassA();
}
is the same as:
public class ClassA()
{
private ClassA myClass = null;
public ClassA()
{
myClass = new ClassA();
}
}
Won't this end up with a stack overflow?
|
|
|
|
|
GlobX wrote: Won't this end up with a stack overflow?
Why not just run it and see for yourself. I guess it will.
|
|
|
|
|
It was more intended as a rhetorical question, like a "have you thought about this...?" 'sides, too busy hating on BizTalk to play around ![Frown | :(](https://codeproject.global.ssl.fastly.net/script/Forums/Images/smiley_frown.gif)
|
|
|
|
|
GlobX wrote: hating on BizTalk
Just work on SharePoint as well and you have witnessed hell.
|
|
|
|