0

在为 Winows Phone 7.1 制作 MVC 应用程序时,我一直在关注 MSDN 教程作为参考:http: //msdn.microsoft.com/en-us/library/hh286405 (v=vs.92).aspx

在我的应用程序中,我在实现 INotifyPropertyChanging 和 INotifyPropertyChanged 接口的表中有一个对象,以及这样的属性:

private DateTime lastViewDate;
[Column]
public DateTime LastViewDate
{
    get { return lastViewDate; }
    set
    {
       if (lastViewDate != value)
       {
           NotifyPropertyChanging("LastViewDate");
           lastViewDate = value;
           NotifyPropertyChanged("LastViewDate");
       }
    }
}

当 LastViewDate 属性发生更改时,即使该属性显然存在,也会在调用 NotifyPropertyChanging 时引发 MissingMethodException。那么我做错了什么?(我是 wp7 编程的菜鸟,所以这可能很明显,只是对我来说不是)

编辑:更多信息

这是接口方法,添加了一些调用来检查方法:

    // Used to notify that a property is about to change
    private void NotifyPropertyChanging(string propertyName)
    {
        var type = this.GetType();
        var method = type.GetMethod(propertyName); // null
        var getMethod = type.GetMethod("get_" + propertyName); // works
        var setMethod = type.GetMethod("set_" + propertyName); // works
        var methods = type.GetMethods(); // set_LastViewDate is in the method list
        //
        if (PropertyChanging != null)
        {
            PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
        }
    }

更改对 NotifyPropertyChanging("set_LastViewDate"); 的调用 仍然给出同样的例外。(并且“方法”在调试类型检查中为空)

编辑:

堆栈跟踪:

System.MissingMethodException was unhandled
Message=MissingMethodException
StackTrace:
   at System.Activator.InternalCreateInstance(Type type, Boolean nonPublic, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type)
   at System.Data.Linq.WorkAround.ActivationHelper.CreateInstance(Type type)
   at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.CreateDataCopy(Object instance)
   at System.Data.Linq.ChangeTracker.StandardChangeTracker.StandardTrackedObject.StartTracking()
   at System.Data.Linq.ChangeTracker.StandardChangeTracker.OnPropertyChanging(Object sender, PropertyChangingEventArgs args)
   at WindowsPhonePlaces.Photo.NotifyPropertyChanging(String propertyName)
   at WindowsPhonePlaces.Photo.set_LastViewDate(DateTime value)
   at WindowsPhonePlaces.Photo.ResetViewDate()
   at WindowsPhonePlaces.PhotoViewerPage.OnNavigatedTo(NavigationEventArgs e)
   at Microsoft.Phone.Controls.PhoneApplicationPage.InternalOnNavigatedTo(NavigationEventArgs e)
   at System.Windows.Navigation.NavigationService.RaiseNavigated(Object content, Uri uri, NavigationMode mode, Boolean isNavigationInitiator, PhoneApplicationPage existingContentPage, PhoneApplicationPage newContentPage)
   at System.Windows.Navigation.NavigationService.CompleteNavigation(DependencyObject content, NavigationMode mode)
   at System.Windows.Navigation.NavigationService.ContentLoader_BeginLoad_Callback(IAsyncResult result)
   at System.Windows.Navigation.PageResourceContentLoader.BeginLoad_OnUIThread(AsyncCallback userCallback, PageResourceContentLoaderAsyncResult result)
   at System.Windows.Navigation.PageResourceContentLoader.<>c__DisplayClass4.<BeginLoad>b__0(Object args)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)
   at System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)
   at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
   at System.Delegate.DynamicInvokeOne(Object[] args)
   at System.MulticastDelegate.DynamicInvokeImpl(Object[] args)
   at System.Delegate.DynamicInvoke(Object[] args)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)
   at System.Windows.Threading.Dispatcher.OnInvoke(Object context)
   at System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)
   at System.Windows.Hosting.DelegateWrapper.InternalInvoke(Object[] args)
   at System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)
4

2 回答 2

1

我将解决方案作为单独的答案放在此处以明确表示:

问题是我将构造函数设为私有并使用静态方法来创建我的数据库对象。这不适用于 LINQ——您需要一个无参数的公共构造函数。

感谢@Metro Smurf 和@Rajeev Nair 解决了这个问题。

于 2012-04-30T19:59:55.507 回答
0

是否PropertyChanging公开?如果不是,则应将其设置为公开。

于 2012-04-30T02:31:08.297 回答