Question Setting BitmapImage.UriSource in a XAML file read with XamlReader

Jayme65

Active member
Joined
Apr 5, 2011
Messages
35
Programming Experience
Beginner
Hello,
My WPF application (WpfApplication1) is, for its XAML part, composed of an single empty grid.
Elements are loaded inside this grid from an external XAML file, read through XamlReader.
This external Xaml file contains (for the purpose of this example) a URL to an image which should be located in the 'My documents' user folder.

The problem is that during importation the url from images should be relative to the application which is loading the file!
In other words, this URL should be relative to the application (pack://siteoforigin:, pack://application)
...and in my case, this path is 'outside' from that context and should be built this way:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "Theme\"

So, I wrote a Converter, hoping to build the complete path url...but without succes!!
> 'Impossible to create the unknow type '{clr-namespace:WpfApplication1}imgurlConvert'.'

The application XAML part:
VB.NET:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:test"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Class="MainWindow" Title="main" Height="600" Width="800">
<Grid></Grid>
</Window>
The application VB part:
VB.NET:
[COLOR=#ff0000]Imports System.IO
Class MainWindow
    Public Shared ThemeTempFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\Theme\"
    Public Shared themeGrid As Grid
    Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
        Debug.Print(ThemeTempFolder)
        loadInterface()
    End Sub
    Sub loadInterface()
        ' Load extracted (grid) xaml file as content of the window
        Dim GridUri As String = ThemeTempFolder & "theme.xaml"
        Dim fs As FileStream = New FileStream(GridUri, FileMode.Open, FileAccess.Read)
        Dim sri = TryCast(System.Windows.Markup.XamlReader.Load(fs), Grid)
        Me.Content = sri
        fs.Close()
        ' Liens, dans le code, aux ?l?ments de l'interface charg?e en Xaml.
        themeGrid = LogicalTreeHelper.FindLogicalNode(sri, "themeGrid")
    End Sub
End Class
Public Class imgurlConvert
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert
        Dim valConv As String = MainWindow.ThemeTempFolder & value
        If File.Exists(valConv) Then
            Return valConv
        End If
        Return Nothing
    End Function
    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Return value
    End Function
End Class[/COLOR]

The XAML file to load:
VB.NET:
<Grid xmlns=[COLOR=#ff0000]"http://schemas.microsoft.com/winfx/2006/xaml/presentation"[/COLOR] xmlns:local=[COLOR=#ff0000]"clr-namespace:WpfApplication1"[/COLOR] xmlns:x=[COLOR=#ff0000]"http://schemas.microsoft.com/winfx/2006/xaml"[/COLOR] xmlns:System=[COLOR=#ff0000]"clr-namespace:System;assembly=mscorlib"[/COLOR] Name=[COLOR=#ff0000]"themeGrid"[/COLOR]>
    <Grid.Resources>
        <local:imgurlConvert x:Key=[COLOR=#ff0000]"urlConvert"[/COLOR] />
    </Grid.Resources>
    <Grid Name=[COLOR=#ff0000]"mainGrid"[/COLOR]>
        <Image Source={Binding Path=[COLOR=#ff0000]"image.png"[/COLOR], Converter={StaticResource urlConvert}} CacheOption=[COLOR=#ff0000]"OnLoad"[/COLOR] x:Name=[COLOR=#ff0000]"Image1"[/COLOR]/>
    </Grid>
</Grid>

I hope that I was clear enough!? Thanks for helping me!!
 

Latest posts

Back
Top