zoukankan      html  css  js  c++  java
  • Windows 8.1 开发过程中遇到的小问题

    最近在开发Windows 8 应用的时候碰到了一个莫名的问题,错误内容如下:(其中 **.DLL是本地创建的项目,在主项目中添加了引用,其中大部分代码是MVVM light 框架库的代码)

    System.InvalidCastException”类型的异常在 **.DLL 中发生,但未在用户代码中进行处理

    其他信息: Unable to cast COM object of type 'System.ComponentModel.PropertyChangedEventHandler' to class type 'System.ComponentModel.PropertyChangedEventHandler'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

    前提是:在charm栏中选择分享,并选择分享到的应用;正常启动应用是不会有事的。我很纳闷为什么,渴望大神赐教

    出错代码是这里(红色字体部分):

    [SuppressMessage("Microsoft.Design", "CA1030:UseEventsWhereAppropriate",
                Justification = "This cannot be an event")]
            protected virtual void RaisePropertyChanged(string propertyName)
            {
    #if WIN8
                if (string.IsNullOrEmpty(propertyName))
                {
                    throw new NotSupportedException(
                        "Raising the PropertyChanged event with an empty string or null is not supported in the Windows 8 developer preview");
                }
                else
                {
    #endif
                    //VerifyPropertyName(propertyName);
    
                    var handler = PropertyChangedHandler;
    
                    if (handler != null)
                    {
                        handler(this, new PropertyChangedEventArgs(propertyName));
                    }
    #if WIN8
                }
    #endif
            }

    我以为是框架本身的问题,于是在相应的代码中,自己重新实现INotifyPropertyChanged接口,并实现此接口:

        public class LoginViewModel : ViewModelBase,INotifyPropertyChanged
        {
            event PropertyChangedEventHandler PropertyChanged;
            void NotifyPropertyChanged(string name)
            {
                if (PropertyChanged != null)
                    PropertyChanged(this,new PropertyChangedEventArgs(name);
            }
        }



    我差了相关帖子,看到了这个回复:

    Just cast it to INotifyCollectionChanged

    var collectionChanged = propertyObject as INotifyCollectionChanged;
    if (collectionChanged != null)
        collectionChanged.CollectionChanged += ...

    于是我修改代码为如下:
        public class LoginViewModel : ViewModelBase,INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            void NotifyPropertyChanged(string name)
            {
                var obj = this as INotifyPropertyChanged;
                if (obj != null)
                    obj.PropertyChanged += (s, e) => {
                        PropertyChanged(s, new PropertyChangedEventArgs(name));
                    };
            }
        }    

     OK,问题解决,关键是谁能告诉我这是为什么,什么情况下会出现这个错误,为什么会在分享中出现,而正常状态下不会呢?

  • 相关阅读:
    python requests 模拟登陆网站,抓取数据
    python 爬取淘宝的模特照片
    vim 和grep 正则表达式相似和区别
    python 读取文件时报错UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
    python 正则表达式
    12个球,其中一个和其他的重量不一样,有一个天平,最多几次找出这个球
    25匹马中选出跑得最快的3匹,每次只有5匹马同时跑,最少要比赛几次
    1000瓶药水,1瓶有毒药,几只小白鼠能够找出毒药
    146 LRU Cache
    用两个int值实现读写锁
  • 原文地址:https://www.cnblogs.com/lihaiyin/p/3455856.html
Copyright © 2011-2022 走看看