Permalink
Browse files

Add Image viewing

  • Loading branch information...
1 parent dd4dab6 commit 287de1a9e61d016e282f2a82d83acc1b16188980 @paulcbetts committed May 8, 2012
@@ -106,6 +106,9 @@
<Compile Include="Views\CacheView.xaml.cs">
<DependentUpon>CacheView.xaml</DependentUpon>
</Compile>
+ <Compile Include="Views\ImageValueView.xaml.cs">
+ <DependentUpon>ImageValueView.xaml</DependentUpon>
+ </Compile>
<Compile Include="Views\JsonValueView.xaml.cs">
<DependentUpon>JsonValueView.xaml</DependentUpon>
</Compile>
@@ -131,6 +134,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
+ <Page Include="Views\ImageValueView.xaml">
+ <Generator>MSBuild:Compile</Generator>
+ <SubType>Designer</SubType>
+ </Page>
<Page Include="Views\JsonValueView.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
@@ -53,8 +53,10 @@ IKernel createStandardKernel()
ret.Bind<ICacheValueViewModel>().To<TextValueViewModel>().Named("Text");
ret.Bind<ICacheValueViewModel>().To<JsonValueViewModel>().Named("Json");
+ ret.Bind<ICacheValueViewModel>().To<ImageValueViewModel>().Named("Image");
ret.Bind<IViewForViewModel<TextValueViewModel>>().To<TextValueView>();
ret.Bind<IViewForViewModel<JsonValueViewModel>>().To<JsonValueView>();
+ ret.Bind<IViewForViewModel<ImageValueViewModel>>().To<ImageValueView>();
return ret;
}
@@ -3,6 +3,10 @@
using System.Linq;
using System.Reactive.Linq;
using System.Text;
+using System.Windows;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using Akavache;
using Newtonsoft.Json;
using ReactiveUI;
@@ -70,4 +74,43 @@ public JsonValueViewModel()
.ToProperty(this, x => x.TextToDisplay);
}
}
-}
+
+ public class ImageValueViewModel : ReactiveObject, ICacheValueViewModel
+ {
+ byte[] _Model;
+ public byte[] Model
+ {
+ get { return _Model; }
+ set { this.RaiseAndSetIfChanged(x => x.Model, value); }
+ }
+
+ ObservableAsPropertyHelper<BitmapImage> _Image;
+ public BitmapImage Image {
+ get { return _Image.Value; }
+ }
+
+ ObservableAsPropertyHelper<Visibility> _ImageVisibility;
+ public Visibility ImageVisibility {
+ get { return _ImageVisibility.Value; }
+ }
+
+ ObservableAsPropertyHelper<Visibility> _ErrorVisibility;
+ public Visibility ErrorVisibility {
+ get { return _ErrorVisibility.Value; }
+ }
+
+ public ImageValueViewModel()
+ {
+ this.WhenAny(x => x.Model, x => x.Value)
+ .Where(x => x != null)
+ .SelectMany(BitmapImageMixin.BytesToImage)
+ .LoggedCatch(this, Observable.Return<BitmapImage>(null))
+ .ToProperty(this, x => x.Image);
+
+ this.WhenAny(x => x.Image, x => x.Value != null ? Visibility.Visible : Visibility.Hidden)
+ .ToProperty(this, x => x.ImageVisibility);
+ this.WhenAny(x => x.ImageVisibility, x => x.Value == Visibility.Visible ? Visibility.Hidden : Visibility.Visible)
+ .ToProperty(this, x => x.ErrorVisibility);
+ }
+ }
+}
@@ -16,12 +16,13 @@
<ColumnDefinition Width="0.579*"/>
</Grid.ColumnDefinitions>
- <ListBox ItemsSource="{Binding Keys}" SelectedItem="{Binding SelectedKey, Mode=TwoWay}" Margin="0,0,6,0" Grid.RowSpan="2" />
+ <ListBox ItemsSource="{Binding Keys}" SelectedItem="{Binding SelectedKey, Mode=TwoWay}" Margin="0,0,6,0" Grid.Row="1" />
<GridSplitter Width="5" HorizontalAlignment="Right" Background="Gray" Grid.RowSpan="2" />
<StackPanel Orientation="Horizontal" Grid.Column="1">
<RadioButton x:Name="textRadio" Content="Text" Margin="16,0,0,0" IsChecked="True" Tag="Text"/>
<RadioButton x:Name="jsonRadio" Content="JSON" Margin="16,0,0,0" Tag="Json" />
+ <RadioButton x:Name="imageRadio" Content="Image" Margin="16,0,0,0" Tag="Image" />
</StackPanel>
<local:ViewModelViewHost ViewModel="{Binding SelectedValue}" Grid.Column="1" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" Grid.Row="1" />
@@ -27,10 +27,10 @@ public CacheView()
{
InitializeComponent();
- Observable.Merge(
- textRadio.ObservableFromDP(x => x.IsChecked).Where(x => x.Value == true).Select(x => x.Sender.Tag),
- jsonRadio.ObservableFromDP(x => x.IsChecked).Where(x => x.Value == true).Select(x => x.Sender.Tag)
- ).Subscribe(x => ViewModel.SelectedViewer = (string)x);
+ new[] { textRadio, jsonRadio, imageRadio }
+ .Select(y => y.ObservableFromDP(x => x.IsChecked).Where(x => x.Value == true).Select(x => x.Sender.Tag))
+ .Merge()
+ .Subscribe(x => ViewModel.SelectedViewer = (string)x);
}
public CacheViewModel ViewModel {
@@ -0,0 +1,17 @@
+<UserControl x:Class="AkavacheExplorer.Views.ImageValueView"
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+ mc:Ignorable="d" x:Name="userControl"
+ d:DesignHeight="300" d:DesignWidth="300">
+
+ <Grid DataContext="{Binding ViewModel, ElementName=userControl}">
+ <ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Hidden">
+ <Grid>
+ <Image Source="{Binding Image}" Visibility="{Binding ImageVisibility}" HorizontalAlignment="Center" VerticalAlignment="Center" />
+ <TextBlock Text="Couldn't load this as an image" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="{Binding ErrorVisibility}" />
+ </Grid>
+ </ScrollViewer>
+ </Grid>
+</UserControl>
@@ -0,0 +1,41 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+using AkavacheExplorer.ViewModels;
+using ReactiveUI.Routing;
+
+namespace AkavacheExplorer.Views
+{
+ /// <summary>
+ /// Interaction logic for ImageValueView.xaml
+ /// </summary>
+ public partial class ImageValueView : UserControl, IViewForViewModel<ImageValueViewModel>
+ {
+ public ImageValueView()
+ {
+ InitializeComponent();
+ }
+
+ public ImageValueViewModel ViewModel {
+ get { return (ImageValueViewModel)GetValue(ViewModelProperty); }
+ set { SetValue(ViewModelProperty, value); }
+ }
+ public static readonly DependencyProperty ViewModelProperty =
+ DependencyProperty.Register("ViewModel", typeof(ImageValueViewModel), typeof(ImageValueView), new UIPropertyMetadata(null));
+
+ object IViewForViewModel.ViewModel {
+ get { return ViewModel; }
+ set { ViewModel = (ImageValueViewModel) value; }
+ }
+ }
+}

0 comments on commit 287de1a

Please sign in to comment.