C
C#2y ago
Lep

❔ ? WPF Build Action(Resource)

I have the following: string Source = "C\\Images\\This.gif"; how can I include this to the resources automatically rather than manually?
<ItemGroup>
<Resource Include="Images\This.gif" />
</ItemGroup>
<ItemGroup>
<Resource Include="Images\This.gif" />
</ItemGroup>
19 Replies
Klarth
Klarth2y ago
To be clear, you mean to automatically include files as a Resource specifically (which gets embedded into the executable), not the string itself? Anyways, with Avalonia, you can do stuff like:
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
<AvaloniaResource Remove="Assets\DemoImages\**" />
<AvaloniaResource Remove="Assets\tslogo.svg" />
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
<AvaloniaResource Remove="Assets\DemoImages\**" />
<AvaloniaResource Remove="Assets\tslogo.svg" />
<None Remove=".gitignore" />
</ItemGroup>
So you can probably do the same with <Resource>
Klarth
Klarth2y ago
Just put them into your project like that.
Lep
LepOP2y ago
I'd like to change the Source to be selected from a ui so I don't want to put it in a specific folder The source location will change whenever I decide to open it from somewhere else
Klarth
Klarth2y ago
Ok, so then you don't want this to be anywhere near your csproj or a ResourceDictionary. Your choices are either 1. Create a BitmapImage from the string in code-behind. Then assign it to the Bitmap.Source. 2. Bind a string to Bitmap.Source and let the WPF internals take care of it. This is what you would do in MVVM.
Lep
LepOP2y ago
Seems like I need it on the resources else it wont work. I'm currently using the WpfAnimatedGif nuget packet to be able to use gifs
Klarth
Klarth2y ago
What is the actual code that's causing the exception? I just see a string.
Lep
LepOP2y ago
Klarth
Klarth2y ago
UriSource is not pulling from your Source string? Where is thegif defined? Sorry I'm misreading. Yeah, so I would suggest using only absolute URIs when you're letting people load images from anywhere on the file system. If you're including external files with your build to load by default, say in an Images folder, then build an absolute Uri from the relative one. Or just define Source as a Uri.
Uri _defaultSource = new Uri("Images\\6.gif", UriKind.Relative);
Uri Source = _defaultSource;
Uri _defaultSource = new Uri("Images\\6.gif", UriKind.Relative);
Uri Source = _defaultSource;
Then you assign whatever Uri you want to Source. Pass it through Gif (change it to take a Uri too).
Lep
LepOP2y ago
Put everything together in 1 place for now, am I doing something wrong? Sorry still kind of learning but, if I do add the resource it works fine, if I don't I still get the exception thrown out.
Lep
LepOP2y ago
To make it less of a mess to read:
Klarth
Klarth2y ago
You have to add the image if you're shipping it with the app. Granted, in this setup, you would possibly do "Copy to Output" and ship the folder instead of embedding it in the executable. If you're not shipping it with the app, then you should not bother...because the image is not going to be there when the app is deployed on another computer.
Lep
LepOP2y ago
This is a personal learning project, not making an actual app for a while. Just trying to learn how to do things correctly at the moment. I could just throw all the images into a single folder and get it from there, but I'd also like the possibility to grab a gif from 1 place and use it. I do have the Images*.gif option set on the resources area, so what I could also do is make a temp gif on the file directory or add a copy. Any opinions on the matter?
Klarth
Klarth2y ago
It's a pain to mix resources and file system stuff, IMO. If I ship a lot of resources that are intended to be user customizable, I will often ship an extra folder. eg. _images. I will define a "default" image to open (usually via config file) with a fallback. The user can then change the image if they want and the folder browser will default to the _images folder, but they can browse anywhere. Whether you reference the image or make a copy into _images is up to you.
Klarth
Klarth2y ago
As for how I include them in my project...I create folders directly under the project.
Klarth
Klarth2y ago
Then copy to output so it follows to your build output.
Klarth
Klarth2y ago
You can embed it as a Resource into the executable, but then that means you need to keep track of embedded resources, keep track of user-defined objects, merge them in your views, etc and it is a pain. The extra overhead might be necessary if you work in an environment where a user deleting something could cost thousands of dollars or more. Not worth it for a hobby app. Basically: Design so that you can treat everything the same and you'll have an easier time.
Lep
LepOP2y ago
Thank you for the info, I'll be looking into user directed configurations when I get a chance, as for this project, looks like I need the resource so I might make a copy into the file directory since I don't really understand/know how to reference to the original file without needing it to be in the resources nor do I know how to add a new path to the resources through actual code rather than manual entry.
Klarth
Klarth2y ago
Give me a bit of time to mock it up. Ok, relative uris seem to stink here. Maybe I'm misremembering or maybe they're just not good in WPF. (I switched to Avalonia awhile back) Clone this repo https://github.com/stevemonaco/WpfImageTest And this is roughly how I would get started if I were to do it with code-behind. That said, I use MVVM. You could also embed the default image into the executable if you wanted to and still have files from the file system. The key here would be to use Uris for everything. In particular, you would want to use Pack Uris for the embedded resources: https://github.com/dotnet/docs-desktop/blob/main/dotnet-desktop-guide/framework/wpf/app-development/pack-uris-in-wpf.md There are relative pack Uris with slightly simpler syntax. Maybe you'd have success there. 🤷‍♂️
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server