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

  • 相关阅读:
    深入浅出Nginx
    图解MySQL索引BTree(B+Tree)
    postman升级后,collection集合中的接口找不到了
    PHP抖音SDK搭建问题整理
    vi10
    vi8
    vi9
    vi4
    shell 自动输入密码
    LINUX平台可以用GDB进行反汇编和调试。
  • 原文地址:https://www.cnblogs.com/lihaiyin/p/3455856.html
Copyright © 2011-2022 走看看