External alias in XAML

天涯浪子 提交于 2019-12-02 11:11:13

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3815492/external-alias-in-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!