This is a migrated thread and some comments may be shown as answers.

RadSlider is throwing a Invalid Operation Exception when used in MAF (System.Addin)

1 Answer 118 Views
Slider
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 24 Nov 2010, 04:51 AM

I am trying to use the Telerik reporting inside a System.Addin control. My control has a separate appdomain and thread then the main form. This is done to isolate the main control from any addins that could cause issues. The RadSlider is grabbing a reference to the mainwindow which is outside of my appdomain and is on a different thread. Is there a way to remove the reference to the main window or set it to the addin control?

System.Windows.Markup.XamlParseException occurred
 
Message=Cannot create instance of 'RadSlider' defined in assembly 'Telerik.Windows.Controls, Version=2010.3.1110.35, Culture=neutral, PublicKeyToken=5803cfa389c90ce7'. Exception has been thrown by the target of an invocation.  Error at object 'System.Windows.Controls.StackPanel' in markup file 'Telerik.ReportViewer.Wpf;component/Themes/Default/ReportViewer.xaml'.
 
Source=PresentationFramework
 
LineNumber=0
 
LinePosition=0
 
NameContext=87_T
 
StackTrace:
       at
System.Windows.Markup.XamlParseException.ThrowException(String message, Exception innerException, Int32 lineNumber, Int32 linePosition, Uri baseUri, XamlObjectIds currentXamlObjectIds, XamlObjectIds contextXamlObjectIds, Type objectType)
       at
System.Windows.Markup.XamlParseException.ThrowException(ParserContext parserContext, Int32 lineNumber, Int32 linePosition, String message, Exception innerException)
       at
System.Windows.Markup.BamlRecordReader.ThrowExceptionWithLine(String message, Exception innerException)
       at
System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
       at
System.Windows.Markup.BamlRecordReader.GetElementAndFlags(BamlElementStartRecord bamlElementStartRecord, Object& element, ReaderFlags& flags, Type& delayCreatedType, Int16& delayCreatedTypeId)
       at
System.Windows.Markup.BamlRecordReader.BaseReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
       at
System.Windows.Markup.BamlRecordReader.ReadElementStartRecord(BamlElementStartRecord bamlElementRecord)
       at
System.Windows.Markup.BamlRecordReader.ReadRecord(BamlRecord bamlRecord)
       at
System.Windows.StyleHelper.LoadOptimizedTemplateContent(DependencyObject container, ParserContext parserContext, OptimizedTemplateContent optimizedTemplateContent, FrameworkTemplate frameworkTemplate, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
       at
System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)
       at
System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)
       at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`
1 templateDataField, FrameworkElement container)
       at
System.Windows.FrameworkElement.ApplyTemplate()
       at
System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at
System.Windows.UIElement.Measure(Size availableSize)
       at MS
.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
       at
System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
       at
System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at
System.Windows.UIElement.Measure(Size availableSize)
       at
System.Windows.Controls.Border.MeasureOverride(Size constraint)
       at
System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at
System.Windows.UIElement.Measure(Size availableSize)
       at
System.Windows.Controls.Control.MeasureOverride(Size constraint)
       at
System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at
System.Windows.UIElement.Measure(Size availableSize)
       at
System.Windows.Controls.DockPanel.MeasureOverride(Size constraint)
       at
System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at
System.Windows.UIElement.Measure(Size availableSize)
       at
System.Windows.Controls.Decorator.MeasureOverride(Size constraint)
       at
System.Windows.Documents.AdornerDecorator.MeasureOverride(Size constraint)
       at
System.Windows.FrameworkElement.MeasureCore(Size availableSize)
       at
System.Windows.UIElement.Measure(Size availableSize)
       at
System.Windows.Interop.HwndSource.SetLayoutSize()
       at
System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
       at
System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
       at
System.Windows.Forms.Integration.ElementHost.OnHandleCreated(EventArgs e)
       at
System.Windows.Forms.Control.WmCreate(Message& m)
       at
System.Windows.Forms.Control.WndProc(Message& m)
       at
System.Windows.Forms.Integration.ElementHost.WndProc(Message& m)
       at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at
System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at
System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at
System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at
System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at
System.Windows.Forms.Control.CreateHandle()
       at
System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at
System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at
System.Windows.Forms.Control.CreateControl()
       at
System.Windows.Forms.Control.WmShowWindow(Message& m)
       at
System.Windows.Forms.Control.WndProc(Message& m)
       at
System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at
System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at
System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at
System.Windows.Forms.UnsafeNativeMethods.IntCreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at
System.Windows.Forms.UnsafeNativeMethods.CreateWindowEx(Int32 dwExStyle, String lpszClassName, String lpszWindowName, Int32 style, Int32 x, Int32 y, Int32 width, Int32 height, HandleRef hWndParent, HandleRef hMenu, HandleRef hInst, Object pvParam)
       at
System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
       at
System.Windows.Forms.Control.CreateHandle()
       at
System.Windows.Forms.Control.get_Handle()
       at
System.Windows.Forms.Integration.WindowsFormsHost.BuildWindowCore(HandleRef hwndParent)
       at
System.Windows.Interop.HwndHost.BuildWindow(HandleRef hwndParent)
       at
System.Windows.Interop.HwndHost.BuildOrReparentWindow()
       at
System.Windows.Interop.HwndHost.OnSourceChanged(Object sender, SourceChangedEventArgs e)
       at
System.Windows.SourceChangedEventArgs.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.PresentationSource.UpdateSourceOfElement(DependencyObject doTarget, DependencyObject doAncestor, DependencyObject doOldParent)
       at
System.Windows.PresentationSource.RootChanged(Visual oldRoot, Visual newRoot)
       at
System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
       at
System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
       at
System.AddIn.Pipeline.FrameworkElementAdapters.ViewToContractAdapter(FrameworkElement root)
       at DRIP
.AddIn.Adapter.DRIPAddInViewToContractAdapter.UrlHandler(String uri)
 
InnerException: System.Reflection.TargetInvocationException
       
Message=Exception has been thrown by the target of an invocation.
       
Source=mscorlib
       
StackTrace:
            at
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
            at
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
            at
System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
            at
System.Activator.CreateInstance(Type type, Boolean nonPublic)
            at
System.Windows.Markup.BamlRecordReader.CreateInstanceFromType(Type type, Int16 typeId, Boolean throwOnFail)
       
InnerException: System.InvalidOperationException
           
Message=The calling thread cannot access this object because a different thread owns it.
           
Source=WindowsBase
           
StackTrace:
                 at
System.Windows.Threading.Dispatcher.VerifyAccess()
                 at
System.Windows.Threading.DispatcherObject.VerifyAccess()
                 at
System.Windows.Application.get_MainWindow()
                 at
Telerik.Windows.Controls.RadSlider..ctor() in c:\Builds\WPF_Scrum\Release_WPF_2010_Q3\Sources\Development\Core\Controls\Slider\RadSlider.cs:line 217
           
InnerException:

1 Answer, 1 is accepted

Sort by
0
Kiril Stanoev
Telerik team
answered on 24 Nov 2010, 04:21 PM
Hi Adam,

To be honest, I am not really sure what is causing this issue. However, let me throw some shots in the dark.

1. Are you able to reproduce this issue if you use a regular WPF slider?

2. Until last week, RadSlider had this piece of code in its constructor:

if (Application.Current != null && Application.Current.MainWindow != null)
{
    Application.Current.MainWindow.SizeChanged += (o, e) =>
    {
        this.Dispatcher.BeginInvoke(new Action(() =>
        {
            if (this.XamlInitialized)
            {
                this.UpdateTrackLayout();
                this.UpdateTickBarMargins();
            }
        }), DispatcherPriority.ApplicationIdle);
 
    };
}

However, this was causing problems and this week we changed it to:

this.parentWindow = Window.GetWindow(this);
if (this.parentWindow != null)
{
    this.parentWindow.SizeChanged += this.ParentWindow_SizeChanged;
}

private void ParentWindow_SizeChanged(object sender, SizeChangedEventArgs e)
{
    this.Dispatcher.BeginInvoke(new Action(() =>
    {
        if (this.XamlInitialized)
        {
            this.UpdateTrackLayout();
            this.UpdateTickBarMargins();
        }
    }), DispatcherPriority.ApplicationIdle);
}

We are using this to ensure that the ticks are correctly redrawn. You can try using the Dispatcher.CheckAccess() method.
This fix might help you, so I'd like to ask you if you can test your scenario with this week's internal build and let us know if you still experience the same issue.

Let me know if you manage to find more on the topic.

Greetings,
Kiril Stanoev
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
Slider
Asked by
Adam
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Share this question
or