As you have probably noticed, I
returned to the consulting field. So I started to publish here again. Today, we’ll speak about some quick extension methods which help me to work
with Windows Phone. We’ll start from
IsolatedStorageFile (frankly, I do not know why they called it "File").
First of all, let’s make a method to delete the directory recursively, including files inside.
public static void DeleteDirectory(this IsolatedStorageFile store, string dir, bool recursive) {
if (store.DirectoryExists(dir)) {
if (recursive) {
foreach (var directory in store.GetDirectoryNames(Path.Combine(dir, "*"))) {
store.DeleteDirectory(directory, true);
}
foreach (var file in store.GetFileNames(Path.Combine(dir, "*"))) {
store.DeleteFile(Path.Combine(dir, file));
}
}
store.DeleteDirectory(dir);
}
}
Usage rather straight-forward (like regular
IsolatedStorageFile.DeleteDirectory
method but with additional parameter.
var store = IsolatedStorageFile.GetUserStoreForApplication();
store.DeleteDirectory(“MyDirectory”, true);
With recursive==true it will delete all inside the directory and the directory itself, without, will work exactly like the original method.
As known. Windows Phone API works only with XDocuments and Xml.Linq, which is good thing, but sometimes, it is nasty getting a value of the attribute you not sure us there. Thus I wrote a method to make the syntax cleaner.
public static string GetAttribute(this XElement element, string name) {
if (element.HasAttribute(name)) return element.Attribute(name).Value;
else return string.Empty;
}
public static bool HasAttribute(this XElement element, string name) {
return element.HasAttributes && element.Attribute(name) != null;
}
Usage is simple:
var items = itm.Descendants("item").Where(e => e.GetAttribute("type") != "default");
foreach (var item in items) {
var pid = item.GetAttribute("id");
…
}
Another problem is the complicated syntax of visual tree manipulations. Thus I wrote some handy methods to handle it.
Check whether the tree contains an object
public static bool IsInVisualTree(this DependencyObject element, DependencyObject treeRoot) {
return element.GetVisualAncestors().Contains(treeRoot);
}
Getting all visual ancestors our of the visual tree on the phone.
public static IEnumerable<DependencyObject> GetVisualAncestors(this DependencyObject element) {
DependencyObject candidate = null;
while (element != null) {
candidate = VisualTreeHelper.GetParent(element);
if (candidate == null) {
var asFe = element as FrameworkElement;
if (asFe != null)
candidate = asFe.Parent;
}
element = candidate;
if (element != null)
yield return element;
}
}
…and descendants for the element inside the tree.
public static IEnumerable<DependencyObject> GetVisualDescendants(this DependencyObject element) {
int childCount = VisualTreeHelper.GetChildrenCount(element);
for (int i = 0; i < childCount; i++) {
var child = VisualTreeHelper.GetChild(element, i);
yield return child;
foreach (var descendant in child.GetVisualDescendants()) {
yield return descendant;
}
}
}
Here are some usage examples
if (originalSource.GetVisualAncestors().OfType<ItemBase>().Any()) { … }
…
itemsHost = this.GetVisualDescendants().OfType<Panel>().Where(p => p is PanelBase).FirstOrDefault()
Since, today I have a number of projects on Windows Phone, I’ll, probably, add some handy methods on going. As for you, you can put those together inside my
golden sugar collection and work easier.
Be good people and have a nice day.
Related posts: