Dynamically loading code-behind along with XAML? - c#

Related to this question I asked earlier, I wonder if it's possible to also dynamically load a code-behind file that is paired with a XAML file. Can it work this way or would it just be better to compile both into a DLL?.
Thanks!

Actually, it's the code-behind that loads the XAML file. The designer generates a hidden file that binds all of your named elements and events to the class. (Notice the 'partial' keyword on your class in the code-behind.) It functions similar to the file that the WinForms designer generates, only it's a bit harder to find. You can find them in the "obj/debug/" folder along with the compiled BAML.
As for actually answering your quetion, it would be better to compile them to a DLL. It may not be impossible to set up a library that can connect a XAML to a special class that has methods to dynamically access elements, but there's nothing like that now as far as I know.

Related

XAML markup extensions design time

https://xamlmarkupextensions.codeplex.com/
I'm having a problem using the xamlmarkupextensions library. This library provides the opportunity to Bind values from a resource file (resx) to different controls. Now this works perfectly, but I don't want to use the resource file... I would like to use my own database.
So I made a new database and tried some stuff and got it working. The only problem is that the value is not visible in design time... Does anybody here knows how this could work? Thanks in advance!

External alias in XAML

I'm currently using a library that implements Menus and ContextMenus for Silverlight 3 and 4. This library defines a MenuItem class in the System.Windows.Controls namespace.
No problems with SL3 because there is no MenuItem class elsewhere in the Silverlight class library; but now I need to use another control in a Silverlight 4 Toolkit assembly and the toolkit now defines a System.Windows.Controls.MenuItem in this same assembly !
So I need a way to indicate to the compiler that I want to use the System.Windows.Controls.MenuItem from my old assembly and not the one in the toolkit 4 assembly.
The solution seems the "external aliases" features.
I can tweak the files I write myself with external aliases but how to indicate to the code generator, the one that generates ".g.i.cs" files from XAML, wich assembly, more exactly which aliases, to use ?
By default it always generates System.Windows.Controls.MenuItem variables in the ".g.i.cs" files, and of course without aliases the C# compiler is unable to know which assembly to use.
I'm using VS 2010 Professional but I haven't been able to find an option to change this behaviour.
Thanks in advance.
I'm afraid the only way around this is to rip the contents of the .g.i.cs file and move it your .cs file, tweak it up with your aliases, remove the partial keyword and then remove the x:Class from the Xaml.
Upside is the designer will still work. The downside is you need create any new control fields yourself and add the FindName code to the copy of InitializeComponent you now have in your .cs. Personally I quite like this, there are plenty of reasons to give an element a name other than it needing to be a field in the class (binding and animation being two of them). Its annoying that fields are automatically created and precious load time devoted to finding and assigning when they're never used.
Finally I've found a workaround :
I've created a library project that wraps the types from the menus library.
For instance :
namespace Alias
{
public class MenuItem : System.Windows.Controls.MenuItem
{
}
}
I then reference this project from my real project and can use the type through their "new" namespace "Alias".
It's a kind of "heavy alias" but seems to work.

How to re-use ASP.NET .aspx or .ascx files?

I know if someone wants to re-use some classes (not UI), he must gather all of them and put in a Visual Studio Class Library, build it to some dells and distribute these dlls. In this approach there just one code, you just update code in one place.
But what about ASP.NET's markups? For example you have an .ascx file or a collection of .aspx files regarding user management. If I want to use them in another project I am forced to copy them in new project again. By this I have two same code that is very hard to maintain.
So is it any way to re-use .ascx and .aspx files just like simple .dlls? For example building them?
Many Thanks,
Afshar Mohebbi
With the default configuration, .ascx and .aspx files will need to exist on disk, because they need to have a path associated with them for everything to work. All the code (everything but the first line which specifies which class to inherit) in them, however, can be compiled away into a DLL file. It would probably be possible to get around this by writing custom handlers and build providers that load things from DLLs, but it's not worth the effort.
If you want to put your user controls into a DLL file, create them as custom controls instead of user controls (.ascx files). That's how all the custom control libraries for sale around the 'net are done.

.NET Custom Control Resources.resx

I have a C# custom control that loads images from Resources.resx. I was loading this resources into the Project's Resources and then accessing them like:
ProjectNamespace.Properties.Resources.resourcename;
This works for one project but now I want to use my control in multiple projects.
What's the best way to handle this? Load the resources into the controls .resx? How can I access them from there? Or should I approach this completely differently?
It should work as is, even if your control is used from other projects.
The code generated by VS is a wrapper around the ResourceManager class, and it gives the assembly of your control as a constructor parameter. So, the ResourceManager always knows where to look for resources.

How do I serialize a .NET control to CODE?

I want to create a .NET Form at runtime, add buttons and other controls to that (also at runtime), and then be able to generate a something.designer.cs file from that form (which can then be added to a C# solution and compiled).
What I want to do is very similar to what the WinForm designer does. But instead of having a drag/drop interface for the user, I want to dynamically build the Form/Controls myself at runtime.
I was thinking I could just reuse what the WinForm designer is doing.
Is that possible?
This MSDN magazine article should have everything you need.
It's really not as simple as it was pre-.NET as the visual version of the form you see in Visual Studio is actually the result of multiple files.
But in the simplest form you could simply just mirror what .NET does at the start of creating a new form:
Create three files Form.cs, Form.Designer.cs and Form.resx (which is an XML file).
Place the same default content in them that VS does
Mimic the code generated when adding controls, code-behind and resources
It will be a tedious task, but it can be done. Adding resources however will be burdensome.
Yes, you can do achieve this using Compiler Services (compiling c# code) or Emit class if you know building correct MSIL.

Categories