WPF Drag, drop image into canvas, reject wrong ones - wpf

I have 8 images and 2 canvas, and i am trying to do a drag drop game, which will be able to reject the wrong image that is drop into the wrong canvas. I tried many codes online just for the drag drop function but it didn't work. The most common error I have is
1) "The type 'WpfApplication9.Window1' already contains a definition for 'butterfly'"
2) 'WpfApplication9.Window1' does not contain a definition for 'Grid_PreviewMouseDown' and no extension method 'Grid_PreviewMouseDown' accepting a first argument of type 'WpfApplication9.Window1' could be found (are you missing a using directive or an assembly reference?)
How do solve the problem? Thankyou in advance

These are very basic errors and developers typically solve these by pasting them into any search engine and reading the result pages. If you can't fix these kinds of problems yourself, you'll have real problems later. Either way, your first error is this:
The type 'WpfApplication9.Window1' already contains a definition for 'butterfly'
In plain English, this means that you have a class named Window1 in a namespace named 'WpfApplication9 (so I'm assuming this is your 9th test application) and in that class, you have declared a property, method, enum, or other member named butterfly... the actual problem is that you have defined two members named butterfly in the same class. You cannot do this, so rename one of them.
Your second error is:
'WpfApplication9.Window1' does not contain a definition for 'Grid_PreviewMouseDown' and no extension method 'Grid_PreviewMouseDown' accepting a first argument of type 'WpfApplication9.Window1' could be found (are you missing a using directive or an assembly reference?)
Again, this is a very common error and simply means that you have attached a PreviewMouseDown event handler to your Grid, but then you didn't actually declare the handler method itself. The solution is to implement your Grid_PreviewMouseDown method.

Related

how to get rid of namespace errors caused by including custom control in wpf application?

I am working on a wpf-project that used a ScrollViewer to show a range of items.
In order to create a more advanced look and feel for the application, I replaced the Scrollviewer with a Carousel-control.
The code for the Carousel-control I downloaded from here:
https://www.codeproject.com/Articles/181835/WPF-Carousel-Control
Conceptually, the carousel control (as part of my wider application) consists of a large black box (which one does not need to touch) plus just a few lines of code for plugging in the carousel control. The black box is the Carousel project, highlighted in the following screenshot:
In order to plug the carousel-control in the application, just a couple of lines had to be commented out and a couple of other lines had to be added. The comments in the following two screenshots mark off the lines of code that had to be commented out and the lines of code that had to be added (in order for the scrollviewer-control to be replaced by the carousel-control):
... and (at the end of the file):
Furthermore, at the beginning of the xaml-file that contains the carousel, the following line was included:
xmlns:Carousel="clr-namespace:Carousel;assembly=Carousel"
as can be seen in the following screenshot:
As you can see, at the moment, the code for the scrollviewer is commented out. Hence, the application is using the Carousel at the moment.
This carousel works fine. However, the error list shows a bunch of errors, mostly of the following type:
the name "XYZ" does not exist in the namespace "bla".
The following screenshot shows some of the error messages:
As already mentioned, the carousel-control works despite those error messages. However, I still need to get rid of the error messages before leaving this project.
Regarding the error messages, the following points might be of interest:
No error messages are shown, when the scrollviewer-control is used.
As you can see from screenshot 5, most error messages refer to line 1 of the file CarouselControl.xaml. This file is located inside the "black box":
The beginning of file CarouselControl.xaml contains the following lines of code:
As you can see from screenshot 5, most of the error messages say that some name or property does not exist in namespace xyz, whereby namespace xyz is one of the namespaces listed at the beginning of file CarouselControl.xaml (see screenshot 7).
********************************UPDATE*****************************************
(Right-click on) Solution -> Properties -> Configuration Properties, I noticed that it was apparently not possible to change the configuration of the Carousel-project to anything other than platform x86:
No matter which platform I selected (from the drop down menu at the top of the screenshot), the platform for the Carousel-project stubbornly remained at value "x86".
Might this have something to do with the error messages?
I'm not sure how useful this may be, but I believe I have found the code that was the source of my problem:
var variables = values[0] as ObservableCollection<variable>;
var identifier = values[1] as string;
var variable = variables.SingleOrDefault(x => x.identifier == identifier);
if (variable == null) return "";
This is code of my converter to access a variable in a collection and return its value as a string, it was used on a TextBox control.
Looking back over my branches, it seems that I made this change:
var variable = variables?.SingleOrDefault(x => x.identifier == identifier);
Resharper suggested adding a Null-conditional Operator at this point, which solved my issue. It would seem that the designer was returning errors due to the LINQ expression, since the collection at this point was uninitialized.
I would look through your code to see if you have a similar instance of uninitialized collections.

To solve warning 'is never used' in WPF

I have declared an event in "Question" class as:
public event SectionAffected OnSectionAffected;
I have not used this event in the entire class.But I have used it in another class as:
Question.OnSectionAffected += new Question.SectionAffected(ResetDependentSection);
I am getting warning as:
The event 'QuestionManager.OnSectionAffected' is never used in Question class.
How to solve this warning?
A warning is just that... a warning. Having warnings is not necessarily a bad thing. However, if you really want to remove it, you can specify that in Visual Studio. If you open the relevant project's property page (by pressing ALT + Enter when the project is focused), turn to the Build tab. Click the RadioButton named Specific warnings and enter the specific error code(s) that you want to supress in a comma separated list. You can find out more from the How to: Suppress Compiler Warnings page on MSDN.
UPDATE >>>
I believe that you are looking for the numerical error code number... for this, you should look in the Output Window of Visual Studio. If you don't already use this window when writing WPF, then I would strongly advise you to do so. However, these codes are not always shown in this window.
As an alternative, you can find descriptions and error codes in the C# Compiler Errors and Code Analysis for Managed Code Warnings pages on MSDN.
UPDATE 2 >>>
After doing a quick search online (which is really what you should have done), I found this page which seems about right: Compiler Warning (level 3) CS0219

Dynamic in xaml Data Template selector

Ok I found this wonderful idea on code project.
link
The idea is great add all your data templates to collection in your list box. Tell each one what to look for (a type) and what data template to use when it runs into that type. The problem is that the included source code is different from the on page code and I can't seen to get any combination of it to work. Even adding the missing quotation marks and changing the type to a local class instead of the non-accessible string and int32 classes.
So the question is. What am I doing wrong?
Bryan
Should be totally redundant when you have DataTemplate.DataType.
(Example)

Error when adding user-defined control to a form.

I have an user-defined control which includes a property ElementList of IList<WFParament> type. When I add my control into a form, I get the error:
"WorkFlowDesign.WFParament[]" can't be transfered into "WorkFlowDesign.WFParament[]"
Does your usercontrol do anything on load? If so, try wrapping it in:
if(!DesignMode)
{
//Do something
}
I would verify that you don't have two versions of the assembly containing the type WorkFowDesign.WFParament loaded/referenced. This could happen if your project where you are using the user control references one version of the assembly while the user defined control is compiled against a different version. If this is the case you will need to update one of the two so that they are referencing the same version of the assembly containing WorkflowDesign.WFParament.

Warnings and errors (CS0436, CS0234) when creating user controls composed of other user controls in the same project

I'm working on a Windows Forms solution with many winform ui projects.
There is a class library project that contains some custom shared controls, named MyControls.
Now, when I create a control in MyControls
that is composed of one or more controls in the same project, I run into problems.
I either get compilation warnings: warning CS0436: The type 'MyType' in 'path-to\MyType.cs' conflicts with the imported type 'MyType' in 'MyControls.dll'. Using the type defined in 'path-to\MyType.cs'. Or I get a bunch of different compilation errors, all pointing to "MyControls.dll" (error CS0234 - "are you missing an assembly reference?").
I get either the errors, or the warnings, never both.
How to solve this?
Note
I added visual-studio-2010 because that's the version I experienced the problems with. No idea if this relates to other versions too.
I found that Visual Studio adds a self-reference to MyControls when I drop a control from the MyControls project on another control in MyControls:
<Reference Include="MyControls, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL" />
My current work-around is to manually delete this reference from the MyControls.csproj using a text editor.
When I've done this, everything works fine, until I drop another control that triggers a self reference.
Better solutions are appreciated!
you can make a small and "legal" change in your solution and get the "legal" solution... lets say your project name is: "project01"
go to references folder in your project - one of your references called "project01" - just remove it...
the the warning is very fair! you design a form and in the other hand import your project as a reference!
I know this thread is a bit old, but I just went looking for a solution to this issue, and it seems that MS doesn't have anything other than what Marijn suggested earlier:
https://connect.microsoft.com/VisualStudio/feedback/details/613502/automatically-add-self-reference
Hopefully it's fixed in VS 2012.

Resources