在为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)
答案 0 :(得分:1)
我将解决方案作为一个单独的答案在这里说清楚:
问题在于我将构造函数设为私有,并使用静态方法创建我的数据库对象。这不适用于LINQ - 您需要一个无参数的公共构造函数。
感谢@Metro Smurf和@Rajeev Nair搞清楚。
答案 1 :(得分:0)
PropertyChanging
是否已公开?如果没有,则应将其设置为公开。