0

I have a WPF user control inside windows form project and I have a problem. Every second time my app crashes as element host can't load data in user control.

Same thing happens when I put this in WPF project. Every second time my app starts normally.

Any idea what could be the problem? This is my user control code:

<UserControl x:Class="Fleet_management.Info"
             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" Height="492" Width="578">
    <UserControl.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFE2E2E2" Offset="0" />
            <GradientStop Color="White" Offset="1" />
        </LinearGradientBrush>
    </UserControl.Background>

    <UserControl.Resources>
        <XmlDataProvider x:Key="rssData" XPath="//item" Source="******" />
    </UserControl.Resources>

    <Grid Margin="3" Height="598" Width="565">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="252*" />
            <ColumnDefinition Width="90*" />
            <ColumnDefinition Width="223*" />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="177*" />
            <RowDefinition Height="55*" />
            <RowDefinition Height="122*" />
            <RowDefinition Height="177*" />
        </Grid.RowDefinitions>

        <ListBox x:Name="lstItems" Margin="3,0" ItemsSource="{Binding Source={StaticResource rssData}}"
                 SelectedIndex="0" VerticalAlignment="Stretch" FontStretch="Normal" FontSize="14" FontFamily="Lucida Sans Unicode" Grid.ColumnSpan="3">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Image Width="20" Margin="3" Source="{Binding XPath=enclosure/@url}" />
                        <TextBlock Margin="3" VerticalAlignment="Center" Text="{Binding XPath=title}" FontWeight="Bold" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <StackPanel Grid.Row="1" Orientation="Vertical" DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" Margin="0,0,0,5" Grid.ColumnSpan="3">
            <TextBlock Margin="3" FontSize="13" FontWeight="Bold" Text="{Binding XPath=title, Path=InnerText}" />
            <TextBlock Margin="3" Opacity="0.72" Text="{Binding XPath=pubDate}" />
        </StackPanel>
        <ScrollViewer CanContentScroll="True" Grid.Row="2" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto"  DataContext="{Binding ElementName=lstItems, Path=SelectedItem}" Margin="0,0,3,115" Grid.RowSpan="2" Grid.ColumnSpan="3">
            <TextBlock  Margin="3"
                       FontStyle="Italic" Text="{Binding XPath=description, Path=InnerText}" TextWrapping="Wrap" TextAlignment="Justify" AllowDrop="False"
                       Foreground="#FF0000E7" FontFamily="Lucida Sans Unicode" FontSize="14" Padding="0,0,5,0" VerticalAlignment="Center" />
        </ScrollViewer>

    </Grid>
</UserControl>
user123_456
  • 5,635
  • 26
  • 84
  • 140

1 Answers1

2

You might want to check if the xml document that contains the data is still locked by the previous app.

So, start up the app. Close it. Check to see if the xml file is still in use.

Also,: add exception handling so you know what exception is being thrown. Use at least Application.DispatherUnhandledException

Emond
  • 50,210
  • 11
  • 84
  • 115
  • source is read from the internet website it is rss reader. How can I put exception handling while this is probbably loaded at runtime. I used WPF element host control in the toolbox and set to load certain user control. – user123_456 May 26 '12 at 09:43
  • I was referring to testing it in the WPF app. – Emond May 26 '12 at 10:12
  • problem is that I am new to WPF and this is why this probably happens, I have probably forgot to dispose something. Can you try to run my code? You can put some rss link that you want, It would probably work – user123_456 May 26 '12 at 10:14
  • I used `http://wpfwonderland.wordpress.com/feed/` as rss source and it works every time. Can you test with this url? – Emond May 26 '12 at 10:50
  • hmm, it starts normal eveytime – user123_456 May 26 '12 at 10:55