can't use a user control - c#

I've just added a user control to a windows phone project, but I can't use it.
what I did after creating the control ( the old fashion way: click add --> UserControl, named it RecordList and added a text block and a button in canvas. Simple), I added the namespace in the MainPage of my project like this: xmlns:local="clr-namespace:Project1" and in the Content Panel I added the control like this: . But it gives me an error :The type 'local:RecordList' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
I picked this method from this link
PS: I am a beginner in the software development.

Try adding the assembly in the namespace declaration attribute
"xmlns:local="clr-namespace:Project1;assembly=%Name of the Assembly%" i.e. (Presentation.Infrastructure) where Presentation is the project name, and Infrastructure is the folder where the control lives. –

Related

Can I encapsulate all of my dependencies inside my dll project?

My Current Situation:
I have a main project, Project_A,
a dll project Project_Parent_dll
and another dll project Project_Child_dll.
Project_Child_dll contains a wpf user control.
Project_Parent_dll contains a wpf user control, that uses the user control contained in Project_Child_dll.
This Project contains a reference to Project_Child_dll.
Project_A uses the user control from Project_Parent_dll.
Right now, I have to reference both Project_Parent_dll and Project_Child_dll, and that works fine, but when I delete the reference to Project_Child_dll I get an XamlParseException.
Question:
Is there a way I could avoid the reference to Project_Child_dll in my Project_A?

Why is XAML(WPF) not searching in my XML namespace definitions for the controls?

I've two similar projects. One is a Silverlight project and the other one a WPF. Both of them containing some namespaces and plenty of custom user controls.
As the controls are distributed over many namespaces, I have to define quite a few namespaces, when I'm using them. So I started to define the XML namespaces in the AssemblyInfo.cs:
[assembly: XmlnsPrefix("http://ui.example.com/xaml/touch", "cui")]
[assembly: XmlnsDefinition("http://ui.example.com/xaml/touch", "example_ui.controls")]
[assembly: XmlnsDefinition("http://ui.example.com/xaml/touch", "example_ui.themes")]
Now I have to define only one namespace in each file:
xmlns:cui="http://ui.example.com/xaml/touch"
Unfortunately this only works in Silverlight. My question is, how do I get this to work in a WPF project?
In the WPF project I get errors like this:
Error 5 The tag 'LookUpField' does not exist in XML namespace
'http://ui.example.com/xaml/touch'. Line 7 Position 14. D:\src\prototype\lookup-control\lookup-control\MainWindow.xaml 7 14 lookup-control
Althougth the tutorial worked for Silverlight, I believe it may be inaccurate for WPF. See accepted answer here.
I have a separate assembly with custom controls in WPF and I'm able to use the XmlnsDefinition attribute without a problem. I think the problem lies within the definitions need to exist before the assembly is built. So even though controls will show up in your designer, it can't be built because the compiler needs the definitions while compiling the XAML in BAML which are built into the assembly at a later point.
Edit
I created another WPF user control library and added the same XmlnsDefinitions you used, added the reference of the user control library to my WPF application project, used <cui:UserControl1 /> in my MainWindow.xaml and had no errors.

how to add a reference of a user control in a xaml file which is shared by two projects

I have created a class library(CustomMapControl) in the solution and inside this class library I create a UserControl(MapItemsControl).
Now, I added a reference of this class library to both projects (Portogruaro & Trieste).
Here is my solution structure.
Portogruaro is the main project and has all files, Trieste has almost all the files which Portogruaro have added as a reference.
So, the problem is when I tried to use the CustomMapControl and drag and drop it in xaml from toolbox it doenst build and give this error
the name "MapsItemControl" doesnt exist in namespace "clr-namespace:CustomMapControl"
And sometimes it shows this error
Element is already a child of another element
Here is the xaml namespace
xmlns:cc="clr-namespace:CustomMapControl"
and this is the user control in xaml
<cc:MapItemsControl /> <cc:MapItemsControl />
The error changes when I open xaml file from different projects.
The xaml file in which I want to have this UserControl is also shared between the two projects.
I am quite sure that this is a referencing issue.
I have no idea how to reference the CustomMapControl so it will work for both the projects.
You probably have a problem with how your namespace is added in your wpf project. Instead of:
xmlns:cc="clr-namespace:CustomMapControl"
try this:
xmlns:cc="clr-namespace:CustomMapControl;assembly=CustomMapControl"

Converting WPF application project into UserControl Library causes InitializeComponent() can not be find error

I was working on the project where I was creating a bunch of User Controls. At the later time I have decided to move those controls into a UserControl library. So that's what I did:
Created a new VS2010 project (User Control library).
Set the .NET version to 3.5
Created a bunch of folders
Added files from the previous project to my library
However when I try to build my library, I get the following error:
Error 14 'EChart.Controls.EPreviewGraphSelector' does not contain a
definition for 'InitializeComponent' and no extension method 'InitializeComponent'
accepting a first argument of type 'EChart.Controls.EPreviewGraphSelector'
could be found (are you missing a using directive or an assembly reference?)
C:\Projects\eControls\EChart\EChart\Controls\EPreviewGraphSelector.xaml.cs 73 18 EChart
Namespace of my XAML file and code behind do match. The whole project when it was an application rather than a library was building fine.
Here is the simplified code for my UserControl:
<UserControl x:Class="EChart.Controls.EPreviewGraphSelector">
Code behind:
namespace EChart.Controls
{
public partial class EPreviewGraphSelector : UserControl
{
public EPreviewGraphSelector()
{
InitializeComponent();
}
}
}
g.cs file doesn't get generated for this user control !
Build Action set to page.
I have all necessary references added
User control Build tool is set to MSBuild:Compile
EDIT:
I have found the possible place where it fails to compile:
If I comment out all code apart from constructors, g.cs file for my usercontrol gets generated, however it comes with an error:
Error 2 The type or namespace name 'Windows' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) C:\Projects\eControls\EChart\EChart\obj\Debug\Controls\EPreviewGraphSelector.g.cs 15 17 EChart
this comes from this part of g.cs
using Microsoft.Windows.Themes;
Any ideas why is that happening?
Actually I have sold the issue, I am not sure why it occurs, but I know hot to fix it.
In my UserControl I had a following reference:
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Luna"
This was probably added by Blend, when I was styling some standard controls. When I am in the design mode I don't get any error messages. So when I compile my userControl, I get g.cs files where my InitializeComponent method is generated. Then there I get the reference to Microsoft.Windows.Themes;, because of that reference I had in my xaml (see above). And when it compiles the g.cs file, then it fails, so the g.cs file is not generated and no InitializeComponent methods exists, which result in that error.
I guess you have not set up the project correctly. I suggest to look at the setup of your WPF application, such as references, and copy them to your library. Is the build tool of the user control set to MSBuild:Compile?
Have you checked the namespaces of the existing files you have added to the new Project? I could imagine that there are inconsistencies or different namespaces that don't fit, or namespaces in xaml don't match the namespaces in the class definitions.

Read resources from a DLL file

I've two Visual Basic 2008 projects - one is a class library project and another is a Windows Forms project. In the class library project, I've defined some strings in the project resources (project properties > Resources tab).
I build that class library project and get the DLL file from the debug folder and added up as a reference in my Windows Forms project.
How do I read those strings from the referenced DLL file?
While you can dynamically load the DLL as ho suggests, it's fine to use a reference as you have done. In fact I would recommend using a reference unless you had a particular requirement to dynamically load the resource assembly.
As to accessing the resources there are a few things you need to do.
In the resource assembly you will need to ensure the resources are public. By default resources are set to internal which means you will not see the resources in the winforms app. Double click on Properties\Resources.resx to open the resources view. In the top toolbar you will see a label "Access Modifier" next to a combo box drop down. Change the selection to public.
You will need to reference the assembly from the forms app. You have stated you have already done this. Just a note that a better way to do this is to create a solution that contains both projects. Then in the forms app choose add reference. Click on the Projects tab up the top. Double click on the resource DLL project name. This works better than referencing the debug DLL directly since it means if you change between a release build and debug build in your forms app, it will automatically build a matching release/debug version of your resource assembly.
Once you have added the reference you can simply reference the type out of the resources DLL, e.g.
ResourceDLLNamespace.Properties.Resource.ResourceName
Just a note, you need to be aware of type name clashes if you are using the same namespace for your forms app and resource DLL. In this situation both your forms app will have access to it's own Properties.Resources class as well as that of the resource DLL. You can do two things to avoid this:
Use a different namespace between the two assemblies
In the resource assembly don't include a default Properties\Resources.resx resource dictionary. Delete it and manually add a new resource, i.e. Add New Item and select "Resources File". You should find that you will not be able to add the new resource dictionary to the Properties folder - add it to the root or some other folder as you require. This will automatically give it a different type name by virtue of being in a different folder. You still may want to avoid using the resource file name of "Resources" however, as if you have all the relevant namespaces in scope via using statements you will get the same issue that the compiler won't know which version of Resources to use.
-Donovan
I think you just use System.Reflection.Assembly.Load to load the other assembly then use the constructor of System.Resources.ResourceManager that takes an assembly.
Note, I don't think it needs to a reference for this to work.

Categories

Resources