Silverlight - bind to URL - silverlight

For some reason when I put
"Img/Covers/Medium/106.jpg"
EDIT:
<Image Grid.Column="0" Stretch="None" HorizontalAlignment="Left" Source="Img/Covers/Medium/106.jpg" Margin="7,0,0,0"></Image>
As the Source for an Image, it works perfectly. But when I try to bind the Source to a property defined as such, it doesn't find it.
public virtual Uri MediumImgURI {
get { return new Uri("Img/Covers/Medium/106.jpg"); }
}
EDIT:
<Image Grid.Column="0" Stretch="None" HorizontalAlignment="Left" Source="{Binding Path=MediumImgURI}" Margin="7,0,0,0"></Image>
Is there something special I have to do to get the latter case to work?
EDIT: Also, making that property a string, instead of Uri causes it to work, but this is an over-simplification - I really need to get it to work with the property as Uri.
EDIT:
When I was linking to images that were in my website, and not my SL app, I had this code (which worked)
public virtual string MediumImgURI {
get { return new Uri(App.Current.Host.Source, String.Format("../Img/Medium/{0}.jpg", CurrentBook.smallID)); }
}
When linking instead to an image in my SL app I thought I could just leave off the first parameter, but it appears not.

Yes, hard to tell from what you have posted. It may be that your binding is failing. Try pulling it up in SilverlightSpy3 and see what the Source is for that image.

Sorry everyone - I did a poor job of researching this myself before asking. As noted here,
https://stackoverflow.com/questions/20586/wpf-image-urisource-and-data-binding
it needs to be a Uri relative to the xap file, so I need a ../ in front of my address.
Thanks for answering.

Related

MahApps panoram control not displaying photo's

I want use the MahApps PanoramaControl to display a bunch of photo's. However I see only the string paths appear in the control, which will be correct if you look at my code.
But I can't figure out howto get it working to show the images instead of the links.
Xaml:
<Controls:Panorama Grid.Row="2"
Grid.ColumnSpan="4"
ItemBox="140"
ItemsSource="{Binding PhotoCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
ViewModel:
string[] filePaths = Directory.GetFiles(#"D:\Google Drive\Images\Backgrounds");
test = new PanoramaGroup("My Photo's", filePaths);
PhotoCollection = new ObservableCollection<PanoramaGroup> { test };
Anyone an idea on how to make it show the images? The control is load as I can scroll sideways on the text.
There is not much documentation on their site on how to get it working...
Or are you using some other Metro style lib for the 4.0 framework?
In order to get MahApps Panorama control i would offer the follow solution below. As to other frameworks that provide this level of detailed MODERN UI experience I have not come across any but interested to know if you do.
Hope the solutions works for you.
You need to add a Data Template the represent the object you're showing.
Start off by defining the object in a POCO class (as illustrated below). Then in the population of the items ensure you translate them to the newly created POCO object instead of leaving them as string values.
public class Photo {
public string Path { get; set; }
}
Next in the definition of you XAML window you need to create a reference to the namespace where the POCO object resides.
xmlns:model="clr-namespace:project.models;assembly=project"
Last, but not least, you want to then create a DataTemplate to represent the object. This is added to the window resources.
<Window.Resources>
<DataTemplate DataType="{x:Type model:Photo}">
<Image Source="{Binding Path}"/>
</DataTemplate>
</Window.Resources>
This should then let the render take place in the interface where it belongs. Hope this works for you.

Playing video in Silverlight with Source binding

I am trying to make a WMV video to play in Silverlight MediaElement. It works in this XAML code:
<MediaElement
x:Name="VideoElement"
Stretch="Fill"
Source=""http://ecn.channel9.msdn.com/o9/pdc09/wmv/CL20.wmv""
Grid.Row="0"
Grid.Column="0"
AutoPlay="True"/>
But when I try to bind the source to some property in my code like this:
<MediaElement
x:Name="VideoElement"
Stretch="Fill"
Source="{Binding VidPath}"
Grid.Row="0"
Grid.Column="0"
AutoPlay="True"/>
Where VidPath is:
public Uri VidPath
{
get
{
return new Uri("http://ecn.channel9.msdn.com/o9/pdc09/wmv/CL20.wmv", UriKind.Absolute);
}
set;
}
It doesn't work. Can you help me figure out why?
First, I'll assume this is an out of browser application with full trust, otherwise cross-domain policy restrictions would prevent the MediaElement from playing that video either way.
Given that, there's nothing wrong with the code you've supplied, but I have a hunch the DataContext of the page that contains your MediaPlayer isn't set correctly. If you put a breakpoint in the getter for VidPath, does it ever get hit? I'm betting no.
Whatever object contains your "VidPath" property, you want to make sure that's the DataContext of your page. E.g. if you just put VidPath as a property in the code-behind, you can add this to the constructor:
this.DataContext = this;

binding an image source in XAML

I am trying to bind an image source to my XAML through c#
this works
<Image Source="images/man.jpg"></Image>
this does not work
<Image Source="images/{Binding imagesource}"></Image>
where imagesource is a string variable in the c# file of this xaml and is being set equal to "man.jpg"
here is a way how to do it in XAML:
add this to the namespace:
xmlns:System="clr-namespace:System;assembly=mscorlib"
then add your images paths
<System:String x:Key="ImageRefresh">/Theme;component/Images/icon_refresh.png</System:String>
<System:String x:Key="ImageSearch">/Theme;component/Images/icon_search.png</System:String>
This is how you use it
<Image Height="16" Source="{StaticResource ImageSearch}" Stretch="Uniform" Width="16"/>
This works ok, but if you load your xaml style in Blend it will go bogus..
An object of type "System.String" cannot be applied to a property that expects the type "System.Windows.Media.ImageSource".
I haven't figured out yet, how to replace System:String with that Media.ImageSource... but hey.. it works for me in Visual Studio.
You can't stick a binding mid-way through the value like that. It's either a binding, or it's not. Assuming imagesource is publicly accessible via your DataContext, you could do this:
<Image Source="{Binding imagesource}"/>
However, if it's been set to "man.jpg" then it won't find the image. Either set imagesource to the full path ("images/man.jpg") or use a converter:
<Image Source="{Binding imagesource, Converter={StaticResource RelativePathConverter}}"/>
The converter would prepend "images/" onto its value. However, it may be necessary for the converter to return an ImageSource rather than a string.
Images have bitten me in the past. There is a certain lookup order involved.
When you use "image/man.jpg" it could refer to a file inside your silverlight xap, or relative to the location of XAP file. For example, it could be in YourProject.Web/ClientBin/image/man.jpg.
You should troubleshoot by using full URLs first and find out if this works.
imagesource needs to be an actual Image object, not a string.
Here is a method that will create a new Image object given a path:
public BitmapImage Load(string path)
{
var uri = new Uri(path);
return new BitmapImage(uri);
}

get absolute file path from image in WPF

I have something like this in my xaml:
<Grid>
<Image Name="image" Source="../../Images/DefaultImage.png" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"></Image>
</Grid>
How can I get (using my code-behind C# code) the absolute path of the image source?
The method which works for me every time is this simple line
(YourImage.Source as BitmapImage).UriSource
In addition you can work on AbsolutePath or AbsoluteUri of UriSource returned according to your need.
When converted from a Uri string or filename, the ImageConverter creates a BitmapDecoder and extracts its first Frame. Getting the Uri should be possible with:
((BitmapFrame)image.Source).Decoder.ToString()
Note that this will generally return an absolute Uri rather than the original Uri found in the XAML. If you need the exact original Uri from the XAML, it is found in the _uri field of the BitmapDecoder but you'll need reflection and elevated code permissions to get to it, plus your code may not work in future NET Framework versions.
You could try this:
string path = ((BitmapImage)image.Source).UriSource.AbsolutePath;

Binding images to Image in Silverlight 2

I'm building a Silverlight 2 application and I need to bind some images to Image object declarated in XAML. I'm doing some other binding in the application and it works just fine, I'm having problem with only images!
This is it's XAML:
<Image Source="{Binding Path=ThumbNail}" Style="{StaticResource ThumbNailPreview}" />
And this is the C# code-behind:
ThumbNail = (string)story.Element("thumbnail").Attribute("src")
I'm just parsing the URL's from a XAML file. When I try to do a foreach loop over all of the ThumbNail properties, it returns the URL as expected. As I said, all other binding works great, so it has to be a problem between the Image control and the ThumbNail property. I also tried chanding the datatype from string to Uri, but nothing happened. Could anyone help me?
I think this has already been asked and answered in StackOverflow here
The key is that the ImageSource property is not a string but a System.Windows.Media.Imaging.BitmapImage type and so needs to be converted.
HTH

Resources