CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2009
    Posts
    11

    Question XamlParseException in WPF application

    Hi all,

    I'm working through a textbook example on WPF and have encountered an exception that I don't understand. I won't include the XAML file of the application since I don't think that's the cause, but I can post it here if you want.

    The "code-behind" file has the name "Window1.xaml.cs" and is as follows:

    Code:
    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 Presidential_Browser
    {
    
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
    
            public Window1()
            {
                InitializeComponent();
            }
    
            private void PresPhotoListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
    
                ListBox lb = sender as ListBox;
    
                if (lb != null)
                {
    
                    if (lb.SelectedItem != null)
                    {
                        string chosenName = (lb.SelectedItem as ImageURL).Name.ToString();
                        Title = chosenName;
                    }
                    else
                        throw new ArgumentException(
                            "Expected ListBox to call selection changed in PresPhotoListBox_SelectionChanged");
    
                }
    
            }
    
        }
        
    }
    Within the application is also the additional class file "ImageURL.cs":

    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Media.Imaging;
    
    namespace Presidential_Browser
    {
    
        public class ImageURL
        {
    
            public string Path
            {
                get;
                private set;
            }
    
            public Uri ImageURI
            {
                get;
                set;
            }
    
            public BitmapFrame Image
            {
                get;
                private set;
            }
    
            public string Name
            {
                get;
                set;
            }
    
            public ImageURL() {}
    
            public ImageURL(string path, string name)
            {
                Path = path;
                ImageURI = new Uri(Path);
                Image = BitmapFrame.Create(ImageURI);
                Name = name;
            }
    
            public override string ToString()
            {
                return Path;
            }
    
        }
    
        public class Images : List<ImageURL> {}
        
    }
    When I run the application in Visual C# 2008 Express I get an "XamlParseException has occurred" dialog. This has the message " 'DependencyObject' type does not have a public TypeConverter class. Error at Line 52 Position 37."

    The exception arises from the InitialiseComponent() method call in the Window1 constructor of the Window1.xaml.cs file. This then points in the call stack to the following line in the ImageURL.cs class:

    Code:
    public class Images : List<ImageURL> {}
    Any ideas what is causing this?

    Thanks

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: XamlParseException in WPF application

    Include the portion of the xaml code that binds the list box data.

    Most likely you need to write a TypeConverter for the ImageURL class.

    Btw, I don't see you using the ObservableCollection class. Are you using it?

  3. #3
    Join Date
    Jun 2009
    Posts
    11

    Re: XamlParseException in WPF application

    Here's the entire XAML file:

    Code:
    <Window x:Class="Presidential_Browser.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Presidential_Browser"
        Title="President Identifier" ShowInTaskbar="False"
        Height="256" Width="253">
        <Window.Resources>
            <LinearGradientBrush x:Key="ListBoxGradient"
                                 StartPoint="0,0"
                                 EndPoint="0,1">
                <GradientStop Color="#90000000" Offset="0" />
                <GradientStop Color="#40000000" Offset="0.005" />
                <GradientStop Color="#10000000" Offset="0.04" />
                <GradientStop Color="#20000000" Offset="0.945" />
                <GradientStop Color="#60FFFFFF" Offset="1" />
            </LinearGradientBrush>
            
            <Style x:Key="SpecialListStyle"
                   TargetType="{x:Type ListBox}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBox}">
                            <Border BorderBrush="Gray"
                                    BorderThickness="1"
                                    CornerRadius="6"
                                    Background="{DynamicResource ListBoxGradient}">
                                <ScrollViewer VerticalScrollBarVisibility="Disabled"
                                              HorizontalScrollBarVisibility="Visible">
                                    <StackPanel IsItemsHost="True"
                                                Orientation="Horizontal"
                                                HorizontalAlignment="Left" />
                                </ScrollViewer>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
            
            <Style x:Key="SpecialListItem"
                   TargetType="{x:Type ListBoxItem}">
                <Setter Property="MaxHeight" Value="75"/>
                <Setter Property="MinHeight" Value="75"/>
                <Setter Property="Opacity" Value="0.75"/>
                <Style.Triggers>
                    <EventTrigger RoutedEvent="Mouse.MouseEnter">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:0.2"
                                        Storyboard.TargetProperty="MaxHeight" To="85"/>
                                    <DoubleAnimation Duration="0:0:0.2"
                                        Storyboard.Target="Opacity" To="1.0"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                    <EventTrigger RoutedEvent="Mouse.MouseLeave">
                        <EventTrigger.Actions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <DoubleAnimation Duration="0:0:1"
                                        Storyboard.TargetProperty="MaxHeight"/>
                                    <DoubleAnimation Duration="0:0:0.2"
                                        Storyboard.TargetProperty="Opacity"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger.Actions>
                    </EventTrigger>
                </Style.Triggers>
            </Style>
    
            <local:Images x:Key="Presidents">
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/1gw_header_sm.jpg" Name="George Washington" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/2ja_header_sm.jpg" Name="John Adams" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/3tj_header_sm.jpg" Name="Thomas Jefferson" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/4jm_header_sm.jpg" Name="James Madison" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/5jm_header_sm.jpg" Name="James Monroe" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/6ja_header_sm.jpg" Name="John Quincy Adams" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/7aj_header_sm.jpg" Name="Andrew Jackson" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/8mv_header_sm.jpg" Name="Martin van Buren" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/9wh_header_sm.jpg" Name="William H. Harrison" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/10jt_header_sm.jpg" Name="John Tyler" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/11jp_header_sm.jpg" Name="James K. Polk" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/12zt_header_sm.jpg" Name="Zachary Taylor" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/13mf_header_sm.jpg" Name="Millard Fillmore" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/14fp_header_sm.jpg" Name="Franklin Pierce" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/15jb_header_sm.jpg" Name="James Buchanan" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/16al_header_sm.jpg" Name="Abraham Lincoln" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/17aj_header_sm.jpg" Name="Andrew Johnson" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/18ug_header_sm.jpg" Name="Ulysses S. Grant" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/19rb_header_sm.jpg" Name="Rutherford B. Hayes" />
                <local:ImageURL ImageURI="http://www.whitehouse.gov/assets/presidents/20jg_header_sm.jpg" Name="James Garfield" />
            </local:Images>
        </Window.Resources>
    
        <Grid Width="300" Height="180"
              DataContext="{StaticResource Presidents}">
            <Grid.RowDefinitions>
                <RowDefinition Height="20" />
                <RowDefinition  />
            </Grid.RowDefinitions>
            <StackPanel Grid.Row="0" Grid.ColumnSpan="3">
                <TextBlock FontSize="14" Grid.Row="0">
                    United States Presidents
                </TextBlock>
            </StackPanel>
            <StackPanel Grid.Row="1">
                <ListBox Style="{StaticResource SpecialListStyle}"
                    Name="PresPhotoListBox" Margin="0,0,0,20"
                    SelectionChanged="PresPhotoListBox_SelectionChanged"
                    ItemsSource="{Binding }"
                    IsSynchronizedWithCurrentItem="True" SelectedIndex="0"
                    ItemContainerStyle="{StaticResource SpecialListItem}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Border VerticalAlignment="Center"
                                HorizontalAlignment="Center" Padding="4"
                                Margin="2" Background="White">
                                <Image Source="{Binding Path=ImageURI}" />
                            </Border>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>
        </Grid>
    </Window>
    I've not come across the ObservableCollection class before. What is it used for (especially in the context of this application)?

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured