How to move custom IDesigner to separate assembly in the same solution? - c#

I created a custom user control with design-time support like this:
Project/Assembly "MyUserControl"
MyUserControl.cs
MyUserControlDesigner.cs
Code is like this:
namespace MyUserControl
{
[Designer("MyUserControl.Design.MyUserControlDesigner, MyUserControl", typeof(IDesigner))]
public class MyUserControl : Control
{
// Some stuff here
}
}
namespace MyUserControl.Design
{
public class MyUserControlDesigner : ControlDesigner
{
// Some other stuff here
}
}
As long as these two classes are in the same assembly, everything works fine. VS2012 shows all my designer options. But for obvious reasons (References to System.Design and others) I don't want to have the designer code in my MyUserControl assembly, but in MyUserControl.Design. So i create a second project in the same solution:
Project/Assembly "MyUserControl"
MyUserControl.cs
Project/Assembly "MyUserControl.Design"
MyUserControlDesigner.cs
Code is like this:
namespace MyUserControl
{
[Designer("MyUserControl.Design.MyUserControlDesigner, MyUserControl.Design", typeof(IDesigner))]
public class MyUserControl : Control
{
// Some stuff here
}
}
When using this, the designer is not found at all. VS2012 does not show the component in-line selectable but like a component which has no designer attached.
Do I have to add my designer assembly to the GAC in order for VS2012 to find it or what is the problem here?
Edit: Everything works fine when adding a reference to MyUserControl.Design to WindowsFormsApplication1, but this is exactly what you don't want ...

You will need to make MyUserControlDesigner a public class.

I came to exactly the same problem. It started to work for me only when I specified the the designer class the following way.
namespace MyUserControl
{
[Designer("MyUserControl.Design.MyUserControlDesigner, MyUserControl.Design, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8bc98ae14acadc09", typeof(IDesigner))]
public class MyUserControl : Control
{
// Some stuff here
}
}

Related

How do I use a separate C# file on one xaml file

I am making an AI in WPF and i want to use a separate C# file for the AI. When I type start(); in one file I want it to find it from the other file and use it, and when I type myImage.visibilty = visibility.hidden so that it will hide the image from the xaml from the original c# file.
Here is my second C# file
using System.Windows;
namespace Hexapawn
{
public class AI2 : MainWindow
{
public AI2()
{
InitializeComponent();
//somecode
}
public void start()
{
//somecode
}
}
}
I have tried
using myproject.Myfile;,
using myFile;
but it isn't able to use methods from the other file or change the xaml.
How about a partial class. Your main window class is already a partial class. You can declare 1 more partial class of the same class in a separate file and use all the methods from it in your first class.
However, your AI2 class seems more like a Model to me which will deal with non-UI business logic. You can implement the MVVM pattern which is more suited for WPF-based applications and make your new AI2 class as the Model of MVVM.

Change namespace on dynamic loading of assembly

I have an app which uses a specific type in a separated dll (developed by someone else).
Say it is InnerType :
namespace SeparatedAssembly
{
public class InnerType
{
}
}
Until now, I was referencing a version of this dll in Visual Studio and I was using the InnerType in my app. However, since the code inside the InnerType could change, the assembly is loaded at runtime with the "AssemblyResolve" event.
But now, the namespace of this class has changed :
namespace SeparatedAssembly.Inner
{
public class InnerType
{
}
}
So, I have an exception TypeLoadException because my app can't find this type anymore. I can't just reference this new version and change the namespace I use, because it as to be compatible with the old versions of this dll.
So my question is: is it even possible to specify the namespace to look for in an assembly, in the AssemblyResolve event?
If there is a way to catch this exception and try with a different namespace, it's also OK.
Thanks.
No, the full name of the method to be called is specified in the calling assembly, and you can't "rewrite" it in an easy way. The namespace is part of the name. I'll make a reference to another response I gave some time ago: Is C# namespace compiled into IL files to be “complete” names?.
To give an example in TryRoslyn:
namespace Foo
{
public class Bar
{
public void Zoo()
{
Console.WriteLine("Hello");
}
}
}
is translated to
.class public auto ansi beforefieldinit Foo.Bar
extends [mscorlib]System.Object
{
(the namespace Foo is directly part of the name Foo.Bar)
and then the method call to Console.WriteLine is:
call void [mscorlib]System.Console::WriteLine(string)

Can't case webusercontrol, namespace not found

I have this control:
public partial class controls_UploadedImageView : System.Web.UI.UserControl
{
And in a static function I have this code:
using (var ctl = (controls_UploadedImageView)tmp0.LoadControl("~/controls/UploadedImageView.ascx"))
{
ctl.RenderControl(h);
}
However the cast to `` fails:
The type or namespace name 'controls_UploadedImageView' could not be
found (are you missing a using directive or an assembly reference?)
I can't work out how to cast the control properly so I can set it's properties before rendering.
Update
Turns out that as my project is a website project and not a Web Application, it is causing this issue. A solution is to convert the entire project to a web application but this looks time consuming and fiddly. Does anyone have any solution that doesn't require me to convert the entire project?
I think I have fixed this before by using an interface.
Create an interface in the app_code folder
public interface ICustomControl
{
... add any extra methods here
}
when you declare the class for your user control, include that interface
public partial class controls_UploadedImageView : System.Web.UI.UserControl, ICustomControl
then use that interface.
using (var ctl = (ICustomControl)tmp0.LoadControl("~/controls/UploadedImageView.ascx"))
This is all from memory, but hopefully it gets you close to the solution. I'll check my code later if its not helping.
Try simply adding a namespace to your .ascx.cs (may need to update the .ascx also). Then add a using <namespace>; statement to the class that needs to reference that control.
Ex:
namespace MyFancyNamespace
{
public partial class controls_UploadedImageView : System.Web.UI.UserControl
{
}
}
and...
using MyFancyNamespace; //this goes at the top of your class.
using (var ctl = (controls_UploadedImageView)tmp0.LoadControl("~/controls/UploadedImageView.ascx"))
{
ctl.RenderControl(h);
}

Error in InitializeComponent of form designer from namespace and constructor names

I have brought in some old (not my own) code to a new solution which I want to modify. I am new to C# so I apologize if my terminology is incorrect or have missed anything obvious.
I have a main form MainForm.cs and custom component CustomComponent.cs which inherits the PropertyGrid. The CustomComponent is within the MainForm.
I am getting an error:
The type name 'MainNameSpace' does not exist in the type 'MainNameSpace.MainNameSpace'
Within MainForm.Designer.cs I have the generated code:
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
// stuff
this.customComponent1 = new MainNameSpace.MainNameSpace();
// more stuff
}
If I manually go in and change it to just "new MainNameSpace();" it works just fine. I don't know why the name of the namespace is added twice. I tried checking the .resx file for anything suspicious but didn't see anything, not that I'd know what to look for. Is there a good starting place to look? I've gone into the MainForm, CustomComponent, and everything else I can think of looking for the culprit but have not found anything that looks like the culprit. Any ideas as to where I should be looking/what to look for? I feel like the issue is in CustomComponent.cs but I do not know.
CustomComponent.cs:
namespace MainNameSpace
{
public partial class MainNameSpace : PropertyGrid
{
public MainNameSpace()
{
InitializeComponent();
}
// other stuff
}
}
Thanks!
I have had this problem before and I am pretty sure that it is with these lines:
namespace MainNameSpace
{
public partial class MainNameSpace : PropertyGrid
{
The namespace and class cannot have the same name because it doesn't know which one you are calling. Try changing either the namespace or class name and it should work.
Hope this helps!

WPF: XAML namespace

I have one assembly XAML registered in AssemblyInfo.cs:
[assembly: XmlnsDefinition("http://schemas.mysite.es/wpf", "SWC.ViewModels")]
And In my namespace I hace these classes:
namespace SWC.ViewModels
{
public class MenuViewModel
: ObservableCollection<MenuViewModel>
{
public MenuViewModel()
: base()
{
}
}
}
If I use the namespace in a user control XAML,
<UserControl x:Class="SWC.UserControls.UserMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:customMenu="http://schemas.mysite.es/wpf">
I can use the class in my XMLNS namespace,
<UserControl.Resources>
<customMenu:MenuViewModel x:Key="MenuItemsSource">
But, when I execute the application, the compiler said
The label 'MenuViewModel' dosen't exist in the namespace XML 'http://schemas.mysite.es/wpf'
All, can help me?? I'm crazy with this problem!!
Best regards,
I think you need to specify the assembly in which your customMenu controls exist. The assembly must also be referenced from the project (in the References section).
xmlns:customMenu="clr-namespace:customMenuNamespace;assembly=customMenuLibrary"
Otherwise I don't see how the compiler can find your implementation only through "http://schemas.mysite.es/wpf". What's at that address? The two microsoft schemas
http://schemas.microsoft.com/winfx/2006/xaml/presentation
http://http://schemas.microsoft.com/winfx/2006/xaml
work like some identifiers for the xaml compiler, there is nothing at those addresses.

Categories

Resources