RSS

Tag Archives: Silverlight

Should you choose Silverlight?

There seems to be a bit of controversy surrounding Silverlight these days; in case you hadn’t noticed. This post is my two-penneth on the current situation and what, I think, the future holds.

I’m a staunch advocate of Silverlight, for now. Especially given the current alternatives. My conviction is such that I’m building a startup that uses Silverlight to test the business model, Percollate. Because of my history of C-level languages and Microsoft development environments, the segue into Silverlight was almost painless. Clearly, it will not be around for ever, but for now it’s hard to see a better alternative.

Here are my thoughts with a little more structure:

Silverlight is Free

The compiler is part of the runtime which is freely downloadable. Microsoft provides a great development environment, Visual Studio and offers the Express Edition for free. There are also 3rd-party developer tools available; made possible by virtue of the free compiler and the reflective nature of the BCL.

A Decade of Support

Microsoft recently announced that they would be supporting Silverlight until the end of 2021. That’s 1 decade. 10 years. 120 months.

Compare this to an open source framework like, say, Ruby on Rails. When ‘Ruby on Rails with Feathers and Neon Lights v2’ comes along, the best developers from the community will jump ship. That’s not a criticism, it’s just that they tend to be trail-blazers looking for the latest innovation.

However, Microsoft will still be fixing the bug that you find in their framework in, say, 8 years time. Considering that 1 year is a lifetime in software development, this level of support is not to be sniffed at.

Now consider this: have you ever worked on a project that took 10 years to complete? How about 3 years? If the answer is yes to either of those questions then you’re probably not using anything as elegant as Silverlight. I don’t know any developers or business owners who are having to plan into the next decade and basing today’s framework decisions on that information.

Know your customer

This section could also have been called ‘It doesn’t work on an iPad‘.

I recently took Percollate to Lean Startup Machine. My pitch of the problem and solution received great responses from the room but, when the S word was introduced the number of interested delegates dwindled. This was at the point when none of us knew who the customer was.

My point is that working on an iPad / iPhone etc is important if you’re producing a consumer application. If, however, you are solving a business problem you don’t necessarily need that reach. As an example: during our private beta trials some customers were borrowing neighbours laptops or digging out old PCs to use our application. If you’re building something where the value proposition is high enough, people will put down their iPads for a few minutes.

Go Rich and Narrow

I love the new developer funnel for Windows 8 / WinRT. You can be a C# developer, a C / C++ developer or an HTML5 / CSS3 developer and yet all compile your work into a native Windows 8 application. Superb. The building blocks of Silverlight (XAML and C#) are the basic of this richer environment.

My hope is that Microsoft also inverts this funnel so that Silverlight / C# developers can ‘compile’ their applications to HTML5 and CSS3. Here’s hoping!

It’s good!

It really is very good. You can easily find a multitude of posts that cover the benefits and delights of Silverlight far more eloquently than I can do here. However, the use of observable data binding, GPU acceleration, compile-time validation and now 3D XNA game integration make it a great environment to work in.

Finally, the bad…

Microsoft engineers produce great works but, alas, its management has failed recently. Quite frankly, someone at Microsoft owes the developer community a thumping great apology. The veil of secrecy about new frameworks and platforms is damaging our infrastructure. You need to talk to us more. Seriously. Talk to us.

Advertisements
 
1 Comment

Posted by on December 19, 2011 in Silverlight

 

Tags:

CustomActions (ItemAdorners) in PivotViewer v2

You can download the example solution here.

PivotViewer v1 introduced the concept of CustomActions on tiles. Enabling the behaviour was a 3-stage process. Firstly, you had to implement your own PivotViewer control and override the GetCustomActionsForItem method. Secondly, you had to write some CustomAction-derived classes. Thirdly, you had to handle the ItemActionExecuted event that the PivotViewer control raised in order to evaluate which CustomAction had been executed. This was not an ideal approach.

PivotViewer v2 has a much richer framework for adding custom functionality to your tiles. I’ll cover some advanced techniques in my next post but, for now, I’ll show you how CustomActions have evolved in this version.

The new version also takes a 3-stage approach, but it’s a lot less invasive of the PivotViewer control. For one, you dont’t have to implement your own custom class any more.

Step 1: Creating the ItemAdorner Style

The new PivotViewer control allows us to define a style for the CustomActions; now called ItemAdorners. This is just a standard Silverlight style, but the trick is to include a PivotViewerDefaultItemAdorner control.

<p:PivotViewer.ItemAdornerStyle>
  <Style TargetType="p:PivotViewerItemAdorner">
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="p:PivotViewerItemAdorner">
          <p:PivotViewerDefaultItemAdorner 
            IsTabStop="False"
            DataContext="{TemplateBinding Content}"
            CommandsRequested="GetCommands"
            IsItemSelected="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsItemSelected}" />
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</p:PivotViewer.ItemAdornerStyle>

Notice how we’ve bound the IsItemSelected property. This allows the control to hide itself when the tile doesn’t have focus. Later, we’ll be wiring up the GetCommands event but first we have to create some commands.

Stage 2: Creating Commands

Any command associated with the adorner must implement a new interface IPivotViewerUICommand. This extends the, now familiar, ICommand interface and retains some familiar members from CustomActions in v1…

using System;
using System.Windows.Input;

namespace System.Windows.Controls.Pivot
{
    public interface IPivotViewerUICommand : ICommand
    {
        string DisplayName { get; }
        Uri Icon { get; }
        object ToolTip { get; }
    }
}

So, let’s create a simple Hello World command. We’ll pass in a reference to the underlying item that is the data source of our tile so that the command has access to its caller…

public class HelloWorldCommand : IPivotViewerUICommand
{
  private readonly Person _person;

  public HelloWorldCommand(Person person)
  {
    _person = person;
  }

  public string DisplayName
  {
    get { return "Hello World"; }
  }

  public Uri Icon
  {
    get { return new Uri("http://dev.opera.com/articles/view/opera-extensions-hello-world/hello.png"); }
  }

  public object ToolTip
  {
    get { return "Click this to say hello"; }
  }

  public bool CanExecute(object parameter)
  {
    return true;
  }

  public event EventHandler CanExecuteChanged;

  public void Execute(object parameter)
  {
    MessageBox.Show(_person.Name + " says 'Hello'");
  }
}

Stage 3: Connecting the Commands

Lastly we’ll add a handler for the PivotViewerItemAdorner’s GetCommands event where we’ll instantiate and add the commands…

public partial class MainPage : UserControl
{
  private void GetCommands(object sender, PivotViewerCommandsRequestedEventArgs e)
  {
    e.Commands.Add(new HelloWorldCommand(e.Item as Person));
  }
}

This is the result:

In my next post I’ll show you how you can get creative with ItemAdorners in ways that weren’t possible in v1.

You can download the example solution here.

 
25 Comments

Posted by on October 28, 2011 in PivotViewer

 

Tags: , , , , , , ,

Efficiency Strategies for PivotViewer

Joris Dries started a Twitter conversation this morning about getting decent performance out of the PivotViewer in SL5RC. So, I thought now would be a good time to gather a bunch of ideas I’ve had over the past few months and put them all in one post. This might not answer Joris’ question directly, but it does satisfy the title.

You can download the complete source code of the examples here.

What I’ll do is build a number of scenarios and compare them against a baseline. Measurements will not be empirical but rather based on when the user thinks the PivotViewer is ready for use. For background info this is the laptop I’m running these tests on:

  • Windows 7 Ultimate x64 SP1
  • Chrome 13
  • WEI: 5.0
  • Intel i5 2.27GHz
  • 4GB RAM
  • Non-SSD HDD

Setting the Baseline

I’m going to start with a data class and a method for creating a sample set from it. This will allow us to stress test the new PivotViewer control. I’m not going to include the code for this here as it’s rather long-winded and not the focus of this post. It’s all in the sample solution.

public class Person : INotifyPropertyChanged
{
    public string Forename;
    public string Surname;
    public int Age
    public string Address1;
    public string Address2;
    public string Address3;
    public string Address4;
    public string Postcode;
    public string Sex;
}

And here’s the footprint of the method for creating a pseudo-random set at runtime that we can databind with:

public static ObservableCollection<Person> GetSampleData(int sampleSize);

Now let’s build some simple xaml so we can show our data. This example uses the new PivotProperties and PivotViewerItemTemplate markup and just needs us to specify an ItemsSource at runtime:

<UserControl x:Class="XamlTileEfficiency.NoEfficiencies"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:pivot="clr-namespace:System.Windows.Controls.Pivot;assembly=System.Windows.Controls.Pivot"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <pivot:PivotViewer x:Name="pv">
            <pivot:PivotViewer.ItemTemplates>
                <pivot:PivotViewerItemTemplate>
                    <StackPanel Orientation="Vertical" Height="150" Width="100" Background="LightBlue">
                        <TextBlock Text="{Binding Forename}" />
                        <TextBlock Text="{Binding Surname}" />
                        <TextBlock Text="{Binding Age}" />
                        <TextBlock Text="{Binding Sex}" />
                        <TextBlock Text="{Binding Address1}" />
                        <TextBlock Text="{Binding Address2}" />
                        <TextBlock Text="{Binding Address3}" />
                        <TextBlock Text="{Binding Address4}" />
                        <TextBlock Text="{Binding Postcode}" />
                    </StackPanel>
                </pivot:PivotViewerItemTemplate>
            </pivot:PivotViewer.ItemTemplates>
            <pivot:PivotViewer.PivotProperties>
                <pivot:PivotViewerStringProperty Binding="{Binding Forename}" DisplayName="First Name" Id="ForenameId" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Surname}" DisplayName="Last Name" Id="SurnameId" Options="CanFilter" />
                <pivot:PivotViewerNumericProperty Binding="{Binding Age}" DisplayName="Age" Id="AgeId" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Sex}" DisplayName="Sex" Id="SexId" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Address1}" DisplayName="Address1" Id="Address1Id" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Address2}" DisplayName="Address2" Id="Address2Id" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Address3}" DisplayName="Address3" Id="Address3Id" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Address4}" DisplayName="Address4" Id="Address4Id" Options="CanFilter" />
                <pivot:PivotViewerStringProperty Binding="{Binding Postcode}" DisplayName="Postcode" Id="PostcodeId" Options="CanFilter" />
            </pivot:PivotViewer.PivotProperties>
        </pivot:PivotViewer>
        <Button HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5,2,0,0" Width="100" Height="25" Content="Stop the Clock!" Click="Button_Click" />
    </Grid>
</UserControl>

Lastly let’s write some client code to create the sample set and bind it to our PivotViewer. Note that I’ve also written a rudimentary stopwatch that we’ll use to time when our PivotViewer is ready for a user to interact with it.

public partial class NoEfficiencies : UserControl
    {
        private const int SAMPLE_SIZE = 1000;

        private DateTime _start;

        public NoEfficiencies()
        {
            InitializeComponent();

            Loaded += (sender, e) =>
                {
                    var data = Person.GetSampleData(SAMPLE_SIZE);
                    _start = DateTime.Now;
                    pv.ItemsSource = data;
                };
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var timespan = TimeSpan.FromTicks(DateTime.Now.Ticks - _start.Ticks);

            MessageBox.Show(string.Format("Elapsed time is {0} seconds.", timespan.TotalSeconds.ToString()));
        }
    }
}

On my laptop it takes about 12 seconds for the items to appear in the PivotViewer.

So, what’s happening under the covers when we set the ItemsSource? First of all the control displays the Filter Panel on the left-hand side. This happens almost instantly and gives a good user experience. The delay is in producing the xaml tiles (or trading cards). This is because the runtime has to render static images from the databound markup. This is expensive.

Strategy 1: Use Pre-generated DeepZoom Images

You may have a situation where you can pre-build all your visuals and store them on a server. If you’ve spent any time working with PivotViewer v1 this will be very familiar territory. If your collections are going to be fairly static you can then just use the new CxmlCollectionSource class in combination with some dynamic features that I talked about in my previous post: Add, Remove & Change Cxml Items in PivotViewer.

Alternatively you can you the new PivotViewerMultiSizeImage control in your PivotViewerItemTemplates to render your pre-generated visuals. Either way, you’re swapping the cost of rendering the images from xaml with that of downloading them from the web.

Strategy 1 works well if your visuals are not very dynamic.

Strategy 2: Use Multiple Visual Templates

The PivotViewerItemTemplate I’ve used in my example is very inefficient for the number of tiles. Before I can interact with the control I have to wait for it to render 1000 tiles of information that I can’t possibly read at the default zoom level. So strategy 2 suggests defining multiple visual templates of increasing complexity. This helps by allowing the control to render simpler visual elements first and then the complex ones asynchronously. In the meantime, however, the user has been able to do some work.

            <pivot:PivotViewer.ItemTemplates>
                <!-- This new template will be shown up to a zoomed width of 100px -->
                <pivot:PivotViewerItemTemplate MaxWidth="100">
                    <StackPanel Orientation="Vertical" Height="75" Width="50" Background="LightGreen">
                        <TextBlock Text="{Binding Forename}" />
                        <TextBlock Text="{Binding Surname}" />
                    </StackPanel>
                </pivot:PivotViewerItemTemplate>
                <pivot:PivotViewerItemTemplate>
                    <StackPanel Orientation="Vertical" Height="150" Width="100" Background="LightBlue">
                        <TextBlock Text="{Binding Forename}" />
                        <TextBlock Text="{Binding Surname}" />
                        <TextBlock Text="{Binding Age}" />
                        <TextBlock Text="{Binding Sex}" />
                        <TextBlock Text="{Binding Address1}" />
                        <TextBlock Text="{Binding Address2}" />
                        <TextBlock Text="{Binding Address3}" />
                        <TextBlock Text="{Binding Address4}" />
                        <TextBlock Text="{Binding Postcode}" />
                    </StackPanel>
                </pivot:PivotViewerItemTemplate>
            </pivot:PivotViewer.ItemTemplates>

Once loaded, select an item by clicking on it. You’ll see the higher res template render as you zoom in. Now use the cursor keys to navigate. You’ll see flashes of green as new tiles come into view. Then, and only then, does the runtime render the higher res image.

Scenario 2 take about 8 seconds to load on my machine. 30% faster than our baseline!

Scenario 3: Stagger your loading

Hitting PivotViewer with 1000 items at once can be a bit overwhelming for it. It also means that your user has to wait until ALL 1000 items have been rendered before they can work. In the pre-beta versions of PivotViewer the team had investigated the idea of a BatchObservableCollection class. Unfortunately, this didn’t make it into the RC but we can still replicate the idea. Basically, we can drip feed batches of the whole over a period of time. If your scenario is very visual, this can be a really cool feature as the user gets to see data being assembled in front of them. Ultimately it takes more time, but might give your users the impression that it’s actually quicker!

First up, let’s add a new static method that will add items to an already existing collection.

public static void PopulateWithSampleData(ObservableCollection<Person> data, int sampleSize);

Next we’ll create a class that will handle the drip-feeding of the items. We need to specify an interval between batches so that a) PivotViewer thinks we’ve finished and starts to refresh the UI and b) so that the UI finishes its refresh before the next batch.

using System;
using System.Collections.ObjectModel;
using System.Windows.Threading;

namespace XamlTileEfficiency
{
    public class DripFeeder<T>
    {
        private readonly DispatcherTimer _timer;
        private readonly ObservableCollection<T> _data;
        private readonly int _sampleSize;
        private readonly int _batchSize;
        private readonly Action<ObservableCollection<T>, int> _populateAction;

        private int _counter;

        public DripFeeder(ObservableCollection<T> data, int sampleSize, int batchSize, TimeSpan interval, Action<ObservableCollection<T>, int> populateAction)
        {
            _timer = new DispatcherTimer { Interval = interval };
            _timer.Tick += _timer_Tick;

            _data = data;
            _sampleSize = sampleSize;
            _batchSize = batchSize;
            _populateAction = populateAction;
        }

        void _timer_Tick(object sender, EventArgs e)
        {
            _timer.Stop();

            if (_counter < _sampleSize)
            {
                // This won't take account of any remainders, but hey.
                _populateAction(_data, _batchSize);
                _counter += _batchSize;

                _timer.Start();
            }
        }

        public void Start()
        {
            _timer_Tick(null, EventArgs.Empty);
        }
   }
}

Now that we have all our building blocks we just need to change the client code from our baseline example like this. Notice that we do our databinding BEFORE we start to populate the collection.

            Loaded += (sender, e) =>
            {
                var data = new ObservableCollection<Person>();
                _start = DateTime.Now;
                pv.ItemsSource = data;

                var feeder = new DripFeeder<Person>(data, SAMPLE_SIZE, BATCH_SIZE, TimeSpan.FromSeconds(INTERVAL_SECONDS), Person.PopulateWithSampleData);
                feeder.Start();
            };

So, scenario 3 is great for situations where the gradual loading of data accompanied with the visual changes is OK. In public demos this might be impressive!

Scenario 4: Lazy Load your Facets

In combination with Scenario 2 this can be very powerful. It is also a great way to use PivotViewer with high-latency data. e.g. stuff gathered from multiple sources across the web. As long as our data items implement INotifyPropertyChanged we can lazy load the data asynchronously or triggered when we need it.

In this last example (LazyLoader.xaml) we’ll just load the Forename and Surname properties. Then, as the user selects items in the PivotViewer we’ll go and fetch the rest of the data. The control will handle the PropertyChanged events and re-render the visual elements for us. It’s not the most elegant example, but you should get the potential from it.

Wrap Up

The PivotViewer control is very powerful and flexible but you have to think about loading data in the right way for your situation. I’d love to hear of other scenarios that I haven’t covered here.

Finally, you can download the complete source code of the examples here.

 
11 Comments

Posted by on September 8, 2011 in PivotViewer

 

Tags: , , , ,

Breaking Changes in PivotViewer SL5RC

I’ve been wondering why a bunch of our collections on PhotoPivot haven’t been working in my SL5RC examples. After much scratching of various heads I’ve figured it out. PivotViewer in SL4 tolerated empty Description elements in <Item> tags in cxml files. PivotViewer in SL5 does not. Unfortunately, no errors were raised during loading. Seems like the Parse Exceptions from SL4 aren’t currently in SL5.

Here’s an example:

    <Item Img="#0" Id="0" Href="http://photopivot.com" Name="An Item">
      <!-- Remove this tag completely if you want to run this in SL5 -->
      <Description />
    </Item>
 
Leave a comment

Posted by on September 5, 2011 in PivotViewer

 

Tags: , , ,

Add, Remove & Change Cxml Items in PivotViewer

The first thing that most people will do with PivotViewer v2 in SL5RC is look at an existing static collection that they’ve made. The mechanism for loading an existing cxml file in Silverlight 5 has changed since the EAP releases, but is still very straight forward. Assuming that you already have a PivotViewer control called pv in your xaml, this is all you need (I’m using one of Tony Champion‘s test collections)…

var collection = new CxmlCollectionSource(new Uri("http://labs.championds.com/MIX10/MIX10Collection.cxml"));
collection.StateChanged += (sender, e) =>
{
  if (e.NewState == Loaded)
  {
    pv.PivotProperties = (IList)collection.ItemProperties;
    pv.ItemTemplates = collection.ItemTemplates;
    pv.ItemsSource = collection.Items;
  }
}

Now, Silverlight 5 promised some dynamic control over these collections, but we need to do a little work first. Firstly, the Items property of CxmlCollectionSource is of type ReadOnlyObservableCollection<PivotViewerItem>. This stops us adding and removing any items. What we need to do is construct an ObservableCollection<PivotViewerItem> for our items and then bind that to the control. We’ll keep a reference to this new collection so that we don’t have to keep casting the ItemsSource. This changes our code to:

public partial class MainPage : UserControl
{
  private ObservableCollection<PivotViewerItem> _items;

  public MainPage()
  {
    InitialiseComponent();

    Loaded += LoadCollection();
  }

  private void LoadCollection()
  {
    var collection = new CxmlCollectionSource(new Uri("http://labs.championds.com/MIX10/MIX10Collection.cxml"));

    collection.StateChanged += (sender, e) =>
    {
      if (e.NewState == Loaded)
      {
        _items = new ObservableCollection<PivotViewerItem>(collection.Items);

        pv.PivotProperties = collection.ItemProperties;
        pv.ItemTemplates = collection.ItemTemplates;
        pv.ItemsSource = _items;
      }
    }
  }

Removing Items

Now that we have our Observable Collection, removing items is trivially simple. Assuming you add a Button to your xaml, this is how we would remove the currently selected item:

private void btnRemove_Click(object sender, RoutedEventArgs e)
{
  if (pv.SelectedIndex >= 0)
    _items.RemoveAt(pv.SelectIndex);
}

Adding Items

Adding new items is harder than removing them. There are a number of required properties that need to be specified and then there are the optional ones that the collection’s designer has specified. First off, we’ll create an extension method that will make it easier to find properties on a PivotViewer:

public static class PivotViewerExtensions
{
  public static PivotViewerProperty FindProperty(this PivotViewer pv, string displayName)
  {
    var query = pv.PivotProperties.AsQueryable().OfType<PivotViewerProperty>();

    return query.Where(q => q.DisplayName == displayName).FirstOrDefault();
  }
}

Next we’ll add a Factory class that will help us create a PivotViewerItem with the required properties. The ‘visualCollectionSource’ parameter specifies the xml file for a single DeepZoom image (either .dzi or .xml):

public static class PivotViewerItemFactory
{
  public static PivotViewerItem Create(PivotViewer parentViewer, string id, string name, string description, string visualCollectionSource)
  {
    var item = new PivotViewerItem(id);

    item.Add(parentViewer.FindProperty("Name"), new List<string> { name });
    item.Add(parentViewer.FindProperty("Description"), new List<string> { description });
    item.Add(parentViewer.FindProperty("VisualCollectionSource"), new List<string> { visualCollectionSource });

    return item;
  }
}

Finally, we need to write some client code that will create a ‘standard’ PivotViewerItem and then add any other properties we want:

var newItem = PivotViewerItemFactory.Create(pv, (_data.Count + 1).ToString(), "New Name", "New Description", "http://labs.championds.com/MIX10/MIXCollectionTest_files2/lcy4icqv.3r5.xml");

// Now add the optional properties...
newItem.Add(pv.FindProperty("Code"), new List<string> { "A New Code" });
newItem.Add(pv.FindProperty("Room"), new List<string> { "A New Room" });
newItem.Add(pv.FindProperty("Speakers"), new List<string> { "A New Speaker" });
newItem.Add(pv.FindProperty("Video"), new List<PivotViewerHyperlink> {  new PivotViewerHyperlink("My video", new Uri("http://a.com/b.wmv")) });

// Finally, add our new item to the collection...
_data.Add(newItem);

Changing Items

The issue we have here is that PivotViewerItem gives us no way to access or change the value of any PivotViewerProperty values. What we have to do is remove the property we want to change and then re-add it with the new value. Again we’ll create a convenient extension method for this:

public static class PivotViewerItemExtensions
{
  public static void Replace(this PivotViewerItem item, PivotViewer pivotViewer, string displayName, IList newValues)
  {
    var property = pivotViewer.FindProperty(displayName);

    if (property == null)
      throw new PropertyNotFoundException(displayName);

    item.Remove(property);

    item.Add(property, newValues);
  }

  public class PropertyNotFoundException : Exception
  {
    public readonly string DisplayName;

    public PropertyNotFoundException(string displayName)
      : base("Failed to find a property with Displayname of " + displayName)
    {
      DisplayName = displayName;
    }
  }
}

Calling this from client code then only requires us to know the name of the property and the new value:

if (pv.SelectedIndex >= 0)
  _data[pv.SelectedIndex].Replace(pv, "Room", new List<string> { "A New Room" });

You can download the complete source code here and follow me @GoodCoffeeCode for more blog posts on PivotViewer.

 
4 Comments

Posted by on September 4, 2011 in PivotViewer

 

Tags: , , ,

Inside PivotViewer v2. Episode 1, Legacy Support

Welcome to the first in a series of video blog posts where we’ll be looking at what’s new for PivotViewer v2 in Silverlight 5.

In this episode we’ll take a deep-dive into PivotViewer v2 and look at what legacy support is available to all those that have created static, server-side cxml collections. We’ll also see some great dynamic benefits that can be applied to these existing collections with v2.

Please leave comments, feedback and any suggestions for things you’d like to see in this series.

Inside PivotViewer v2. Episode 1, Legacy Support from Chris Arnold on Vimeo.

Here are the links to the references I make at the end of the video:

Tony Champion (.Net Rocker)
http://tonychampion.net

Xpert360
http://xpert360.com

Kingsley Uyi Idehen
http://twitter.com/kidehen

 
11 Comments

Posted by on May 13, 2011 in PivotViewer

 

Tags: , , , , , ,

Faking PivotViewer in Blend 4

If you’ve been using the new PivotViewer Silverlight control then you’ve probably come across the Blend problem. Basically, it doesn’t work and you get the following error…

Error HRESULT E_FAIL has been returned from a call to a COM component.

This problem does not occur in Visual Studio 2010 (VS2010) – although the page view does look a little suspect. Some people have suggested commenting out the PivotViewer element when opening the page in Blend, but there’s a much better approach – fakes.

First, create a new “Silverlight Class Library” project in VS2010 called FakePivot and, once loaded, delete the Class1.cs file. Now add a new Code File called PivotViewer.cs with the following starting code:

using System;

namespace FakePivot
{
  public class PivotViewer
  {
  }
}

Next add a reference to the PivotViewer library (System.Windows.Pivot.dl) and the SharedUI library (System.Windows.Pivot.SharedUI.dll). If you don’t find them under the .Net tab you can browse for them at <Program Files>\Microsoft SDKs\Silverlight\v4.0\PivotViewer\Jun10\Bin\. Now update your PivotViewer class to look like this…

using System;

namespace FakePivot
{
  public class PivotViewer : System.Windows.Pivot.PivotViewer
  {
  }
}

Right click on Microsoft’s PivotViewer and choose “Go To Definition”. You should now see the following metadata file:

#region Assembly System.Windows.Pivot.dll, v2.0.50727
// C:\Program Files\Microsoft SDKs\Silverlight\v4.0\PivotViewer\Jun10\Bin\System.Windows.Pivot.dll
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Resources;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;

namespace System.Windows.Pivot
{
  [ScriptableType]
  [TemplatePart(Name = "PART_Container", Type = typeof(Grid))]
  public class PivotViewer : Control, INotifyPropertyChanged
  {
    public PivotViewer();
    public PivotViewer(ResourceDictionary colorScheme);

    public IDictionary<string, IList<string>> AppliedFilters { get; }
    public int CollectionItemCount { get; }
    public string CollectionName { get; }
    public Uri CollectionUri { get; }
    public string CurrentItemId { get; set; }
    public ICollection<string> InScopeItemIds { get; }
    public string SortFacetCategory { get; }
    public string ViewerState { get; }

    public event EventHandler CollectionLoadingCompleted;
    public event EventHandler<CollectionErrorEventArgs&gt; CollectionLoadingFailed;
    public event EventHandler<ItemActionEventArgs> ItemActionExecuted;
    public event EventHandler<ItemEventArgs> ItemDoubleClicked;
    public event EventHandler<LinkEventArgs> LinkClicked;
    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual List<CustomAction> GetCustomActionsForItem(string itemId);
    public PivotItem GetItem(string id);
    public void LoadCollection(string collectionUri, string viewerState);
    public override void OnApplyTemplate();
    public static void SetResourceManager(ResourceManager resourceManager);
  }
}

What we want to do next is extract the interface for the standard PivotViewer class. If you have a refactoring tool I’d use it, otherwise you just have to do it manually. Either way, we should now have an interface in our project called IPivotViewer.cs …

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Pivot;

namespace FakePivot
{
  public interface IPivotViewer : INotifyPropertyChanged
  {
    IDictionary<string, IList<string>> AppliedFilters { get; }
    int CollectionItemCount { get; }
    string CollectionName { get; }
    Uri CollectionUri { get; }
    string CurrentItemId { get; set; }
    ICollection<string> InScopeItemIds { get; }
    string SortFacetCategory { get; }
    string ViewerState { get; }

    event EventHandler CollectionLoadingCompleted;
    event EventHandler<CollectionErrorEventArgs> CollectionLoadingFailed;
    event EventHandler<ItemActionEventArgs> ItemActionExecuted;
    event EventHandler<ItemEventArgs> ItemDoubleClicked;
    event EventHandler<LinkEventArgs> LinkClicked;

    PivotItem GetItem(string id);
    void LoadCollection(string collectionUri, string viewerState);
  }
}

Next, we want to go back to our PivotViewer class and make it extend Control and implement our new IPivotViewer interface like this…

using System;
using System.Collections.Generic;
using System.Windows.Pivot;
using System.ComponentModel;
using System.Windows.Controls;

namespace FakePivot
{
  public class PivotViewer : Control, IPivotViewer
  {
    public IDictionary<string, IList<string>> AppliedFilters { get { throw new NotImplementedException(); } }

    public int CollectionItemCount { get { throw new NotImplementedException(); } }

    public string CollectionName { get { throw new NotImplementedException(); } }

    public Uri CollectionUri { get { throw new NotImplementedException(); } }

    public string CurrentItemId { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }

    public ICollection<string> InScopeItemIds { get { throw new NotImplementedException(); } }

    public string SortFacetCategory { get { throw new NotImplementedException(); } }

    public string ViewerState { get { throw new NotImplementedException(); } }

    public event EventHandler CollectionLoadingCompleted;

    public event EventHandler<CollectionErrorEventArgs> CollectionLoadingFailed;

    public event EventHandler<ItemActionEventArgs> ItemActionExecuted;

    public event EventHandler<ItemEventArgs> ItemDoubleClicked;

    public event EventHandler<LinkEventArgs> LinkClicked;

    public event PropertyChangedEventHandler PropertyChanged;

    public PivotItem GetItem(string id) { throw new NotImplementedException(); }

    public void LoadCollection(string collectionUri, string viewerState) { throw new NotImplementedException(); }
  }
}

OK, I know, I know – this is pretty convoluted. But, what we now have is a ‘real’ Silverlight control that exposes the same interface as Microsoft’s control. Here’s how to use it…

Let’s add a new “Silverlight Application” project to our solution – we’ll call it PivotViewerApp. For this example we don’t need to “Host the Silverlight application in a new Web site”. Now add three references to this project (2 of which we already added to our FakePivot project):

  1. System.Windows.Pivot
  2. System.Windows.Pivot.SharedUI
  3. Our FakePivot project

VS2010 should have opened the MainPage.xaml file in “split view” mode. Let’s add some references to our 2 namespaces of interest (pivot and fakepivot) and then create our pivot control…

<UserControl x:Class="PivotViewerApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"             
    xmlns:pivot="clr-namespace:System.Windows.Pivot;assembly=System.Windows.Pivot"
    xmlns:fakepivot="clr-namespace:FakePivot;assembly=FakePivot"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
  <Grid x:Name="LayoutRoot" Background="White">
    <pivot:PivotViewer x:Name="pivotViewer1" />
  </Grid>
</UserControl>

If you now right click on MainPage.xaml in the Solution Explorer and choose “Open in Expression Blend” you’ll see the exception I mentioned at the start of this post. So let’s go back to VS2010 and change the namespace of our pivotViewer1 control from pivot to fakepivot…

<UserControl x:Class="PivotViewerApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"             
    xmlns:pivot="clr-namespace:System.Windows.Pivot;assembly=System.Windows.Pivot"
    xmlns:fakepivot="clr-namespace:FakePivot;assembly=FakePivot"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
  <Grid x:Name="LayoutRoot" Background="White">
    <fakepivot:PivotViewer x:Name="pivotViewer1" />
  </Grid>
</UserControl>

Because both namespaces contain a PivotViewer class our xaml is perfectly happy and the solution will still compile. Now go ahead and reopen the same file in Blend – ta dah, no exceptions.

The real beauty of this approach, however, becomes evident when working with the fake control in Blend. Because it exposes exactly the same events, methods and properties as the Microsoft control we can use the Blend UI to hook into these. Selecting pivotViewer1 in our Objects and Timeline window we can then click the Events button in the Properties tab and see all the events that we’d expect our real control to expose.

Once we’ve done your design work in Blend we, obviously, have to revert our fakepivot namespace to pivot before we can build anything useful. One option is to go back to VS2010 to do this. However, if you hide the Design window in Blend and just have the XAML window visible you can change it there and successfully compile it.

Hopefully, that wasn’t too complicated and, once you have your FakePivot library, you can reuse it in any related projects. If anyone want me to do a video of this process please let me know in the comments.

Finally, click here to download the example solution for this post.

 
1 Comment

Posted by on July 12, 2010 in Silverlight

 

Tags: , , ,