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);
}
Related
.I m very new to programming, and I think I'm loosing my mind trying to understand what's the problem here. Does Anyone have any suggestions?
The Test Project has Reference set to OrderManagementSystem.Domain and OrderManagementSystem.Controllers.
The Controllers Class is Public .
Im able to access Classes from Domain Namespace , but not Controllers??
What did i do wrong?
This error is because your class name is Controllers and namespace also contains .Controllers.
Either update the name of class or remove .Controllers from the namespace OrderManagementSystem.Controllers
In Controllers.cs file,
using Systems;
....
//Remove .Controllers from namespace
namespace OrderManagementSystems
{
public class Controllers
{
//Your code
}
}
I'm making a Xamarin cross-platform app and i'm trying to pass a custom class parameter to the constructor of a new page class, but the error "Inconsistent accessibility: parameter type is less accesible than method" showed up.
I have a page that shows a ListView with grouped items and I want to push a new page into the navigation stack when any of the items of the ListView is tapped. All of the XAML and C# code in the .cs and .xaml files where I declare the event that pushes the new page is OK and doesn't give any kind of errors. I'm declaring the event right here:
private async void Exercises_ItemTapped(object sender, ItemTappedEventArgs e)
{
var tappedExercise = (Exercise)sender;
if(tappedExercise != null)
{
await Navigation.PushAsync(new ExerciseInfoPage(tappedExercise));
}
}
The ExerciseInfoPage that I push and that gives the error is right here:
using PumpFit.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace PumpFit
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class ExerciseInfoPage : ContentPage
{
public ExerciseInfoPage(Exercise tappedExercise)
{
InitializeComponent();
}
}
}
The error shows only when I'm trying to pass a custom class like Exercise, when I pass a .NET class or a class that alredy came with the libraries or packages of the system it doesn't give the error. The error: error image
PS: I need to send that Exercise parameter because In the new page I need to show details about the selected item of the ListView
IF SOMEONE COULD HELP I WILL BE VERY GRATEFULL :)
The reason that is the Constructor of ExerciseInfoPage is public , we need to keep the parameter with it be the same attribute .
For example , if setting the constructor be private , there will be no error :
private ExerciseInfoPage(Exercise tappedExercise)
{
InitializeComponent();
}
However , navigation method will occurs error :
Navigation.PushAsync(new ExerciseInfoPage(tappedExercise));
Therefore , here we need to use public for Constructor first , then Exercise need to be public .
Thanks for the comments and replies! you make me find that the Exercise class that I'm using in the constructor parameters was internal and not a public class, That issue was causing the error. Make sure to always set the visibility of all of your classes, if you do so that will prevent dumb errors like this one to happen in your code.
I created a new Razor Class Library (using "dotnet new razorclasslib"). I then created a folder called Models in the new library and it contains a class with the following test code:
using System;
namespace TestLibrary.Models
{
public class TestModel
{
public string TestMethod { get; set; }
}
}
However, when I try to use this class from the web application project (by referring to it as TestLibrary.Models.TestModel) I keep being told that the TestLibrary.Models namespace doesn't exist. The exact error is:
The type or namespace name 'Models' does not exist in the namespace 'TestLibrary' (are you missing an assembly reference?) [TestApplication]
I'm assuming I'm missing something simple, just can't figure out what.
This issue was somehow related to the issue posted here: Razor Class Library MSBuild MSB4062 Error During Compile. Once that was resolved this issue went away.
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
}
}
Using .NET 2.0, C#, Windows Forms development, Enterprise Library 3.1.
We have a project namespace (call it Project). We also have several sub-namespaces inside of that project, for example Project.Namespace1, Project.Namespace2, etc.
In one class, we define enums and such to be used with the Enterprise Library Logging block, like this:
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
// enum values here
}
}
}
In another class, I use the enum values so I need to declare a using statement. Same project, so there is no assembly to reference, right?
If I declare the using inside of the local namespace, like this, it works fine:
namespace Project.SomeName
{
using Project.Logging;
// code referencing the Logging enum
}
However, if I put the using statement outside of the local namespace declaration, I get the "type or namespace name 'LogPriority' does not exist in the namespace 'Project.Logging'... Like this:
using Project.Logging;
namespace Project.SomeName
{
// code referencing the Logging.LogPriority.whatever
}
Why is this? Has anyone run across this before?
I have run into similar (though not exactly the same) problems before when using a class that has the same name as its namespace.
Oddly enough it seemed to compile ok on some developers pc's but not on others. In the end we made sure that no namespace contained a class of the same name.
namespace Project.Logging
{
public static class Logging // this is what caused the probems for me
{
}
}
I also had a wired error. I cannot find any namespace which is coming from different assemblies, but begins with executing assembly name.
Finally, I found out that I have set the target framework to .NET framework client profile.
Yes, most likely you have an unusual value set for the "Default Namespace" in your project properties. I would validate the project configuration.
We ran into this issue before and it all went down to ambiguous naming of the namespace and the class name.
When we tried to have our namespace as Services.Web.xxx and also add in a service reference as Services.Web.xxxx and ALSO add a references to an assembly that was named Services.Web.xxx you can only imagine the problems we ran into.
In the end to fix it we simply did a rename to make sure that there was only one instance of the Services prefix
Also you could do the following and create an alias to LogPriority to LogEnum:
using LogEnum= Project.Logging.Logging.LogPriority;
namespace Project.SomeName
{
internal class MyClass
{
public MyClass()
{
LogEnum enum1 = LogEnum.None;
}
}
}
namespace Project.Logging
{
public static class Logging
{
public enum LogPriority
{
None,
Default
}
}
}
It definitely can make a difference if you have usings inside or outside the namespace. There is a good discussion here, and it is likely to be related to your default namespace settings.