Adding a user control causes form to throw resources error - c#

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.

Related

Frame.Navigate to a XAML Page in different assembly [WinRT]

I have these files.
"Control.cs" [this has method activateabc()] and "abc.xaml" in an assembly (created as a Class Library) and I have referred it in my working project(Windows Phone 8.1 Runtime App). I have added
Frame.Navigate(typeof(abc)) within the activateabc() and when I call activateabc() from the working project it throws XAMLPARSEEXCEPTION.
Any idea how do I navigate to XAML page in another assembly? Also I have no dots or hypens or underscores in any of my assembly names.
Edit: NavigationService.Naivage()is available in Windows Phone 8 to do the job. However, Frame.Navigate() allows navigation to a type rather than to a URI as in WP8. So please tell me a way how to navigate to a XAML page in different assembly in Windows Phone 8.1 Runtime Apps
Can you provide more information please? I don't have an answer but I do have some additional information that might help.
Do you know if it's failing to parse the xaml markup or is it in truth failing to find the abc.xaml file? I bet this is the real problem. If it's failing to find the file you might need to modify the xaml file Uri to include the assembly name. I don't know how to override the default file load location for pages since that code gets generated but the Uri would look like this.
If your assembly is MyCustomControls.dll then the Uri would be new Uri("ms-appx:///MyCustomControls/abc.xaml").
If your xaml file is found but some markup is failing you might try to comment out all the markup until there is no load failure then comment it back in by bits until you isolate which bit of markup seems to be the problem. It might be some namespace resolution issue in the app's xamltypeinfo.g.cs. I doubt this is the issue though since you say you added the assembly as a reference.
I hope some of the above information is useful.

C# load external dll with embedded images

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.

"Cannot locate resource" again

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

How can I reuse .NET resources in multiple executables?

I have an existing application that I'm supposed to take and create a "mini" version of. Both are localized apps and we would like to reuse the resources in the main application as is. So, here's the basic structure of my apps:
MainApplication.csproj
/Properties/Resources.resx
/MainUserControl.xaml (uses strings in Properties/Resources.resx)
/MainUserControl.xaml.cs
MiniApplication.csproj
link to MainApplication/Properties/Resources.resx
link to MainApplication/MainUserControl.xaml
link to MainApplication/MainUserControl.xaml.cs
MiniApplication.xaml (tries to use MainUserControl from MainApplication link)
So, in summary, I've added the needed resources and user control from the main application as links in the mini application. However, when I run the mini application, I get the following exception. I'm guessing it's having trouble with the different namespaces, but how do I fix?
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
\"MainApplication.Properties.Resources.resources\"
was correctly embedded or linked into
assembly \"MiniApplication\" at
compile time, or that all the
satellite assemblies required are
loadable and fully signed.
FYI, I know I could put the user control in a user control library but the problem is that the mini application needs to have a small footprint. So, we only want to include what we need.
You are correct that the different namespaces are the problem. A resource file cannot be given a namespace, it will take the namespace of the folder that contains it. If the namespaces across your two apps are different, the namespace will be different.
I can see three options available to you
Use the same default namespace for both applications
Have an assembly purely for your resource file and reference that in both apps
In the code loading the resource, generate the namespace based on the namespace of the class that's loading it
The Resources generated by visual studio are not public, they are only visible to classes within a library. in order to generate resources as public properties you will need to use a custom generator like this: http://www.codeproject.com/kb/dotnet/ResXFileCodeGeneratorEx.aspx

.Net Designer assemblies, C++\C# error

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.

Categories

Resources