I want a Listbox that lists all the images I have as resources in my app, then on selection changed the Image Source in my app is changed.
Need to:
Enumerate all the image files,
Load them into an imagesource
In WPF, I can just System.IO to grab the files from a directory, but I can't do this in silverlight.
how do I do this?
Don't think you can do that for files set as Resource or Content. It is possible for Embedded Resource (although Embedded Resource is not recommended for Silverlight. Don't think that an Image control can access them directly). See related discussion It suggests creating such a list during build time using T4
Related
Has anyone had experience, or is it even possible to load an external XAML file into a WPF project from a hosted website.
We are wondering because we are defining the XAML file as our "styles". We would like a person not familiar with XAML to edit the file and then we don't have to redeploy the whole application, but next time the application loads it will just reference the changed XAML file.
Or is this not possible because the XAML files are compiled into the project?
Or would an option be to load an external XML file in code behind and populate our "style" properties that way? Is this possible?
We currently are using ResourceDictionary and calling an internal XAML file in the application, but we would like a more dynamic solution.
Using XamlReader.Load you can use the XAML parser. There is a performance hit from parsing XAML instead of BAML, and downloading a file from the network could negatively impact the (in my experience) often already slow startup times for WPF applications. The blog entry below provides a nice explanation of dynamically loading resource dictionaries.
http://blogs.interknowlogy.com/2011/09/02/xamlreader-looseresourcedictionaryfiles-parsercontext/
I'm working on a modular Silverlight application and in one of the modules I'd like to display an image on a view. I've added the image file under the module project in the /Resources/Images directory. And than I've changed the image file's Build Action property to Content. I think it's nicer not to embed the image into the dll file. The xap file after build contains the image.
In the View xaml I've inserted the image <Image Source="/Resources/Images/Yoda.jpg" /> and design time it displays correctly but runtime the image is missing.
My question is how to solve this problem? I don't want to embed in the dll (Build Action: Resource).
I've made a small test solution if you want to play with it:
Solution.zip
Thanks in advance
Design time the image displayed correctly:
But runtime the image is missing:
Solution structure:
You could set the Copy to Output Directory property of the image to either:
Copy always, or
Copy if newer
Then at runtime it will be available.
I'm creating a windows forms application. I have all of my resource strings (resx files) in its own project within the solution. Is it possible to reference those resource files at design time?
Coming from web (ASP.Net MVC) we were able to add a reference in the view to the resource file(s) we needed. Am I able to do something similar and set the labels at design time?
Not unless you are prepared to go down the route of using Localizable forms - see WinForms strings in resource files, wired up in designer
My motivation for this question is really just to specify an image to be used in a user control via a dependency property for ImageSource. I'm hitting some pain points involving the management, access, and unit testing for this.
Is the resource editor a good tool to use to maintain images for the application?
What is the best way to translate the Bitmap from the editor to an ImageSource?
How can I grab the resource Filename from the editor?
No, the resource editor is not a good tool for this.
In a WPF application the best way is to put all of your images in an "Images" directory and mark each one as a "Resource". Then you can reference them directly in Image controls and elsewhere.
Here are the precise steps:
Crop and otherwise adjust your images using your favorite bitmap editing program (Paint.NET, Photoshop, etc)
Save them as .png files (or .jpg or .gif if you prefer)
Create an "Images" folder inside your Visual Studio solution (or multiple folders, however you want to organize it)
Drag the images from your hard disk into your "Images" folder (or right-click the project, select New -> Existing Item and select the images)
Now you can reference your images easily in XAML:
<Image Source="Images/MyImage.png" />
Or in code:
var source = (BitmapSource)Application.LoadComponent(
new Uri("Images/MyImage.png", UriKind.Relative));
You can also reference images in external assemblies:
<Image Source="ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png" />
Which in code would be:
var source = (BitmapSource)Application.LoadComponent(
new Uri("ReferencedAssembly;v1.0.0.1;component/Images/MyImage.png",
UriKind.Relative));
I need to put an image in the default view of a custom control. However, whenever I try to test the control it can't locate the image. I have tried to compile it as an embedded resource and just a plain resource in VS. Neither of these have worked. So is there a correct way to do this?
That's probably because you specified the image path as a relative path. You should use the Pack URI Scheme to specify that the resource is in the current assembly. For instance :
<Image Source="pack://application:,,,/Images/MyImage.png"/>
I have an open-source library that allows you to include country flags in your WPF application via a value converter. The flags images are stored as resources within the assembly.
It's available on NuGet:
Install-Package FamFamFam.Flags.Wpf
The source is up on GitHub:
https://github.com/drewnoakes/famfamfam-flags-wpf
You can take a look to see how the images are embedded and the Pack URI scheme is used.