I have an application which loads dlls with user controls dynamically. Inside one of the dlls I have a control which shows a button, which, when hit, pops up a new window. Everything would be great but the window does not appear... all I get is the "Cannot locate resource XXX.xaml" message with the name of the xaml which defines the window to pop up.
The window to pop up is created in a command which is binded to the button:
private void onCmdSetIndexValidator(object _param) {
IIndex param = (IIndex)_param;
new IndexValidatorsEditor(param).Show();
}
Any ideas?
Resources in the Window need to be scoped for the assembly where they live. If you access an asset in your Window (Image, Page, etc), you need to specify the assembly it comes from (even if it comes from its own assembly). If you do not specify the assembly, WPF will assume the resource comes from the executing assembly (your client app).
Define the assembly via pack URI's. Example: /nameOfAssembly;component/folder/background.png
It also may help to look at the inner exception value of the original exception. Sometimes this provides clues as to what resource its having trouble locating.
http://msdn.microsoft.com/en-us/library/aa970069.aspx
The solution was simple... the code which loaded my dll was wrong - it used Assembly.LoadFile instead of Assembly.LoadFrom
Related
SplashScreen s = new SplashScreen(System.Reflection.Assembly.GetExecutingAssembly(),"splash.png");
s.Show(false);
when invoking s.Show() whether the parameter is false or true, it throws an IOException
with the message
Cannot locate resource 'splash.png'
even though that splash.png is added to resources and its Build Action is Resource.
I have noticed many problems when using images in WPF, also with Image control.
According to the principle "My Code doesn't work and I don't know why. My code Works and I don't know why", I have solved the problem.
First I have made the Build Action of the splash image as Resource. (Putting the image as Embedded Resource didn't solved the problem).
Second, My code is :
SplashScreen s = new SplashScreen("resources/splash.png");
s.Show(false);
/* do some things */
s.Close(Timespan.FromMilliseconds(300));
even though that splash.png is added to resources and its Build Action is Resource.
Wrong build action, make it Embedded Resource. If you still have trouble then use ildasm.exe to look at the assembly manifest for the .mresource directive to ensure that the resource got properly embedded with the expected name.
Or use a file, following these how-to steps. Generally the more sane approach since it doesn't make that much sense to have a large resource occupy address space for the life of the program when it is only needed for a short amount of time at the start of the program.
try with explicitly specifying the assembly name, even though the caller was in the same assembly as the resource.
Here's how it looks with a Pack URI syntax:
pack://application:,,,/MyAssemblyName;component/MyResourcesFolder/MyImage.png
http://msdn.microsoft.com/en-us/library/aa970069.aspx
So, I have a user control UcEx which inherits from Uc. Uc has other custom controls and forms, such as MyTree (a TreeView). MyTree and Uc have localized strings located in an assembly MyResources in the file Common.resx. UcEx is part of another form MainForm.
In this situation everything looks fine, but when I add a new MyTree to the UcEx, which already has 4 inherited objects of type MyTree from Uc, MainForm [design] throws an error:
{Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "MyResources.Common.resources" was correctly embedded or linked into assembly "MyResources" at compile time, or that all the satellite assemblies required are loadable and fully signed.}
I don't understand this since all the other MyTree-s that are already there don't cause any error.
It's not a problem of namespace or assembly name or missing a resource file, so I have no idea where to go from here...
Also when I run a debug, no exception is thrown.
EDIT1: I have already tried changing Build Action to Embedded Resource and Custom Tool as ResXFileCodeGenerator
Change the BuildAction of your ResourseFile(Common.resx) to EmbededResource and try.
First off, I should probably say that I'm probably at a grade 5 level with this stuff... I'm using a C++ add-in in a WPF application. Whenever I try to exit the program, I get this error:
Unhandled exception at 0x770d15de in Raptor.exe: 0xC0020001: The string binding is invalid.
I've been using this blog entry to try and figure the problem out, but I'm having no luck. One thing I noticed though, when I use the same C++ addin in a Console application, calling many of the same methods used in the WPF application, the Console exits without a problem.
I've also gone through the C++ code and cannot find a single static variable declared anywhere. There are static methods though.
Any help would be much appreciated!
EDIT: I enabled a number of debugging features to see where this breaks. It was breaking the sp_counted_impl.hpp file (Boost) on the last bracket of the following:
virtual void dispose() // nothrow
{
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
boost::sp_scalar_destructor_hook( px_, sizeof(X), this );
#endif
boost::checked_delete( px_ );
}
This occurs with certain DLLs that don't link with native libraries and thus their DllMain does not initialize some needed native subsystem (like CRT or ATL). Sounds like you have a mixed-mode DLL of some sort. One recommended solution is to remove the entry point from the managed DLL:
Remove the Entry Point of the Managed DLL
Link with /NOENTRY. In Solution Explorer, right-click the project
node, click Properties. In the Property Pages dialog box, click
Linker, click Command Line, and then add this switch to the
Additional Options field.
Link msvcrt.lib. In the Property Pages dialog box, click Linker,
click Input., and then add msvcrt.lib to the Additional Dependencies
property.
Remove nochkclr.obj. On the Input page (same page as in the previous step), remove nochkclr.obj from the Additional Dependencies property.
Link in the CRT. On the Input page (same page as in the previous step), add __DllMainCRTStartup#12 to the Force Symbol References property.
More detail can be found here: https://support.microsoft.com/en-us/kb/814472
I've a tiny application with several icons. I decided to put them in a separated DLL, so within my solution, I created a project and I've embedded those icons as resources.
Then I compiled this library (myImages.dll)
After, I added that dll as reference in the main program and I used the ResourceManager to get the reference of my custom library. Then I pointed to use GetObject method to retrieve the name of each file.
The following code works, but an error occurs when I try to retrieve an icon:
ResourceManager rm = new ResourceManager("myAssembly.MyNamespace", Assembly.LoadFile("Images.dll"));
Image myImage = ((System.Drawing.Image)(rm.GetObject("myIcon")));
Seems that the reference to my Images.dll doesn't work properly.
Any hints?
Usually when you get resources by name, you need to provide a fully qualified name. If you call GetManifestResourceNames, you can get a list of the names in your loaded resource manager to easily find out exactly what the names area that you must use.
I'm working on an designer-heavy application (using Visual C++ 2.0, but a C# solution should still be relevant). My setup is this:
I have a UserControl named "Host"
I'm attempting a UserControl named "Child"
Child contains a property to a class whose type is defined in a different dll entirely, named "mytools.dll"
Child works just fine in the designer. However, when I go to drag "child" onto "host" from the designer, I get the following error:
Failed to create component 'Child'. The error message follows: 'System.io.filenotfoundexception: could not load file or assembly MyTools, Version XXXXXX, Culture=neutral
.....
{unhelpful callstack}
If I comment out the property in "child" that points to the class in mytools.dll, everything designs just peachy. I have the property marked with "Browsable(false), and DesignerSerializable(hidden), and it does not help.
Is there a way for me to explicitly say "Don't load this dll, you won't need it in design time", or some way for me to force a dll to load from the designer programmatically?
Thanks!
If the property is marked 'public' then the UserControl will force the designer to load the external assembly.
You must rework your control to return Object instead of the bad type, and do typecasting if you must keep it public, otherwise, protected should be fine.
No, there's a chicken-and-egg problem here. In order to find the attributes you applied, the designer has to use Reflection to load the Type. Loading the type requires any types used by its members to be loaded as well. Which will cause the CLR to go hunting for your mytools.dll assembly.
The probing path in effect at design time is the one for Visual Studio, not your app's. What exactly that path looks like is murky to me. The toolbox plays a role as well. It makes a copy of the assembly that contains the control in a private directory. The place to look is c:\users\yourname\appdata\local\microsoft\visualstudio\9.0\projectassemblies. This often goes wrong and stray copies are left behind in that directory. I have to clean it out by hand from time to time when I notice that toolbox initialization starts to get slow.
Well, something you can check to see if a copy of mytools.dll is present there as well and isn't an old version. Putting it the GAC is one way to stay out of trouble.
UserControl has a DesignMode property, but I'm not sure if .NET wants to resolve the type beforehand (ie. return null if DesignMode == true).
Alternatively if you're the only developer you could handle the AppDomain.CurrentDomain.AssemblyResolve event and load a dummy assembly yourself.