Thursday, October 29, 2009

Monday, October 12, 2009

How to determine an webresponse stream type

               WebRequest request = WebRequest.Create(url);

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

 

                if (response.ContentType.StartsWith("text/html", true, CultureInfo.CurrentCulture))

                {

                    //Html page

                }

Thursday, October 8, 2009

Up comming: Silverlight: how to scroll to target element in ScrollViewer or nested ScrollViewer

   public static class ScrollViewerExtension 
{
/// <summary>
/// Scroll to the current element, if there is has an parent scrollviewer
/// </summary>
/// <param name="target"></param>
public static void ScrollToElement(this UIElement target)
{
ScrollToElement(target, null);
}
/// <summary>
/// Find next level scrollviewer, and scroll to the target element
/// </summary>
/// <param name="target"></param>
/// <param name="childScroller"></param>
private static void ScrollToElement(this UIElement target, ScrollViewer childScroller)
{
try
{
ScrollViewer scrollViewer;
if (childScroller == null)
{
scrollViewer = ParentOfType<ScrollViewer>(target);
}
else
{
scrollViewer = ParentOfType<ScrollViewer>(childScroller);
}
if (scrollViewer != null)
{
if (scrollViewer.ComputedVerticalScrollBarVisibility == Visibility.Visible)
{
UIElement scrollContent = scrollViewer.Content as UIElement;
GeneralTransform gtToContent = target.TransformToVisual(scrollContent);
Point offsetToContent = gtToContent.Transform(new Point(0, 0));
GeneralTransform gtToScroller = target.TransformToVisual(scrollViewer);
Point offsetToScroller = gtToScroller.Transform(new Point(0, 0));
if (offsetToScroller.Y < 5 offsetToScroller.Y > scrollViewer.ViewportHeight)
{
scrollViewer.UpdateLayout();
scrollViewer.ScrollToVerticalOffset(offsetToContent.Y - scrollViewer.ViewportHeight / 4);
}
}
ScrollViewer parentScroller = ParentOfType<ScrollViewer>(scrollViewer);
if (parentScroller != null)
{
ScrollToElement(target, scrollViewer);
}
}
}
catch
{
//TODO: Log exception message here
}
}
private static T ParentOfType<T>(UIElement currentElement) where T:DependencyObject
{
return currentElement.GetVisualAncestors().Where(p => p is T).FirstOrDefault() as T;
}
}