A XAML browser application (XBAP) running in partial trust can safely upload files from a client machine.


   
  


<Page x:Class="SafeFileUploadPartialTrustSample.HomePage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="HomePage"
  xmlns:dialogs="clr-namespace:Microsoft.Win32;assembly=PresentationFramework">
    <StackPanel>
    <Button Name="uploadButton" Click="uploadButton_Click" Width="100">Upload Image...</Button>
    <Image Name="viewImage" Width="500" Height="300"></Image>
    <Label Name="nameLabel"></Label>
    
    </StackPanel>
</Page>
//File:Window.xaml.cs
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Win32;

namespace SafeFileUploadPartialTrustSample {
    public partial class HomePage : Page, IProvideCustomContentState {
        public HomePage() {
            InitializeComponent();
        }

        void uploadButton_Click(object sender, RoutedEventArgs e) {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "Image Files(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|All files (*.*)|*.*";

            if (dlg.ShowDialog() == true) {
                if (this.viewImage.Source != null) {
                    ImageCustomContentState iccs = new ImageCustomContentState(this.viewImage.Source, (string)this.nameLabel.Content);
                    this.NavigationService.AddBackEntry(iccs);
                }
                using (Stream stream = dlg.OpenFile()) {
                    BitmapDecoder bitmapDecoder = BitmapDecoder.Create(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                    this.viewImage.Source = bitmapDecoder.Frames[0];
                    this.nameLabel.Content = dlg.SafeFileName;
                }
            }
        }
        public CustomContentState GetContentState() {
            return new ImageCustomContentState(this.viewImage.Source, (string)this.nameLabel.Content);
        }
    }
    [Serializable]
    public class ImageCustomContentState : CustomContentState
    {

        ImageSource imageSource;
        string filename;

        public ImageCustomContentState(ImageSource imageSource, string filename)
        {
            this.imageSource = imageSource;
            this.filename = filename;
        }

        public override string JournalEntryName
        {
            get { return this.filename; }
        }

        public override void Replay(NavigationService navigationService, NavigationMode mode)
        {
            HomePage homePage = (HomePage)navigationService.Content;
            homePage.viewImage.Source = this.imageSource;
            homePage.nameLabel.Content = this.filename;
        }
    }
}

   
    
     


UI With Page for Dynamic Button content


   
  



<Page    
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="QuickStart4.Page1">
  <StackPanel>
    <Button HorizontalAlignment="Left"
        Width="100"
        Margin="10,10,10,10"
        Click="HandleClick"
        Name="Button1">Click Me</Button>
  </StackPanel>
</Page>

//File:Page1.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;

namespace QuickStart4
{
    public partial class Page1 : Page
    {
        void HandleClick(object sender, RoutedEventArgs e)
        {
            Button1.Content = "Hello World";
        }
    }
}

   
    
     


Create a button when the page loads.


   
  

<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.FELoaded" Loaded="OnLoad" Name="root">
</StackPanel>
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace WpfApplication1 {
    public partial class FELoaded {
        void OnLoad(object sender, RoutedEventArgs e)
        {
            Button b1 = new Button();
            b1.Content = "New Button";
            root.Children.Add(b1);
            b1.Height = 25;
            b1.Width = 200;
            b1.HorizontalAlignment = HorizontalAlignment.Left;
        }
    }
}

   
    
     


Navigate to an instance of a custom class, instead of a Page.


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="WpfApplication1.HomePage"
  xmlns:local="clr-namespace:WpfApplication1"
  WindowTitle="Page that Navigates to an Object">
  <Page.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
      <TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
        <TextBlock FontWeight="Bold">Name:</TextBlock>
        <TextBlock Text="{Binding Path=Name}" />
        <LineBreak />
        <TextBlock FontWeight="Bold">Favorite Color:</TextBlock>
        <TextBlock Text="{Binding Path=FavoriteColor}" FontWeight="Bold">
          <TextBlock.Background>
            <SolidColorBrush Color="{Binding Path=FavoriteColor}" />
          </TextBlock.Background>
        </TextBlock>
      </TextBlock>
    </DataTemplate>
  </Page.Resources>
  <Hyperlink Name="hyperlink" Click="hyperlink_Click">Navigate to Nancy Davolio</Hyperlink>

</Page>
//File:Window.xaml.cs
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Media;

namespace WpfApplication1
{
    public partial class HomePage : Page
    {
        public HomePage()
        {
            InitializeComponent();
        }

        void hyperlink_Click(object sender, RoutedEventArgs e)
        {
            Person person = new Person("A", Colors.Yellow);
            this.NavigationService.Navigate(person);
        }
    }
    public class Person
    {
        string name;
        Color favoriteColor;

        public Person() { }
        public Person(string name, Color favoriteColor)
        {
            this.name = name;
            this.favoriteColor = favoriteColor;
        }

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

        public Color FavoriteColor
        {
            get { return this.favoriteColor; }
            set { this.favoriteColor = value; }
        }
    }
}

   
    
     


Remember and navigate through multiple sets of state for a single page instance


   
  

<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="CustomContentStateNavigationSample.StateNavigationPage" 
  xmlns:ns="clr-namespace:CustomContentStateNavigationSample">
  <DockPanel>
    <DockPanel.Resources>
      <ObjectDataProvider x:Key="usersDataSource" ObjectType="{x:Type ns:Users}"/>
      <DataTemplate x:Key="NameTemplate">
        <TextBlock Text="{Binding Path=Name}"/>
      </DataTemplate>
    </DockPanel.Resources>
    <Button Name="removeBackEntryButton" DockPanel.Dock="Top" Click="removeBackEntryButton_Click" Height="25">Remove Back Entry</Button>
    <ListBox Name="userListBox" DockPanel.Dock="Top" Height="150" SelectionChanged="userListBox_SelectionChanged" 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>

//File:Window.xaml.cs
using System;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections.Generic;
using System.Text;

namespace CustomContentStateNavigationSample
{
    public partial class StateNavigationPage : Page, IProvideCustomContentState
    {
        void removeBackEntryButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.NavigationService.CanGoBack)
            {
                JournalEntry entry = this.NavigationService.RemoveBackEntry();
                UserCustomContentState state = (UserCustomContentState)entry.CustomContentState;
                this.logListBox.Items.Insert(0, "RemoveBackEntry: " + state.JournalEntryName);
            }
        }

        internal void userListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if( e.RemovedItems.Count == 0 ) return;

            User previousUser = e.RemovedItems[0] as User;
            this.logListBox.Items.Insert(0, "AddBackEntry: " + previousUser.Name);

            UserCustomContentState userPageState = new UserCustomContentState(previousUser);
            this.NavigationService.AddBackEntry(userPageState);
        }

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

    public class User
    {
        private string name;
        public User() { }
        public User(string name)
        {
            this.name = name;
        }
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
    }
    [Serializable]
    class UserCustomContentState : CustomContentState
    {
        private User user;

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

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

        public override void Replay(NavigationService navigationService, NavigationMode mode)
        {
            StateNavigationPage page = (StateNavigationPage)navigationService.Content;
            ListBox userListBox = page.userListBox;

            page.userListBox.SelectionChanged -= page.userListBox_SelectionChanged;
            page.userListBox.SelectedItem = this.user;
            page.userListBox.SelectionChanged += page.userListBox_SelectionChanged;
        }
    }

    public class Users : ObservableCollection<User>
    {
        public Users()
        {
            this.Add(new User("A"));
            this.Add(new User("B"));
            this.Add(new User("C"));
            this.Add(new User("D"));
            this.Add(new User("E"));
            this.Add(new User("F"));
            this.Add(new User("G"));
        }
    }
}

   
    
     


Page Loaded event


   
  
<Page       x:Class="WpfApplication1.StartPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      Loaded="Init" />
//File:Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace WpfApplication1
{
    public partial class StartPage : Page
    {
        TextBlock txtElement;
        StackPanel rootPanel;
        Button aButton;
        void Init(object sender, EventArgs args)
        {
            rootPanel = new StackPanel();
            txtElement = new TextBlock();
            aButton = new Button();
            aButton.Content = "Press me";
    
            rootPanel.Children.Add(txtElement);
            rootPanel.Children.Add(aButton);
        }
    }
}

   
    
     


Add Thickness for Padding


   
  

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="250" Width="250" Loaded="Window_Loaded">
    <Canvas Name="canvas1">
    </Canvas>
</Window>
//File:Window.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;

namespace WpfApplication1
{

    public partial class Window1 : Window
    {
        Button button1 = null;
        Button button2 = null;
        Button button3 = null;

        public Window1()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            button1 = new Button { Content = "Button", Width = 70, Height = 23 };
            Canvas.SetLeft(button1, 119);
            Canvas.SetTop(button1, 24);
            canvas1.Children.Add(button1);
            button2 = new Button { Content = "Wider" };
            Canvas.SetLeft(button2, 44);
            Canvas.SetTop(button2, 69);
            canvas1.Children.Add(button2);
            button3 = new Button { Content = "Button" };
            Canvas.SetLeft(button3, 78);
            Canvas.SetTop(button3, 119);
            button3.Padding = new Thickness(10, 2, 10, 2);
            canvas1.Children.Add(button3);
        }
    }
}