Class Library not including images - c#

I have a class library that is essentially a collection of forms to be run. Consider it a module/plugin in a larger program, that can be developed independently, all the larger program cares about is the DLL (and interface).
Running the main form of the class library is fine and works well. My issue is with pictures. I've set up an Images folder in the class library, added an image, set it's Build Action to Embedded Resource and then rebuilt the project, but the images won't appear in the main program.
XAML:
<Button x:Name="btnAdd" Command="{Binding Add}">
<StackPanel Orientation="Horizontal">
<Image x:Name="imgAdd" Source="Resources/Add.png"/>
<Label>New</Label>
</StackPanel>
</Button>
The interesting part though, is that if I create a BitmapSource in code-behind and assign it to imgAdd in the constructor of the form, it works as expected. Does anyone have any ideas as to why this might be the case?

Use Pack URIs for your images.
<Button x:Name="btnAdd" Command="{Binding Add}">
<StackPanel Orientation="Horizontal">
<Image x:Name="imgAdd" Source="pack://application:,,,/ReferencedAssembly;component/Resources/Add.png"/>
<Label>New</Label>
</StackPanel>
</Button>

It turns out that the correct Build Action is actually Resource rather than Embedded Resource. Thinking about it now, Embedded Resource does seem more like a reference to a Resource in another DLL.
I inadvertently found the answer in this post while trying to improve my code.

Related

Button not showing up in run as in preview

so i have a button whos XAML is as follows:
<Grid>
<!--MoreCode -->
<Button x:Name="Camera2" Grid.Column="2" Grid.Row="7">
<Grid>
<Image Source="images\tabNormal.png"></Image>
<Image Source="images\OfflineRed_21x76.png" Height="18"
VerticalAlignment="Bottom" Margin="0,0,0,5"></Image>
<TextBlock HorizontalAlignment="Center" Text="Camera 2"
VerticalAlignment="Center"/>
</Grid>
</Button>
<!--MoreCode -->
</Grid>
and it Previews ok.
But when i Run the project I get this:
In the end I will end up making this button a standard control, but i want to understand why it does not seem to build right when I run.
in Closing, the problem seemed to be something with the compiler not referencing the images properly (perhaps because I added the images folder to the project after building everything?), the solution was to add the full path to the images i.e. C:\...folders...\images\filename.png as opposed to the initial images\filename.png once this was done once for one file all files in that folder are readable and you can return to a relitive location. Everything seems to be working fine now even after deleting and re adding the files/folder

Images in WPF application disappear after navigating away

I'm experiencing an interesting phenomenon in my WPF application.
I have two separate views affected - one that allows editing of statistics and one that allows editing of templates. On both of these views I have a navigation bar that on allows the user to navigate forward and backward through search results and I use buttons with an image for the Back/Next UI. If I go navigate through statistics, and then through templates, the images show up fine; however, if I go back to statistics, the images no longer appear, but the buttons and navigation works. If I go back to templates, the images are still there. If I do templates first, then statistics and back to templates, the same order of behavior persists - statistics keeps the images. So it seems to do with the order, and not the views.
I have the images added to my project and have Build Action set to Resource. I have them referenced in my ResourceDictionary like so:
<Image x:Key="ico_Right" Source="/GOKOMS.Home;component/Images/nav_single_right.png" />
Within my View itself, I reference as
<Button Content="{StaticResource ico_Right}" Width="35" Height="35"
Visibility="{Binding Vis_Next, UpdateSourceTrigger=PropertyChanged}"
ToolTip="Next Record"
Command="{Binding NavCommand}"
CommandParameter="Next"
/>
I know it's not the binding as the button itself appears; I've even removed that just to be certain. It feels like this is the pertinent code, but I can add more if needed. Has anyone experienced something like this before?
This is because your Image control resource can only be attached to one place in the visual tree and you are trying to attach it to 2 different button elements (on different views)
You could try restructuring your views so that there is only one instance of the navigation menu, or you could create multiple image resources, or you could use a BitmapImage resource instead:
Resource:
<BitmapImage x:Key="ico_Right" UriSource="/GOKOMS.Home;component/Images/nav_single_right.png" />
Button:
<Button Width="35" Height="35"
Visibility="{Binding Vis_Next, UpdateSourceTrigger=PropertyChanged}"
ToolTip="Next Record"
Command="{Binding NavCommand}"
CommandParameter="Next">
<Image Source="{StaticResource ico_Right}" />
</Button>

Different ways to define resources in a WPF app

Fairly new (couple of days) to WPF development and C#. Looking for a high-level philosophical difference between two ways of defining and accessing application resources from XAML:
I can define resources in a .resx file in the visual editor and then read them in a XAML file like <TextBox Text="{x:Static p:Resources.name}"> (provided the "p" namespace points to the application Properties).
Or I can define resources anywhere in my control hierarchy like, say, <sys:String x:Key="name">Name</sys:String> and then reference them as <TextBox Text="{StaticResource name}">
When and why would I want to prefer one over the other?
The resources of this type:
<TextBox Text="{x:Static p:Resources.name}">
Usually used instead of the system registry, that is, there store some metadata applications that can change the user, such as background color, some settings, etc.
Example
Binding default value from XAML:
xmlns:properties="clr-namespace:WorkWithSettings.Properties"
<Button Width="100" Height="30"
Background="{Binding Source={x:Static properties:Settings.Default}, Path=Setting, Mode=TwoWay}" />
Set value from code:
private void Button_Click(object sender, RoutedEventArgs e)
{
WorkWithSettings.Properties.Settings.Default.Setting = "#FF007CE4";
}
As mentioned in the comments #gomi42, this type of resource is used to localize the application. Here:
WPF Localization for Dummies
Sample for globalization and localization in WPF
You can see an example of this implementation.
The resources of this type:
<sys:String x:Key="name">Name</sys:String>
Are constants and are meant they do not change throughout the life of the application (not including DynamicResource). I keep it there window sizes that do not change, and other strings of window headers.

WPF button with image doesn't appear

I'm using the following code I found on a question a few hours ago to make a button have an image:
<Button Name="bPlay" Height="70" Width="70" Margin="359,480,349,11">
<Button.Template>
<ControlTemplate>
<Border HorizontalAlignment="Center" VerticalAlignment="Center">
<Image Source="pack://siteoforigin:,,,/Resources/play.bmp" Width="70" Height="70" />
</Border>
</ControlTemplate>
</Button.Template>
</Button>
The problem is that for some reason it looks fine on Visual Studio, but when I run the program those buttons won't appear. I can't find the problem and I'm a little bit stuck. The image play.bmp is added to resources obviously, but still I don't know what the problem is, thanks!
"siteoforigin" in the URI indicates the file must be in the given path relative to the directory of the executing assembly. Your executable file is most likely in a bin/Debug folder. It's looking in the subdirectory of the executable, which probably doesn't exist ("bin/Debug/Resources/play.bmp").
If you want to link to the file that way then you have to tell Visual Studio to copy it to the output folder (from the Properties pane). Or copy it yourself.
Or better yet you should link to it as a resource and it will get embedded in the application. Set the file's build type to Resource in the properties pane, and link to it using the relative path in your project folder. In this case, literally write "Resources/play.bmp" in the XAML.
Try simplifying your code, perhaps just pull the image out and see if that displays.
<Button Name="bPlay" Height="70" Width="70" Margin="359,480,349,11">
<Image Source="pack://siteoforigin:,,,/Resources/play.bmp" Width="70" Height="70" />
</Button>
should be valid as well

Image Control asynchronous

I have an image control that I want it to load the image asynchronously over the network. I see some sample code in the network saying something like:
<Image HorizontalAlignment="Left" Height="118" Margin="2,8,0,0" VerticalAlignment="Top" Width="167" x:Name="ImageThumbnail" Source="{Binding SummaryImageLink, IsAsync=True}" />
However, in Windows Phone 7, I cannot find anything like that. Does anybody know a way to do that without I have to code a lot myself
Unfortunately you are going to have to write, or include, more code yourself to do this properly. I have a similar solution with the goal to cache images after the first download. The image cache class is accessed via a IValueConverter, so once you have included the appropriate code, you only have to add a decorator on the Image object:
<Image Source="{Binding ElementName=ImageSource, Path=Text, Converter={StaticResource imageCacheConverter}}" Width="200" />
Full details here and source code here: http://www.ben.geek.nz/2010/07/one-time-cached-images-in-windows-phone-7/
David Anson's work with LowProfileImageLoader may be of interest to you.
Keep a low profile [LowProfileImageLoader helps the Windows Phone 7 UI thread stay responsive by loading images in the background] - Delay's Blog
Alternatively you can asynchronously download the picture (for ex. with a BackgroundWorker) and assign it as source for your image control only when the download has completed.

Categories