2011-11-22 24 views
15

के प्रकार 'MS.Internal.NamedObject' की वस्तु कास्ट करने में असमर्थ रहा WPF के लिए नया नहीं हूँ, लेकिन मैं इस समस्या को समझ नहीं सकता:कस्टम प्रकार

मैं एक ObservableCollection<MyCustomType> का उपयोग है कि मैं ComboBox तों के लिए बाध्य , यह सब सामान्य रूप से महान काम कर रहा है, लेकिन किसी बिंदु पर मुझे संग्रह की सामग्री को बदलने की जरूरत है (मैं डेटाबेस से एक नया सेट पुनर्प्राप्त करता हूं, जिसमें पुराने का हिस्सा हो सकता है या नहीं)।

अब तत्वों को जोड़ना या निकालना सभी WPF द्वारा किया जाता है, लेकिन यदि आप पूरे संग्रह को बदलते हैं तो मुझे पता चला कि आपको संग्रह के लिए PropertyChanged को उठाना था।

समस्या यह है कि कभी-कभी यह मुझे एक बहुत ही अनुकूल अपवाद देने में दुर्घटनाग्रस्त हो जाता है।
मुझे पता नहीं है कि वास्तव में क्रैश को किस प्रकार ट्रिगर करता है, लेकिन यदि मैं दृश्य को स्पर्श नहीं करता हूं और डीबी से नया सेट पुनर्प्राप्त करता हूं, तो यह क्रैश नहीं होता है। मैं अपवाद

public const string MyObsCollectionProperty = "MyObsCollection"; 
    private ObservableCollection<MyCustomType> m_MyObsCollection; 
    public ObservableCollection<MyCustomType> MyObsCollection 
    { 
     get { return m_MyObsCollection; } 
     set 
     { 
      m_MyObsCollection = value; 
      RaisePropertyChanged(MyObsCollectionProperty); // <-- Crashes here 
     } 
    } 

:

यहाँ कुछ कोड है Unable to cast object of type 'MS.Internal.NamedObject' to type 'MyNameSpace.MyCustomType'

कृपया ध्यान दें कि यह अपवाद प्रिज्म से आता है, लेकिन यह शायद सिर्फ इसलिए कि मैं अपने ViewModel पर NotificationObject से विरासत है।

मैं इस समस्या पर किसी भी मदद की सराहना करते हैं,

सादर,

लालकृष्ण।

संपादित करें: मुझे लगता है कि समस्या कॉम्बोबॉक्स से आती है, और विशेष रूप से, जिस तरह से मैंने अपनी संपत्ति निर्धारित की है।

मैं आइटम्ससोर्स प्रॉपर्टी को एक अवलोकन करने योग्य चयन में बांधता हूं, और यह तय करने के लिए कि कौन सा तत्व चुनना है, मैं चयनित वेल्लू को एक पर्यवेक्षण चयन के आइटम के क्षेत्र में बांधता हूं (कॉम्बोबॉक्स एक सूची दृश्य में घोंसला है)।
मैंने चयनित वैल्यूपैथ भी सेट किया है (आपको विचार मिलता है)।

मुझे लगता है कि जब मैं पहला संग्रह बदलता हूं तो नरक ब्रेक उस सेटअप पर हार जाते हैं। और यह इसे शांति में छोड़ देता है जब मैं वर्चुअलाइजेशन के कारण दृश्य को स्पर्श नहीं करता (यह पहले छिपा हुआ है)।

अलावा:

InnerException nulll है।

संदेश:Unable to cast object of type 'MS.Internal.NamedObject' to type 'CocoonV4.DAL.WcfServiceCocoonV4.DekoTemplate'.

at Microsoft.Practices.Prism.Commands.DelegateCommand`1.<>c__DisplayClass6.<.ctor>b__3(Object o) 
    at Microsoft.Practices.Prism.Commands.DelegateCommandBase.CanExecute(Object parameter) 
    at Microsoft.Practices.Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.CanExecute(Object parameter) 
    at System.Windows.Interactivity.InvokeCommandAction.Invoke(Object parameter) 
    at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter) 
    at System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs) 
    at System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs) 
    at System.Windows.Controls.SelectionChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 
    at System.Windows.Controls.ComboBox.OnSelectionChanged(SelectionChangedEventArgs e) 
    at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems) 
    at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() 
    at System.Windows.Controls.Primitives.Selector.OnItemsChanged(NotifyCollectionChangedEventArgs e) 
    at System.Windows.Controls.ItemsControl.OnItemCollectionChanged(Object sender, NotifyCollectionChangedEventArgs e) 
    at System.Collections.Specialized.NotifyCollectionChangedEventHandler.Invoke(Object sender, NotifyCollectionChangedEventArgs e) 
    at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 
    at System.Windows.Controls.ItemCollection.System.Windows.IWeakEventListener.ReceiveWeakEvent(Type managerType, Object sender, EventArgs e) 
    at System.Windows.WeakEventManager.DeliverEventToList(Object sender, EventArgs args, ListenerList list) 
    at System.Windows.WeakEventManager.DeliverEvent(Object sender, EventArgs args) 
    at System.Collections.Specialized.CollectionChangedEventManager.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
    at System.Windows.Data.CollectionView.OnCollectionChanged(NotifyCollectionChangedEventArgs args) 
    at System.Windows.Data.ListCollectionView.RefreshOverride() 
    at System.Windows.Data.CollectionView.RefreshOrDefer() 
    at System.Windows.Data.ListCollectionView.ProcessCollectionChanged(NotifyCollectionChangedEventArgs args) 
    at System.Windows.Data.CollectionView.OnCollectionChanged(Object sender, NotifyCollectionChangedEventArgs args) 
    at System.Collections.ObjectModel.ObservableCollection`1.OnCollectionChanged(NotifyCollectionChangedEventArgs e) 
    at System.Collections.ObjectModel.ObservableCollection`1.ClearItems() 
    at System.Collections.ObjectModel.Collection`1.Clear() 
    at CocoonV4.Modules.CocoonViewsVMs.ManageTemplateDekocastViewModel.SetAllowedDekos(IEnumerable`1 _NewAllowedDekos) in C:\Users\lkottmann\Documents\Visual Studio 2010\Projects\IAV-Cocoon\MAIN\CocoonV4\CocoonV4.Modules.RunningItem\CocoonViewsVMs\ManageTemplateDekocastViewModel.cs:line 83 
    at CocoonV4.Modules.CocoonViewsVMs.ManageTemplateDekocastViewModel.LoadTemplates(Nullable`1 param) in C:\Users\lkottmann\Documents\Visual Studio 2010\Projects\IAV-Cocoon\MAIN\CocoonV4\CocoonV4.Modules.RunningItem\CocoonViewsVMs\ManageTemplateDekocastViewModel.cs:line 128 
    at Microsoft.Practices.Prism.Commands.DelegateCommand`1.<>c__DisplayClass6.<.ctor>b__2(Object o) 
    at Microsoft.Practices.Prism.Commands.DelegateCommandBase.Execute(Object parameter) 
    at Microsoft.Practices.Prism.Commands.DelegateCommandBase.System.Windows.Input.ICommand.Execute(Object parameter) 
    at System.Windows.Interactivity.InvokeCommandAction.Invoke(Object parameter) 
    at System.Windows.Interactivity.TriggerBase.InvokeActions(Object parameter) 
    at System.Windows.Interactivity.EventTriggerBase.OnEvent(EventArgs eventArgs) 
    at System.Windows.Interactivity.EventTriggerBase.OnEventImpl(Object sender, EventArgs eventArgs) 
    at System.Windows.Controls.SelectionChangedEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e) 
    at System.Windows.Controls.ComboBox.OnSelectionChanged(SelectionChangedEventArgs e) 
    at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems) 
    at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() 
    at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(Object item, Boolean assumeInItemsCollection) 
    at System.Windows.Controls.ComboBox.NotifyComboBoxItemMouseUp(ComboBoxItem comboBoxItem) 
    at System.Windows.Controls.ComboBoxItem.OnMouseLeftButtonUp(MouseButtonEventArgs e) 
    at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e) 
    at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
    at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent) 
    at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e) 
    at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget) 
    at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target) 
    at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs) 
    at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) 
    at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) 
    at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args) 
    at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted) 
    at System.Windows.Input.InputManager.ProcessStagingArea() 
    at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input) 
    at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport) 
    at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel) 
    at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) 
    at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 
    at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) 
    at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) 
    at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) 
    at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) 
    at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 
    at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) 
    at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
    at System.Windows.Application.RunDispatcher(Object ignore) 
    at System.Windows.Application.RunInternal(Window window) 
    at System.Windows.Application.Run(Window window) 
    at System.Windows.Application.Run() 
    at CocoonV4.App.Main() in C:\Users\lkottmann\Documents\Visual Studio 2010\Projects\IAV-Cocoon\MAIN\CocoonV4\CocoonV4\obj\x86\Debug\App.g.cs:line 0 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

अधिक संपादित करें:

मैं पालन declarated एक भी ComboBox के लिए समस्या पृथक:

<custom:CComboBox x:Name="cmbFirstDko" 
        ItemsSource="{Binding Path=DataContext.MyFirstCollection, ElementName=myViewName}" 
        SelectedValue="{Binding anObjectID, Mode=TwoWay}" 
        SelectedValuePath="anObjectID"> 

कृपया ध्यान दें कि anObjectID संपत्ति SelectedValue में ListView के डेटाकॉन्टेक्स्ट से आता है (यह कॉम्बोबॉक्स उस सूची दृश्य के किसी आइटम का हिस्सा प्रदर्शित कर रहा है), जबकि SelectedValuePath में एक संपत्ति को संदर्भित करता है कि प्रत्येक आइटम DataContext.MyFirstCollection में है।

यह मुझे ListView के प्रत्येक तत्व के लिए एक अलग कॉम्बोबॉक्स इटैम चुनने की अनुमति देता है।

मुझे आशा है कि मेरी स्पष्टीकरण स्पष्ट है, अन्यथा मुझे टिप्पणियों में बताएं और मैं स्पष्टीकरण देने की कोशिश करूंगा।

निर्णायक: comboboxes उनके DataContext {DisconnectedItem} पर सेट, इस तथाकथित SentinelObjects जो हस्तक्षेप कब वहां 2 संग्रह के बीच एक बाध्यकारी, मेरे उदाहरण के रूप में के काम का परिणाम है।

यह वास्तव में सरकारी तौर पर ढांचे की एक बग है, और यह तय किया गया है अगले संस्करण के लिए, इस MS Bug Page

किसी को भी एक समाधान का पता है देखते हैं?

संपादित करें: वहाँ एक जवाब मेरे ब्लॉग को जोड़ने हुआ करता था, लेकिन जब मैं इसे स्थानांतरित लोग जवाब downvoted। मैं इसके बारे में बीमार हो गया, इसलिए जब यह तैयार हो जाए तो यह वापस आ जाएगा।

+0

मैं 'ऑब्जर्जेबल कोलेक्शन' गुणों पर एक सेटटर की अनुशंसा नहीं करता, मैं आमतौर पर 'रीडोनली' फ़ील्ड और केवल एक गेटटर वाली संपत्ति का उपयोग करता हूं। –

+0

क्या आप पोस्ट कर सकते हैं कि यह RaisePropertyChanged कैसे लागू किया गया है?यह एक स्ट्रिंग क्यों नहीं ले रहा है? – MBen

+0

@MBen: MyObsCollectionProperty एक स्ट्रिंग है। –

उत्तर

2
  1. राइजप्रोपर्टी को कॉल करने की कोई आवश्यकता नहीं है MyObsCollection के लिए चेंज किया गया है, क्योंकि ऑब्जर्वेबल कोलेक्शन आंतरिक उपकरण INotifiyProperty बदल गया है।
  2. मुझे लगता है कि मान (सेट कॉल के दौरान) असाइन करते समय क्रैश हो रहा है, यह तब होगा जब आप किसी अन्य प्रकार के संग्रह के संग्रह को असाइन करने का प्रयास करेंगे। आप MyCustomType की वस्तु बनाने के लिए है और

MyObsCollection लिए यह बहुत अच्छा होगा अगर पूरे कोड या कोड का टुकड़ा पोस्ट कर सकता है, जहां यू आवंटित सीधे विभिन्न प्रकार के एक संग्रह बताए के बजाय MyObsCollection में जोड़ा जाना चाहिए या MyObsCollection

2

मुझे अभी भी एक ही समस्या है, और जब मैं ItemContainerStyle पर स्थैतिक-संसाधन शैली को हटा देता हूं और इसे सीधे सूची बॉक्स के नीचे परिभाषित करता हूं, तो समस्या दूर हो जाती है।

मेरे पास x था: डेटा टेम्पलेट में तत्वों पर नाम संपत्ति, और जब से मैंने उन्हें हटा दिया, तो संकलक ने इसे बिल्कुल भी नहीं बनाया। तो मैंने इसे समझने के लिए बिल्कुल एक रास्ता बदल दिया।

आशा है कि यह छोटी चाल आपकी मदद करेगी!

संबंधित मुद्दे