无法创建类型的实例 [英] Could not create an instance of type

查看:89
本文介绍了无法创建类型的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在更改某些内容后立即得到了这个错误,但是我不记得是什么,即使注意到这一点,我还是撤消了我在错误之前所做的一切,但仍然没有答案.

我正在做一个项目,女巫使用元素宿主. elementHost应该连接到我的Usercontroll,看起来像这样:

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>



"MonthViewCalendar"仅是一个类,其中没有任何xaml文件,该类的代码为:



The "MonthViewCalendar" is only a class, without any xaml files in it, and the code of this class is:

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>



如果我进入设计视图,它将抛出我提到的错误,并选择行< local:monthviewcalendar xmlns:local =#unknown">.

我无法编译应用程序,也无法将元素宿主的子对象设置为我的UserControll.

我什至试图取消注释我写的这里的代码"或这里的东西"的所有行,女巫很长,我认为这与它有关,但仍然-对我没有答案.

有人知道有什么大不了的吗?

希望能尽快得到答复,
汤姆

[修改:将三倍的反斜杠转换为双倍,因为它弄乱了颜色]



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]

推荐答案

您可以尝试将
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


1)User控件的标签关闭!是jjuts错字吗?

2)从Calendar控件标记中删除xmlns:local属性.
1)The tag close of the User control!! Is it jjuts a typo??

2) Remove ths xmlns:local attribute from the Calendar control tag.



中的异常
The exception in
foreach (Appointment appointment in (ObservableCollection<Appointment>)values[0]


可能会因为无法将值转换为ObservableCollection而引发.
尝试做这样的事情:


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; }

 } 



也许那不是处理它的最佳方法,但应该摆脱异常.

GetDate函数中的问题可能是由设计时输入数据错误引起的.您需要精确的字符串DD/MM/YYYY,如果不符合格式,则不会处理.您也可以在此处添加try catch或使用更好的验证方法



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


这篇关于无法创建类型的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆