External alias in XAML - c#

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.

Related

Enum Intellisense Display Attribute?

I want to do this:
enum Foo
{
[Display="Item One"]
ItemOne,
}
So that Intellisense will display it like in the attribute instead of the actual name.
I know it's possible, I've seen it before.
Well you could provide XML documentation:
enum Foo
{
/// <summary>Item One</summary>
ItemOne
}
I'm not sure whether that's quite what you were thinking of, but here's an example of what it looks like in VS 2010:
Note that I'm assuming you mean from the code editor... if you mean within a property editor, that could be something entirely different, e.g. DisplayNameAttribute (although that's meant for properties, events or methods).
If you know an example of what you want within the framework, we may be able to help more.
As a note... if you are building a .dll that is to be referenced by another application, just writing a summary will not allow the text to show up in intellisense for the referencing application. To accomplish this, you must deploy the XML documentation file as well, which requires a re-compiled version of the same .dll.
To do this (in VS2008 anyways), go into the Properties of your project, click the Build tab, click the checkbox at the bottom next to 'XML documentation file:', rebuild the application, and now you have the files needed to make it work.

ASP.net c#, how do I know what to use?

I keep coming accross code samples online for ASP.net c#, however they never seem to list which namespaces they include, for example:
using System.Data.SqlClient;
etc etc
Am I missing something obvious or should I be expected to know exactly what namespaces each code example requires?
When I'm in that situation, typically I search for the class on MSDN. The documentation will tell you which namespaces contain the class.
If they don't include them, you can follow this list in order:
Find that they are in one of the namespaces listed in the "blank code file" template , or
In Visual Studio You can click the missing type and press shift+F10 or Ctrl+. To get the option to automatically add the using statement (if the assembly is referenced)
With Resharper, Select the type and hit alt+enter for Resharper to find the namespace for you, and add it to the usings (possibly even reference the assembly as well)
Go to MSDN and search the name.
Go to Google and search the name (honestly, I normally do this before hitting MSDN anyway)
Compain to the article author
If code samples use the assemblies that a project references by default, then you can hover on the class name and click shift+F10 which will add the using statement automatically. If the class is not in any of the referenced assemblies then you are out of luck and need to know in what assembly does the class resides.
A quick google search can help, and in time you will memorize the namespaces... Of course its best if samples included the namespace and reference info, but mostly they do not.
If you are viewing code in Visual studio, just hover mouse over class or object you want and you will get tool tip about it if assemly of that class is present or you can google for particular class.For example if you want to know more about 'DataTable'class, just google it and you will come to know that its part of Syste.Data namespace.
I'm with the OP on this one. Having to just magically "know" what namespaces are required seems supremely silly.
I spent some time before C# as a Java Developer, and the NetBeans IDE will resolve these for you automatically. Ctrl-Shift-I, and it will insert all the packages (ie, namespaces) you need. If more than one package defines the class you are resolving, a window pops up and lets you choose which one you want.
For as fine a product as VS is, I am incredulous that this feature is not included.

.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.

Dynamically loading code-behind along with XAML?

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.

Categories