diff options
Diffstat (limited to 'Juick/Classes')
-rw-r--r-- | Juick/Classes/DelegateCommand.cs | 6 | ||||
-rw-r--r-- | Juick/Classes/InvokeDelegateCommandAction.cs | 151 | ||||
-rw-r--r-- | Juick/Classes/LowProfileImageLoader.cs | 4 | ||||
-rw-r--r-- | Juick/Classes/ScrollViewerMonitor.cs | 64 |
4 files changed, 157 insertions, 68 deletions
diff --git a/Juick/Classes/DelegateCommand.cs b/Juick/Classes/DelegateCommand.cs index cc7adcd..40256e5 100644 --- a/Juick/Classes/DelegateCommand.cs +++ b/Juick/Classes/DelegateCommand.cs @@ -5,10 +5,10 @@ namespace Juick.Classes { public class DelegateCommand : ICommand { - readonly Action action; + readonly Action<object> action; readonly Func<bool> canExecute; - public DelegateCommand(Action execute, Func<bool> canExecute) + public DelegateCommand(Action<object> execute, Func<bool> canExecute) { this.action = execute; this.canExecute = canExecute; @@ -23,7 +23,7 @@ namespace Juick.Classes public void Execute(object parameter) { - action(); + action(parameter); } public void NotifyCanExecuteChanged() diff --git a/Juick/Classes/InvokeDelegateCommandAction.cs b/Juick/Classes/InvokeDelegateCommandAction.cs new file mode 100644 index 0000000..24861cf --- /dev/null +++ b/Juick/Classes/InvokeDelegateCommandAction.cs @@ -0,0 +1,151 @@ +using System; +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.Shapes; +using System.Windows.Interactivity; +using System.Reflection; +using System.Linq; + +namespace Juick.Classes +{ + public sealed class InvokeDelegateCommandAction : TriggerAction<DependencyObject> + { + /// <summary> + /// + /// </summary> + public static readonly DependencyProperty CommandParameterProperty = + DependencyProperty.Register("CommandParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); + + /// <summary> + /// + /// </summary> + public static readonly DependencyProperty CommandProperty = DependencyProperty.Register( + "Command", typeof(ICommand), typeof(InvokeDelegateCommandAction), null); + + /// <summary> + /// + /// </summary> + public static readonly DependencyProperty InvokeParameterProperty = DependencyProperty.Register( + "InvokeParameter", typeof(object), typeof(InvokeDelegateCommandAction), null); + + private string commandName; + + /// <summary> + /// + /// </summary> + public object InvokeParameter + { + get + { + return this.GetValue(InvokeParameterProperty); + } + set + { + this.SetValue(InvokeParameterProperty, value); + } + } + + /// <summary> + /// + /// </summary> + public ICommand Command + { + get + { + return (ICommand)this.GetValue(CommandProperty); + } + set + { + this.SetValue(CommandProperty, value); + } + } + + /// <summary> + /// + /// </summary> + public string CommandName + { + get + { + return this.commandName; + } + set + { + if (this.CommandName != value) + { + this.commandName = value; + } + } + } + + /// <summary> + /// + /// </summary> + public object CommandParameter + { + get + { + return this.GetValue(CommandParameterProperty); + } + set + { + this.SetValue(CommandParameterProperty, value); + } + } + + /// <summary> + /// + /// </summary> + /// <param name="parameter"></param> + protected override void Invoke(object parameter) + { + this.InvokeParameter = parameter; + + if (this.AssociatedObject != null) + { + ICommand command = this.ResolveCommand(); + if ((command != null) && command.CanExecute(this.CommandParameter)) + { + command.Execute(this.CommandParameter); + } + } + } + + private ICommand ResolveCommand() + { + ICommand command = null; + if (this.Command != null) + { + return this.Command; + } + var frameworkElement = this.AssociatedObject as FrameworkElement; + if (frameworkElement != null) + { + object dataContext = frameworkElement.DataContext; + if (dataContext != null) + { + PropertyInfo commandPropertyInfo = dataContext + .GetType() + .GetProperties(BindingFlags.Public | BindingFlags.Instance) + .FirstOrDefault( + p => + typeof(ICommand).IsAssignableFrom(p.PropertyType) && + string.Equals(p.Name, this.CommandName, StringComparison.Ordinal) + ); + + if (commandPropertyInfo != null) + { + command = (ICommand)commandPropertyInfo.GetValue(dataContext, null); + } + } + } + return command; + } + } +} diff --git a/Juick/Classes/LowProfileImageLoader.cs b/Juick/Classes/LowProfileImageLoader.cs index d1eb3da..a69c386 100644 --- a/Juick/Classes/LowProfileImageLoader.cs +++ b/Juick/Classes/LowProfileImageLoader.cs @@ -51,7 +51,7 @@ namespace Juick.Classes [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "UriSource is applicable only to Image elements.")] public static void SetUriSource(Image obj, Uri value) { - if (null == obj) + if (null == obj || null == value) { throw new ArgumentNullException("obj"); } @@ -146,6 +146,8 @@ namespace Juick.Classes pendingRequests[index] = pendingRequests[count - 1]; pendingRequests.RemoveAt(count - 1); count--; + if (pendingRequest.Uri == null) + continue; if (pendingRequest.Uri.IsAbsoluteUri) { // Download from network diff --git a/Juick/Classes/ScrollViewerMonitor.cs b/Juick/Classes/ScrollViewerMonitor.cs deleted file mode 100644 index 4e96797..0000000 --- a/Juick/Classes/ScrollViewerMonitor.cs +++ /dev/null @@ -1,64 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Diagnostics; -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 = (ScrollViewer)element.Parent; - if (scrollViewer == null) - { - throw new InvalidOperationException("ScrollViewer not found."); - } - - var listener = new DependencyPropertyListener(); - listener.Changed += (s, eArgs) => - { - var atBottom = scrollViewer.VerticalOffset >= scrollViewer.ScrollableHeight - scrollViewer.ScrollableHeight/3; - 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); - } - } -} |