Difference between revisions of "WPF CustomContentState"

From no name for this wiki
Jump to: navigation, search
 
Line 1: Line 1:
 +
Sample demonstriert CustomContentState.
 +
Page1.xaml:
 
<source lang="xml">
 
<source lang="xml">
 
<Page x:Class="WpfApplication3.Page1"
 
<Page x:Class="WpfApplication3.Page1"
Line 27: Line 29:
  
 
</Page>
 
</Page>
 +
</source>
 +
 +
Page1.xaml.cs:
 +
<source lang="csharp">
 +
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 System.Collections.ObjectModel;
 +
 +
namespace WpfApplication3
 +
{
 +
    /// <summary>
 +
    /// Interaction logic for Page1.xaml
 +
    /// </summary>
 +
    public partial class Page1 : Page, IProvideCustomContentState
 +
    {
 +
        public Page1()
 +
        {
 +
            InitializeComponent();
 +
        }
 +
 +
        public void RemoveBackEntryButton_Click(object sender, RoutedEventArgs args)
 +
        {
 +
            JournalEntry entry = base.NavigationService.RemoveBackEntry();
 +
            CustomContentState state = (CustomContentState)entry.CustomContentState;
 +
            this.logListBox.Items.Insert(0, "RemoveBackEntry: " + state.JournalEntryName);         
 +
        }
 +
 +
        CustomContentState IProvideCustomContentState.GetContentState()
 +
        {
 +
            User user =  (User) this.userListBox.SelectedItem;
 +
            this.logListBox.Items.Insert(0, "GetContentState: " + user.Name);
 +
            return new UserCustomContentState(user);         
 +
        }
 +
 +
        public void userListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
 +
        {
 +
            if (e.RemovedItems.Count > 0)
 +
            {
 +
                User user = (User) e.RemovedItems[0];
 +
                this.logListBox.Items.Insert(0, "AddBackEntry: " + user.Name);
 +
                UserCustomContentState state = new UserCustomContentState(user);
 +
                base.NavigationService.AddBackEntry(state);               
 +
            }
 +
        }
 +
    }
 +
 +
    public class User
 +
    {
 +
        public User(string name)
 +
        {
 +
            this.name = name;
 +
        }
 +
 +
        string name;
 +
        public string Name
 +
        {
 +
            get { return this.name; }
 +
            set { this.name = value; }
 +
        }
 +
    }
 +
 +
    public class Users : ObservableCollection<User>
 +
    {
 +
        public Users()
 +
        {
 +
            this.Add(new User("Lisa"));
 +
            this.Add(new User("Homer"));
 +
            this.Add(new User("Barth"));
 +
        }
 +
    }
 +
 +
    [Serializable]
 +
    public class UserCustomContentState : CustomContentState
 +
    {
 +
        User user;
 +
 +
        public UserCustomContentState(User user)
 +
        {
 +
            this.user = user;
 +
        }
 +
 +
        public override string JournalEntryName
 +
        {
 +
            get
 +
            {
 +
                return user.Name; 
 +
            }
 +
        }
 +
 +
        public override void Replay(NavigationService navigationService, NavigationMode mode)
 +
        {
 +
            Page1 page = (Page1)navigationService.Content;
 +
            page.userListBox.RemoveHandler(ListBox.SelectionChangedEvent, new SelectionChangedEventHandler(page.userListBox_SelectionChanged));
 +
            page.userListBox.SelectedItem = this.user;
 +
            page.userListBox.AddHandler(ListBox.SelectionChangedEvent, new SelectionChangedEventHandler(page.userListBox_SelectionChanged));
 +
        }
 +
 +
    }
 +
}
 
</source>
 
</source>

Latest revision as of 15:41, 10 February 2010

Sample demonstriert CustomContentState. Page1.xaml:

<Page x:Class="WpfApplication3.Page1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ns="clr-namespace:WpfApplication3"
    Title="Page1">

    <DockPanel>

        <DockPanel.Resources>
            <ObjectDataProvider x:Key="usersDataSource" ObjectType="{x:Type ns:Users}"/>
            <DataTemplate x:Key="NameTemplate">
                <TextBlock Text="{Binding Path=Name}"/>
      </DataTemplate>
        </DockPanel.Resources>

        <Label FontWeight="Bold" DockPanel.Dock="Top">State Navigation</Label>
        <TextBlock TextWrapping="Wrap" DockPanel.Dock="Top">Change the list selection several times, then navigate backwards and forwards to reapply previous and next list selections.</TextBlock>

        <Button Name="removeBackEntryButton" DockPanel.Dock="Top" Click="RemoveBackEntryButton_Click" Height="25">Remove Back Entry</Button>

        <ListBox Name="userListBox"  SelectionChanged="userListBox_SelectionChanged" DockPanel.Dock="Top" Height="150"  DataContext="{StaticResource usersDataSource}" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource NameTemplate}" />

        <ListBox Name="logListBox" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible"></ListBox>

    </DockPanel>

</Page>

Page1.xaml.cs:

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 System.Collections.ObjectModel;

namespace WpfApplication3
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page, IProvideCustomContentState
    {
        public Page1()
        {
            InitializeComponent();
        }

        public void RemoveBackEntryButton_Click(object sender, RoutedEventArgs args)
        {
            JournalEntry entry = base.NavigationService.RemoveBackEntry();
            CustomContentState state = (CustomContentState)entry.CustomContentState;
            this.logListBox.Items.Insert(0, "RemoveBackEntry: " + state.JournalEntryName);           
        }

        CustomContentState IProvideCustomContentState.GetContentState()
        {
            User user =  (User) this.userListBox.SelectedItem;
            this.logListBox.Items.Insert(0, "GetContentState: " + user.Name);
            return new UserCustomContentState(user);           
        }

        public void userListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.RemovedItems.Count > 0)
            {
                User user = (User) e.RemovedItems[0];
                this.logListBox.Items.Insert(0, "AddBackEntry: " + user.Name);
                UserCustomContentState state = new UserCustomContentState(user);
                base.NavigationService.AddBackEntry(state);                 
            }
        }
    }

    public class User
    {
        public User(string name)
        {
            this.name = name;
        }

        string name;
        public string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
    }

    public class Users : ObservableCollection<User>
    {
        public Users()
        {
            this.Add(new User("Lisa"));
            this.Add(new User("Homer"));
            this.Add(new User("Barth"));
        }
    }

    [Serializable]
    public class UserCustomContentState : CustomContentState
    {
        User user;

        public UserCustomContentState(User user)
        {
            this.user = user;
        }

        public override string JournalEntryName
        {
            get
            {
                return user.Name;   
            }
        }

        public override void Replay(NavigationService navigationService, NavigationMode mode)
        {
            Page1 page = (Page1)navigationService.Content;
            page.userListBox.RemoveHandler(ListBox.SelectionChangedEvent, new SelectionChangedEventHandler(page.userListBox_SelectionChanged));
            page.userListBox.SelectedItem = this.user;
            page.userListBox.AddHandler(ListBox.SelectionChangedEvent, new SelectionChangedEventHandler(page.userListBox_SelectionChanged)); 
        }

    }
}