-2

During runtime I import some assets (including a html file) and I copy it to a given directory in my project. Just found out that the reason the file doesn't show up is because the files are not included in the project, even if they are there. Question is, how to include some freshly added assets during runtime?

Edit

By "not show up" I mean the assets are like they are not there, so the program doesn't see them. However, I see the copy was successful because after closing the program and refreshing the solution explorer, I have the files with a dashed line.

agiro
  • 2,018
  • 2
  • 30
  • 62

2 Answers2

-1

Here is some XAML for an asset which will be decided at runtime:

<Image Source="{Binding Converter={StaticResource SomeImageSourceConverter}}"
         x:Name="SomeImage"/>

And this is the code-behind:

SomeImage.DataContext = "pack://application:,,,/.../SomeImagePlaceAtRuntime.png";

public class SomeImageSourceConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
    object parameter_, System.Globalization.CultureInfo culture_)
    {
        return (new ImageSourceConverter()).ConvertFromString(value.ToString());
    }

    public object ConvertBack(object value, Type targetType,
    object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

So depending on the value of DataContext, it will load different images.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
-1

I think what you're describing sounds like a similar problem I had with a solution a few weeks ago.

If you've referenced the file paths relative to the directory the program is executing from then try selecting all the files that aren't showing and in the properties panel of VS set "Copy To Output Directory" to "Copy Always"

Source: The accepted answer to Load local HTML file in a C# WebBrowser

Community
  • 1
  • 1