IlMerge Silverlight Class Library with Custom Controls - silverlight

I am trying to merge all the assemblies of an class library in a single .dll file.
I can merge all the assemblies using the Ilmerge but is that when I use the merged dll in a Silverlight application I am having trouble when a template is apply to my control and binding problems if I use a control that inherits with UserControl.
is there any solution to solve this problem?

The problem is that when the initial dlls are built the Xaml in the project is added as a resource in the dll. The code generated to load this xaml will look something like this:-
System.Windows.Application.LoadComponent(this, new System.Uri("/SilverlightLibrary1;component/MyControl.xaml", System.UriKind.Relative));
Note how the name of the dll forms part of the Uri need to fetch the xaml. I doubt IlMerge is able to spot that and fix it up. Hence once merged the Uris cannot be found.
A resolution for this is probably uglier than multiple references or simply creating another project that links all the code files involved.

Related

Loading external XAML file into a WPF project? The file is our "styles"

Has anyone had experience, or is it even possible to load an external XAML file into a WPF project from a hosted website.
We are wondering because we are defining the XAML file as our "styles". We would like a person not familiar with XAML to edit the file and then we don't have to redeploy the whole application, but next time the application loads it will just reference the changed XAML file.
Or is this not possible because the XAML files are compiled into the project?
Or would an option be to load an external XML file in code behind and populate our "style" properties that way? Is this possible?
We currently are using ResourceDictionary and calling an internal XAML file in the application, but we would like a more dynamic solution.
Using XamlReader.Load you can use the XAML parser. There is a performance hit from parsing XAML instead of BAML, and downloading a file from the network could negatively impact the (in my experience) often already slow startup times for WPF applications. The blog entry below provides a nice explanation of dynamically loading resource dictionaries.
http://blogs.interknowlogy.com/2011/09/02/xamlreader-looseresourcedictionaryfiles-parsercontext/

Generate dll file based on xaml and cs files

I have an application in WPF and I would do the following: In a folder I have a file .xaml that contains the screen, and a file .cs that contains the behavior of that screen. I need to generate a single .dll file from these two files (xaml an cs) and be able to access this .dll file within my application, so that this screen would make part of my application. I have no idea how to do this. Could anyone help me???
Thank's
Have you looked into Prism? It is useful for the scenario you describe, as long as you aren't trying to compile the screen at runtime. If that is what you're trying to do, I don't think you can associate a class with loose XAML.
If you have a ViewModel, and a View without code-behind, then you could potentially do this during the lifetime of the application. However, I think it will be far easier to compile these files, and have Prism load them as a module.

VS2012 - Add WPF existing user control to project

This seems like it should be pretty simple but I can't seem to make it happen. Lets say I have an existing project with a user control named uc1. I would like to use this user control in another project. I right-click the project name in the solution explorer and select add>existing item, change the drop down to all files and select the files uc1.xaml and uc1.xaml.vb. This of course adds the files to the project but there is no correlation between the xaml and the code behind file and there is no way to use the control. What is the proper way of doing this?
Reed's answer is a good architectural one. If you plan on creating a control that you will reuse in many projects then it's best to use a control library.
Your original question is valid in some situations though. Say you have some source code from the Internet that you've unzipped to your drive. This project contains a .XAML file and its linked .vb file that you want to add to a project.
As you seen, the Visual Studio Solution Explorer doesn't link the files when adding with the "Add Item" dialog. I think this is a bug. I find that if I reload the project, the affiliation is added.
Here's a workaround I use. I drag the files from Windows Explorer /File Explorer onto the project in Solution Explorer. That works correctly the first time.
This of course adds the files to the project but there is no correlation between the xaml and the code behind file and there is no way to use the control. What is the proper way of doing this?
Normally, you'd add a reference to the other project, and use the UserControl directly.
This allows you to build a single project with your UserControl, and use the resulting assembly (DLL) in multiple projects without duplicating the code.
If you want to reuse your user controls you need to create a new project and choose "Class Library" from the list of available projects. When compiled this class library can easily be used by any number of other projects and solutions simply by adding a reference to compiled DLL created when you build this class library.
Edit: As mentioned in other answer it's "WPF UserControl Library", not simple "Class Library"...
You just need to add the .xaml file and VS should auto add the code behind(nested). I've seen this not work a few times and as #Walt Ritscher said this is probably a bug.
I found simply restarting Visual Studio and reloading my solution worked.

How to assign resource dictionary for custom controls in a dll

I have a WPF custom control MyControl.cs in my application project (.exe) with its style in a resource dictionary in MyControlResources.xaml. This xaml is specified in app.xaml as a part of the merged dictionaries. Everything works fine.
Now I want to move this custom control into an existing DLL project that the application references. Is there a way that I can create the resource dictionary "assignment" in the DLL and make it transparent to the callers i.e. the application project can use it like any built-in control that doesn't require you to know anything about resource dictionary?
I've read about creating a new custom control project can do the trick, but it's only one control for which I don't want to create a new project for. Anyone knows how to do it in an existing class library DLL?
This answer helped me find the trick.
Create a file called Themes\Generic.xaml to merge the resource dictionary.
Add the following attribute to Assembly.cs
[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]

Resource Dictionaries in a Silverlight Assembly?

I've just begun dabbling in putting together a set of controls as assemblies and I'm working on default styling. What I currently have is a UserControl in a project (thanks Reed!) and I'm able to bring that into another project via reference. I plan to add more controls over time to build something of an SDK.
I currently have some hooks that look for resources in the hosting application which either apply the resources to their respective properties, or style out the control via hard coded defaults.
Is it possible to set up resource dictionaries within the project containing the UserControls so they can use those references as the default, instead of hard coding? If so, how do I target them?
(I have a ResourceDictionary set up within the same project as the controls: Resources>Dictionaries>Colors.xaml)
Thanks in advance!
E
You should really look at creating custom templated controls in library rather than derivatives of UserControls. This will allow projects that reference your library to specify an alternative default style for you controls in the same way as we can for the controls in Microsofts own SDK.
The Creating a New Control by Creating a ControlTemplate topic on MSDN is good starter.
I think this is a better explanation, but i'm trying on a desktop application and i got the same problem.
XamlParseException: Failed to create a 'System.Type' from the text 'local:CustomerEntity'
If I'm undestanding correctly you want to create the file "generic.xaml" in the folder "Themes". However, I don't believe automatic styling works with UserControl only with Control. Generally if you trying to make a control that can be stylized and retemplated you want to inherit from Control and not UserControl.

Resources