"Invalid cross-thread access" exception thrown when assigning value to Image.Source

davidm.123davidm.123 ✭✭USUniversity ✭✭

When I attempt to set the Source property on an Image element, an UnauthorizedAccessException exception is thrown. The message is "Invalid cross-thread access." and this is the stack trace:

at MS.Internal.XcpImports.CheckThread()
at System.Windows.DependencyObject..ctor(UInt32 nativeTypeIndex, IntPtr constructDO)
at System.Windows.Media.Imaging.BitmapImage..ctor()
at Xamarin.Forms.Platform.WinPhone.StreamImagesourceHandler.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Xamarin.Forms.Platform.WinPhone.ImageRenderer.d__0.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.b__3(Object state)

This is in a Xamarin.Forms project targetting the Windows Phone 8 framework. My Xamarin.Forms projects for iOS and Android do not have this issue.

This is XAML for the Image element:

This is the class for that XAML:

public partial class MyView
{
    public MyView()
    {
        InitializeComponent();
        ImageSource s = ImageSource.FromResource("Hello.jpg");
        this.theImage.Source = s;
    }
}

I have even tried wrapping the code in a call to Device.BeginInvokeOnMainThread (as shown below) but the same exception is still being thrown.

        Device.BeginInvokeOnMainThread(() =>
        {
            ImageSource s = ImageSource.FromResource("Hello.jpg");
            this.theImage.Source = s;
        });

Does anyone have any idea what's going on or how to solve this?

Posts

Sign In or Register to comment.