diff options
Diffstat (limited to 'Juick/Classes')
-rw-r--r-- | Juick/Classes/AccountManager.cs | 23 | ||||
-rw-r--r-- | Juick/Classes/BindingChangedEventArgs.cs | 15 | ||||
-rw-r--r-- | Juick/Classes/DelegateCommand.cs | 37 | ||||
-rw-r--r-- | Juick/Classes/DependencyPropertyListener.cs | 52 | ||||
-rw-r--r-- | Juick/Classes/ScrollViewerMonitor.cs | 86 |
5 files changed, 196 insertions, 17 deletions
diff --git a/Juick/Classes/AccountManager.cs b/Juick/Classes/AccountManager.cs index 82fc446..8f8e366 100644 --- a/Juick/Classes/AccountManager.cs +++ b/Juick/Classes/AccountManager.cs @@ -1,20 +1,7 @@ using System;
-using System.IO;
using System.IO.IsolatedStorage;
-using System.Linq;
using System.Net;
-using System.Windows;
using System.Windows.Controls;
-using System.Windows.Documents;
-using System.Windows.Ink;
-using System.Windows.Input;
-using System.Windows.Media;
-using System.Windows.Media.Animation;
-using System.Windows.Navigation;
-using System.Windows.Shapes;
-using System.Windows.Threading;
-using System.Xml;
-using System.Xml.Serialization;
namespace Juick.Classes
{
@@ -25,12 +12,14 @@ namespace Juick.Classes {
get
{
+ string userName;
+ string password;
+ IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("user", out userName);
+ IsolatedStorageSettings.ApplicationSettings.TryGetValue<string>("password", out password);
return new NetworkCredential
{
- UserName = IsolatedStorageSettings.ApplicationSettings.Contains("user") ?
- IsolatedStorageSettings.ApplicationSettings["user"] as string : null,
- Password = IsolatedStorageSettings.ApplicationSettings.Contains("password") ?
- IsolatedStorageSettings.ApplicationSettings["password"] as string : null,
+ UserName = userName,
+ Password = password,
};
}
set
diff --git a/Juick/Classes/BindingChangedEventArgs.cs b/Juick/Classes/BindingChangedEventArgs.cs new file mode 100644 index 0000000..3b68751 --- /dev/null +++ b/Juick/Classes/BindingChangedEventArgs.cs @@ -0,0 +1,15 @@ +using System; +using System.Windows; + +namespace Juick.Classes +{ + public class BindingChangedEventArgs : EventArgs + { + public DependencyPropertyChangedEventArgs EventArgs { get; private set; } + + public BindingChangedEventArgs(DependencyPropertyChangedEventArgs e) + { + EventArgs = e; + } + } +} diff --git a/Juick/Classes/DelegateCommand.cs b/Juick/Classes/DelegateCommand.cs new file mode 100644 index 0000000..cc7adcd --- /dev/null +++ b/Juick/Classes/DelegateCommand.cs @@ -0,0 +1,37 @@ +using System; +using System.Windows.Input; + +namespace Juick.Classes +{ + public class DelegateCommand : ICommand + { + readonly Action action; + readonly Func<bool> canExecute; + + public DelegateCommand(Action execute, Func<bool> canExecute) + { + this.action = execute; + this.canExecute = canExecute; + } + + public bool CanExecute(object parameter) + { + return canExecute(); + } + + public event EventHandler CanExecuteChanged; + + public void Execute(object parameter) + { + action(); + } + + public void NotifyCanExecuteChanged() + { + if (CanExecuteChanged != null) + { + CanExecuteChanged(this, EventArgs.Empty); + } + } + } +} diff --git a/Juick/Classes/DependencyPropertyListener.cs b/Juick/Classes/DependencyPropertyListener.cs new file mode 100644 index 0000000..693c16a --- /dev/null +++ b/Juick/Classes/DependencyPropertyListener.cs @@ -0,0 +1,52 @@ +using System; +using System.Windows; +using System.Windows.Data; + +namespace Juick.Classes +{ + public class DependencyPropertyListener + { + static int index; + + readonly DependencyProperty property; + FrameworkElement target; + public event EventHandler<BindingChangedEventArgs> Changed; + + public DependencyPropertyListener() + { + property = DependencyProperty.RegisterAttached( + "DependencyPropertyListener" + DependencyPropertyListener.index++, + typeof(object), + typeof(DependencyPropertyListener), + new PropertyMetadata(null, new PropertyChangedCallback(HandleValueChanged))); + } + + public void Attach(FrameworkElement element, Binding binding) + { + if (element == null) + { + throw new ArgumentNullException("element"); + } + if (target != null) + { + throw new InvalidOperationException("Cannot attach an already attached listener"); + } + target = element; + target.SetBinding(property, binding); + } + + public void Detach() + { + target.ClearValue(property); + target = null; + } + + void HandleValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) + { + if (Changed != null) + { + Changed.Invoke(this, new BindingChangedEventArgs(e)); + } + } + } +} diff --git a/Juick/Classes/ScrollViewerMonitor.cs b/Juick/Classes/ScrollViewerMonitor.cs new file mode 100644 index 0000000..6a65773 --- /dev/null +++ b/Juick/Classes/ScrollViewerMonitor.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Input; +using System.Windows.Media; + +namespace Juick.Classes +{ + public class ScrollViewerMonitor + { + public static DependencyProperty AtEndCommandProperty = + DependencyProperty.RegisterAttached("AtEndCommand", typeof(ICommand), typeof(ScrollViewerMonitor), new PropertyMetadata(OnAtEndCommandChanged)); + + public static ICommand GetAtEndCommand(DependencyObject obj) + { + return (ICommand)obj.GetValue(AtEndCommandProperty); + } + + public static void SetAtEndCommand(DependencyObject obj, ICommand value) + { + obj.SetValue(AtEndCommandProperty, value); + } + + public static void OnAtEndCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var element = (FrameworkElement)d; + if (element != null) + { + element.Loaded -= element_Loaded; + element.Loaded += element_Loaded; + } + } + + static void element_Loaded(object sender, RoutedEventArgs e) + { + var element = (FrameworkElement)sender; + element.Loaded -= element_Loaded; + var scrollViewer = FindChildOfType<ScrollViewer>(element); + if (scrollViewer == null) + { + throw new InvalidOperationException("ScrollViewer not found."); + } + + var listener = new DependencyPropertyListener(); + listener.Changed += (s, eArgs) => + { + var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight; + if (atBottom) + { + var atEnd = GetAtEndCommand(element); + if (atEnd != null && atEnd.CanExecute(null)) + { + atEnd.Execute(null); + } + } + }; + var binding = new Binding("VerticalOffset") { Source = scrollViewer }; + listener.Attach(scrollViewer, binding); + } + + static T FindChildOfType<T>(DependencyObject root) + where T : class + { + var queue = new Queue<DependencyObject>(); + queue.Enqueue(root); + + while (queue.Count > 0) + { + var current = queue.Dequeue(); + for (int i = VisualTreeHelper.GetChildrenCount(current) - 1; 0 <= i; i--) + { + var child = VisualTreeHelper.GetChild(current, i); + var typedChild = child as T; + if (typedChild != null) + { + return typedChild; + } + queue.Enqueue(child); + } + } + return null; + } + } +} |