Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Well I got this error right after I changed something, but I can't remember what, and even after noticing this thing I UNDOed everything I have done before the error, and still - no answer.

I'm working on a project witch uses an element host. The elementHost should connect to my Usercontroll, witch looks like this:
<usercontrol>
    x:Class="CustomCalendar.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CustomCalendar"
    xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
    xmlns:primitives="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"
    xmlns:vsm="clr-namespace:System.Windows;assembly=WPFToolkit"     
    Width="500"
    Height="400" FlowDirection="RightToLeft">

    <grid>
        <local:monthviewcalendar xmlns:local="#unknown" />
    </grid>
</usercontrol>


The "MonthViewCalendar" is only a class, without any xaml files in it, and the code of this class is:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Drawing;
using System.Data;
using Microsoft.Windows.Controls;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

namespace CustomCalendar
{
    // <summary>
    // Custom calendar control that supports appointments.
    // </summary>
    public class MonthViewCalendar : Calendar, INotifyPropertyChanged
    {
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        public static DependencyProperty AppointmentsProperty =
            DependencyProperty.Register
            (
                "Appointments",
                typeof(ObservableCollection<appointment>),
                typeof(Calendar)
            );

        // <summary>
        // The list of appointments. This is a dependency property.
        // </summary>
        // 

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);

        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
        private bool addingApp = false;
        bool HiddingForm = false;
        public ObservableCollection<appointment> Appointments
        {
            get { return (ObservableCollection<appointment>)GetValue(AppointmentsProperty); }
            set { SetValue(AppointmentsProperty, value); }
        }

        static MonthViewCalendar()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(MonthViewCalendar), new FrameworkPropertyMetadata(typeof(MonthViewCalendar)));
        }

        public MonthViewCalendar()
            : base()
        {
            if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                //my code here..
            }
        }
        protected override void OnMouseDoubleClick(MouseButtonEventArgs e)
        {
            base.OnMouseDoubleClick(e);
            if (addingApp == true)
            {
                FrameworkElement element = e.OriginalSource as FrameworkElement;
                if (element.DataContext is DateTime)
                {
                    AppointmentWindow appointmentWindow = new AppointmentWindow
                    (
                        (Appointment appointment) =>
                        {
                            Appointments.Add(appointment);
                            if (PropertyChanged != null)
                            {
                                PropertyChanged(this, new PropertyChangedEventArgs("Appointments"));
                            }
                        }
                    );
                    appointmentWindow.Show();

                }
            }
            else
            {
                if (this.SelectedDate != null)
                {
                    // something goes here
                     
                }
            }
        }


        static public System.Drawing.Font BoldFont(System.Drawing.Font font, string result)
        {
            if (font != null)
            {
                System.Drawing.FontStyle fontStyle = font.Style;
                if (result == "bold")
                {
                    if ((fontStyle & System.Drawing.FontStyle.Bold) == 0)
                    {
                        fontStyle |= System.Drawing.FontStyle.Bold;
                        font = new Font(font, fontStyle);
                    }
                }
                else if (result == "underline")
                {
                    if ((fontStyle & System.Drawing.FontStyle.Underline) == 0)
                    {
                        fontStyle |= System.Drawing.FontStyle.Underline;
                        font = new Font(font, fontStyle);
                    }
                }
                else if (result == "italic")
                {
                    if ((fontStyle & System.Drawing.FontStyle.Italic) == 0)
                    {
                        fontStyle |= System.Drawing.FontStyle.Italic;
                        font = new Font(font, fontStyle);
                    }
                }
                else if (result == "strikeout")
                {
                    if ((fontStyle & System.Drawing.FontStyle.Strikeout) == 0)
                    {
                        fontStyle |= System.Drawing.FontStyle.Strikeout;
                        font = new Font(font, fontStyle);
                    }
                }
                else if (result == "regular")
                {
                    if ((fontStyle & System.Drawing.FontStyle.Regular) == 0)
                    {
                        fontStyle |= System.Drawing.FontStyle.Regular;
                        font = new Font(font, fontStyle);
                    }
                }
            }
            return font;
        }

        private void MenuItemClickHandlerEdit(object sender, EventArgs e)
        {
            / // something goes here
        }



            EditAppointmentWindow editappointmentWindow = new EditAppointmentWindow
            (
                (Appointment appointment) =>
                {
                    Appointments.Add(appointment);
                    //MessageBox.Show("Added Succsefully!");
                    Event_ReloadEvents();
                    if (PropertyChanged != null)
                    {
                        PropertyChanged(this, new PropertyChangedEventArgs("Appointments"));
                        Event_ReloadEvents();
                    }
                }
            ,title,type,date,time,dhifut,assignedto,discription,EventId);
            editappointmentWindow.Show();
            editappointmentWindow.Focus();
        }

        private void MenuItemClickHandlerShow(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolStripMenuItem clickedItem = (System.Windows.Forms.ToolStripMenuItem)sender;
            MessageBox.Show("Show eventID: " + clickedItem.Tag.ToString());
        }

        private void MenuItemClickHandlerDelete(object sender, EventArgs e)
        {
             // something goes here

        }

        private void MenuClickHandlerDeleteAll(object sender, EventArgs e)
        {
            System.Windows.Forms.ToolStripMenuItem clickedItem = (System.Windows.Forms.ToolStripMenuItem)sender;
            // Take some action based on the data in clickedItem
            Event_DeleteAllEvents();
        }

        public void Event_ReloadEvents()
        {
            System.Windows.Forms.Form tF = new System.Windows.Forms.Form();
            foreach (System.Windows.Forms.Form S in System.Windows.Forms.Application.OpenForms)
            {
                if (S is Form1)
                {
                    tF = S;
                    S.Hide();
                    HiddingForm = true;
                }
            }
            if (HiddingForm == true)
            {
                //tF.Close();
                //tF.Dispose();
                new Form1().Show();
            }
        }

        public void Event_DeleteAllEvents()
        {
            DateTime d = ConvertDateExtoDate(this.SelectedDate.ToString());

            // something goes here
        }


        public void Event_CreateNewAppointment()
        {
             // something goes here
        }
    }
}
</appointment></appointment></appointment>


If I goes to the design view, it trows me the error I mentioned, and selects the line <local:monthviewcalendar xmlns:local="#unknown">.

I can't compile the application and can't set the child of the element host to my UserControll.

I even tried to uncomment all the lines witch I wrote "code here" or "something goes here", witch were extremely long and I thought it has something to do with it, but still - no answer for me.

Anyone knows whats the big deal?

Hope for a quick answer,
Tom

[Modifed: switched the triple backslashes to doubles because it was screwing up the colors]
Posted
Updated 3-Jan-11 8:25am
v2
Comments
William Winner 3-Jan-11 14:28pm    
What exactly does the error message say? (are there any inner messages?) And what line does it say the message is for?

And are you just trying to make things more complicated? What's with the
if ((fontstyle & System.Drawing.FontStyle.Bold) == 0)?

What's wrong with
if (font.Style != FontStyle.Bold)?

That's, IMO, a lot more readable.
[no name] 3-Jan-11 15:37pm    
A good case for the use of source control
TomNidi 3-Jan-11 15:43pm    
I know these code is not the best, but I don't have a problem with it right now.
When I go the the designer in order to view the code of the control, it doesn't show me the usercontrol and underlines the line "< local:MonthViewCalendar/>" with the error "Could not create an instance of type MonthViewCalendar".
Sergey Alexandrovich Kryukov 3-Jan-11 20:09pm    
Why not "local:MonthViewCalendar" but "local:monthviewcalendar" -- all low-case?
TomNidi 4-Jan-11 2:36am    
I didn't get your question, sorry for not understanding - my english is not that good.

You can try to put
try 
{
   //user control stuff here 
}
catch(Exception ex)
{
    MessageBox.Show(ex.ToString());
}

In constructor of the user control or in constructor of the parent window. It should give you some more information about what is wrong
 
Share this answer
 
Comments
TomNidi 4-Jan-11 8:05am    
I didn't get where should I put it. What is the constructor?
Anyway, even if I put it I'm not able to set the child of my element host to the user control, and therefore I can not make the code start.

Can you explain a little bit more? Thank you.
Pawel Gielmuda 4-Jan-11 8:24am    
Please read my other comment above
TomNidi 4-Jan-11 8:47am    
Already read, waiting for a new answer.
1)The tag close of the User control!! Is it jjuts a typo??

2) Remove ths xmlns:local attribute from the Calendar control tag.
 
Share this answer
 
v2
Comments
TomNidi 4-Jan-11 8:09am    
I can not remove anything from the list you have mentioned.
If I remove the tag close from the line " < local:MonthViewCalendar/> ", it throws me the next error: "Closing tag for element '<local:monthviewcalendar>' was not found.".
If I remove the xmlns:local line, it shows me the error: "The type 'local:MonthViewCalendar' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built ".

None of the rebuilds actually helped.

What shell I do?
The exception in
foreach (Appointment appointment in (ObservableCollection<Appointment>)values[0]

might be thrown because values cannot be converted to ObservableCollection.
Try to do something like that:
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
{ 
try {
   if (values == null || values.Length < 2) 
      return null;
   DateTime date = (DateTime)values[1];
   ObservableCollection<Appointment> appointments = new ObservableCollection<Appointment>();
   foreach (Appointment appointment in ObservableCollection<Appointment>)values[0]) 
   { 
      if (appointment.Date.Date == date) 
         { appointments.Add(appointment); } 
   }
 return appointments;
 }
catch(Exception ex) { return null; }

 } 


Maybe thats not the best way to handle it but should get rid of the exception.

The problem in GetDate function might be caused by wrong input data at design time. You expect exact string DD/MM/YYYY and don't handle if it doesn't meet the format. You can add try catch too here or use better validation method
 
Share this answer
 
Comments
TomNidi 5-Jan-11 15:16pm    
:) I'm so stupid.. Well, you were right of course.
I just couldn't understand why it keep telling me that the input string is wrong, but when I went to check the info in the database, instead of one date I saw "?????". I had a problem with converting the right information, and it had NOTHING AT ALL to do with my function.

I would like to thank you for your time, and I hope that you will be able to help others same as you helped me:)
Pawel Gielmuda 6-Jan-11 8:26am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900