TypeConverters

J

Jeroen van Langen

?Hi,

I'm always struggling with wpf/xaml. The image is in the resource..
Why can I use
<image name="testimage" source="test.png">

but in code this fails..

testimage.source = "test.png";

of course.. a string isn't a ImageSource...
but,
testimage.source = new BitmapImage(new Uri( "test.png")); // won't do the
trick either,

Invalid URI: The format of the URI could not be determined. .. this makes it
complicated.....

is there a way to solve it like 'they' do?
something like: testimage.source =
ToImageSourceConvertor.FromString("test.png");


wpf/xaml is way different than forms, many things are below the surface and
can't be traced. Most of the time I'm busy with researching instead of
developing.


Jeroen
 
J

Jeroen van Langen

With some debugging I found that this will work:
pack://application:,,,/APP;component/test.png but that’s not what I wrote in
the xaml. Where does the pack come from?

"Jeroen van Langen" schreef in bericht

?Hi,

I'm always struggling with wpf/xaml. The image is in the resource..
Why can I use
<image name="testimage" source="test.png">

but in code this fails..

testimage.source = "test.png";

of course.. a string isn't a ImageSource...
but,
testimage.source = new BitmapImage(new Uri( "test.png")); // won't do the
trick either,

Invalid URI: The format of the URI could not be determined. .. this makes it
complicated.....

is there a way to solve it like 'they' do?
something like: testimage.source =
ToImageSourceConvertor.FromString("test.png");


wpf/xaml is way different than forms, many things are below the surface and
can't be traced. Most of the time I'm busy with researching instead of
developing.


Jeroen
 
K

kndg

?Hi,

I'm always struggling with wpf/xaml. The image is in the resource..
Why can I use
<image name="testimage" source="test.png">

but in code this fails..

testimage.source = "test.png";

of course.. a string isn't a ImageSource...
but,
testimage.source = new BitmapImage(new Uri( "test.png")); // won't do
the trick either,

Invalid URI: The format of the URI could not be determined. .. this
makes it complicated.....

Uri by default parse the sring as an absolute path. You can either pass
the full path or override the behaviour by passing the UriKind.Relative
parameter.

testimage.Source = new BitmapImage(new Uri( "test.png", UriKind.Relative));
is there a way to solve it like 'they' do?
something like: testimage.source =
ToImageSourceConvertor.FromString("test.png");

Yeah, WPF is fulls with magic :)
Basically below are the steps taken by the CLR to convert the string
attribute in XAML to a .Net object.

1. Check whether that property have TypeConverter attribute and if have
use the the converter specified to convert the string. If not, moves to
step 2.
2. Check whether the type of that property contains the TypeConverter
attribute. If have, convert using the specified converter
3. If step-1 and step-2 fail, throw an exception.

So, since the Source property don't have the TypeConverter attribute, it
moves to step-2. In step-2, the type of Source property is ImageSource,
and ImageSource have a TypeConverter attribute attached to it
(ImageSourceConverter), so it then use that class to parse the string.

So, your below XAML tag

<Image Name="testimage" Source="test.png">

would roughly equals to

Image testimage = new Image();
testimage.Source = new ImageSourceConverter().ConvertFrom("test.png") as
ImageSource;
wpf/xaml is way different than forms, many things are below the surface
and can't be traced. Most of the time I'm busy with researching instead
of developing.

Me too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top