Silverlight : how to put a simple image? - silverlight

I have troubles with element.
Well, I think my path should be wrong but I dont find the problem.
Here is my simple code :
<UserControl x:Class="videoplayer.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Black">
<Image x:Name="btnPlay" Source="cat.jpg" />
</Grid>
</UserControl>*
I have put the file cat.jpg in the same directory that the .xap file, in the parent directory too, and again in the parent directory, but i still dont see any cat when I launch the application ! :(
Only the black grid ...
Someone see the mistake ?
Thanks !
ps : it's silverlight 2 sdk

You have to include the file in your project as a source. then in the property inspector select the image for the Source attribute.

The image will need to have a build-action setting of Resource.
From within Visual Studio:
Select the file,
open the Properties view,
select Resource from the Build Action drop-down.

Related

Show window icon in title bar with MahApps.Metro?

I started using MahApps.Metro just yesterday, and I can't figure out how to get application icon to show as window icon on a window (or better, all of them). Here in the example window icon is included, but I can't seem to get it. When I use
ShowIconOnTitleBar="True"
it doesn't do anything.
What am I doing wrong?
You need to set the Iconproperty like this example (taken from the Mahapps demo app) :
<Controls:MetroWindow x:Class="MetroDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
Title="MahApps.Metro - Demo Application"
Width="960" Height="600"
Icon="mahapps.metro.logo2.ico"
ShowIconOnTitleBar="True"
ShowTitleBar="True">
<Grid />
</Controls:MetroWindow>
I suggest that you download the source code for the Mahapps project, which also contains the source for a demo app (too bad they don't advertise it on their website), it's great for examples. You can find it on GitHub here

View in a module not displaying image but it will display text

I am using MEF, Prism and WPF. I have a simple module (for now, I'll do more with it when I get past this roadblock) that displays a single image sourced as static resource in that module. Here is the xaml for the view:
<UserControl x:Class="SplashScreenModule.SplashView"
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="960" Width="1180">
<Grid>
<Label>
<Image Source="Images/My Image.png"/>
</Label>
</Grid>
The image shows up in the designer but when I run the code the window is blank. If I replace the image with simple text the text is display as expected.
<Label>
This shows up in the window.
</Label>
I've spent the last 3 hours trying to figure this out and am stumped. I would appreciate any help.
Thanks,
Bill
You have to know that there is a difference on how ressources are managed at DesignTime and at RunTime. At DesignTime there is no compilation required, thus it is able to display a ressource even if it is not include in the project.
At runtime this is normally possible, but it is better to include the ressources in your project (ie create a folder Images in your solution, and add the "My Image.png" in this folder), to be sure of the location of your ressource compared to your exe.
If you do it and the image still doesn't show up, try to remove the label and execute again.
By doing this it works fine for me. As soon as the image show up, you can add back the label.
Hope it helps.
The following code is working for me:
<Window x:Class="LabelImage.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Label>
<Image Source="Images/visual studio.png" />
</Label>
</Grid>
</Window>
I structured my solution like you did: I have the picture inside a folder named Images. The MainWindow is located directly under the project node (it's not in any subdirectory).
Make sure that the Build Action for your image is set to resource, otherwise it won't work with the WPF Resource Finding System (which internally uses PACK-Uris: http://msdn.microsoft.com/en-us/library/aa970069(v=vs.110).aspx)
I use .NET 4.5 with VS 2013. If you have any further questions, please feel free to ask.
Update after Bill's comment:
That's a whole different thing when your resource resides within another assembly. The syntax for referencing the image should be like this:
<Image Source="/ClassLibraryForResources;component/Images/visual studio.png" />
where you do the following steps:
Begin with a slash "/"
Specify the name of the assembly that holds your resource (this can also be a strong assembly name)
Continue with ";component/"
End with the path of the image (or other resource)
(Please note that you have to omit the quotation marks).
The solution that I used is structured like this:
This should resemble what you have described. Again: it's all about the PACK URIs that WPF uses (see link above).

Silverlight Image element not showing up

Silverlight newbie question:
I've got a small Silverlight application with the following XAML snippet:
<Image Source="http://judahhimango.com/images/smileys/cool.gif" />
The image doesn't show up! I would expect the image to download asynchronously, then show up in the UI, but alas, no such luck.
What am I doing wrong?
Update -------------------------------------------------
Ok, no-go on the .GIFs, forgot about that. But using a PNG doens't work either:
<Image Source="http://judahhimango.com/images/smileys/cool.png" />
However, if I change the Source to a BitmapImage, then it works:
<Image>
<Image.Source>
<BitmapImage UriSource="http://judahhimango.com/images/smileys/cool.png" />
</Image.Source>
</Image>
Why can't I just specify the Image Source? Why do I have to specify a BitmapImage?
I changed two properties on my png image: Build Action to Content and Copy to Output Direct Copy always.
I think SL does not support gifs:
http://forums.silverlight.net/forums/p/3883/232781.aspx
can you try a jpg?
3/9/10: Editing per request:
I can't post complete code, but here is my MainPage.xaml
<UserControl x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<Image Source="http://judahhimango.com/images/smileys/cool.png"/>
</Grid>
</UserControl>
3/9/10: OK, I think I know what your problem may be. When you create a new silverlight application project in Visual Studio, it gives you the option of hosting it in an ASP.NET website. Did you select that option? If not, please select it, and give it a try. See this:
deep zoom is not displayed

Problem Inserting Report Viewer into WPF Application using WindowsformsHost

I am trying to create a popup from my WPF application which shows a report viewer hosted in a WindowsFormsHost
however I am having problems with the following Xaml
<Page x:Class="FIS3.ReportViewer.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="MapViewer">
<Grid>
<my:WindowsFormsHost>
</my:WindowsFormsHost>
<Label Name="exampleText" Content="this is the Report Viewer ..." />
</Grid>
I am getting a build error notifying me that
"The type 'my:WindowsFormsHost' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built"
have I made an error in my XAML
I have added WindowsFormsIntegration as a reference to my project.
Thanks for your help
Col
There is a second solution to this that people seem to have overlooked. I scratched my head on this one for a good 20 minutes. No solution, including this one, would work. Turns out the solution for me was to add:
WindowsFormsIntegration
to my references
after I realized that based on:
http://msdn.microsoft.com/en-us/library/system.windows.forms.integration.windowsformshost.aspx
that this uses the WindowsFormsIntegration.dll. It seemed to have been missing from my references.
Hope this helps!
According to the MSDN documentation, WindowsFormsHost is included in the default Xaml namespace ("http://schemas.microsoft.com/winfx/2006/xaml/presentation") - though you do need to reference WindowsFormsIntegration, as you have done. Have you tried referencing WindowsFormsHost with no namespace prefix?
<Page x:Class="FIS3.ReportViewer.ReportViewer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MapViewer">
<Grid>
<WindowsFormsHost>
<WindowsFormsHost>
<Label Name="exampleText" Content="this is the Report Viewer ..." />
</Grid>

What to do when WPF Window doesn't recognize user control defined in the same project?

I have a window "Operation" in XAML that uses a user control "Status" defined in the same project. When I build the solution, it returns ok, but when I open the design view for the Window, visual studio says "Could not create an instance of type 'Status'. The XAML for the Window "Operation" is below:
<Window x:Class="TCI.Indexer.UI.Operacao"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:tci="clr-namespace:TCI.Indexer.UI.Controles"
Title=" "
MinHeight="550"
MinWidth="675"
Loaded="Load"
ResizeMode="NoResize"
WindowStyle="None"
WindowStartupLocation="CenterScreen"
WindowState="Maximized"
Focusable="True">
<Canvas Name="canv">
<tci:Status x:Name="ucStatus"/>
<Grid Canvas.Top="0" Canvas.Left="0">
<StackPanel Orientation="Horizontal">
<!-- Indices -->
<Label Width="200"/>
</StackPanel>
</Grid>
</Canvas>
</Window>
The xmlns:tci is the namespace where the Status Usercontrol is. And tci:Status becomes underlined in blue when this error happens. How to use a UserControl like this?
I was consistently having this problem with one control in my project and wound up resolving it by changing all of my images to DynamicResource rather than StaticResource.
The really strange thing is that the designer worked just fine for the control itself and showed the images as expected. It was only when I used the control inside my main window that the designer gave me the "Could not create instance of " message.
I don't know exactly whats the solution, but that happens to me too from time to time. I end up deleting declaration of the namespace, rebuilding and trying again. :\
Try cleaning your entire project, then do a full rebuild. The WPF designer in VS is poorly done, in my opinion, and there are weird issues like this all over the place.
I recommend not relying on Design View for anything, at this point - it's just too unstable. Try Expression Blend, it's a little bit better with things like this. If you don't want to go that route, you're probably better off building and running your app :-(
If you're running VS 2008, do you have SP1 installed?
I typically see this when I haven't built the control. Make sure you build the control and see if you still see the problem. Occasionally, VS just gets confused and you need to close and open the control you are having a problem with. In this case, that would be your window.
Similar to what Jon Norton said, I also found this link (-link removed, see below-) that implicates resources. I had the situation described in the link, and Jon's fix sorted it.
EDIT
Sorry, the link now requires a login and the page doesn't exist anymore. I couldn't find what it was supposed to be after three years.

Resources