xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

System Access Violation Exception is thrown in Forms.UWP while using ListView #3858

Open PavithraSyncfusion opened 6 years ago

PavithraSyncfusion commented 6 years ago

Hi Team,

We at Syncfusion develop data visualization controls for Xamarin and we have more than 90 controls spread across all platforms of Xamarin. Please refer the below link for a description about us.

UserGuide: https://help.syncfusion.com/xamarin/sfdatagrid/overview FeatureTour: https://www.syncfusion.com/products/xamarin

This is to bring to your notice an exception when trying to show the Xamarin.Forms.ListView inside a template of the Syncfusion's SfPopupLayout.

The SfPopupLayout control can host any view inside it and display the view as a Popup. This is achieved by setting the desired view as a DataTemplate. For more information refer the below link :

Also SfPopupLayout is a control that is mapped from native projects of each platforms. XForms.Android is mapped from Xamarin.Android , Xamarin.Forms.iOS from Xamarin.iOS and Xamarin.Forms.UWP from native UWP.

Hence we will get the data template from the user and convert this Forms view to the necessary native view based on the platform and then set this converted view as the child of the Native UWP popup.

So each time a popup is opened this conversion happens and the popup is shown. You can see the conversion codes below.

`internal static FrameworkElement ConvertFormsToNativeView(Xamarin.Forms.View view, formsNamespace.SfPopupLayout formsPopupLayout, int width, int height) { FrameworkElement native = null; var templateLayout = view as Xamarin.Forms.View; templateLayout.Parent = formsPopupLayout; var renderer = ConvertToNative(templateLayout, formsPopupLayout); native = renderer as FrameworkElement; var size = templateLayout.Measure(width, height); templateLayout.Layout(new Xamarin.Forms.Rectangle(0, 0, width, height)); return native; }

internal static IVisualElementRenderer ConvertToNative(Xamarin.Forms.View source, formsNamespace.SfPopupLayout formsPopupLayout) { if (source == null) { return null; } IVisualElementRenderer renderer = source.GetOrCreateRenderer(); var properties = typeof(Xamarin.Forms.VisualElement).GetRuntimeProperties(); foreach (var item in properties) { if (item.Name == "IsPlatformEnabled") { item.SetValue(source, true); } } properties = typeof(Element).GetRuntimeProperties(); foreach (var item in properties) { if (item.Name == "Platform") { if (formsPopupLayout.Content == null) { item.SetValue(source, item.GetValue(formsPopupLayout.GetMainPage())); } else { item.SetValue(source, item.GetValue(formsPopupLayout)); } } } return renderer; }`

In the below example that we have attached we have loaded a Xamarin.Forms.ListView inside the SfPopupLayout. Here we create a single instance of the ListView and pass this instance (template) to the conversion codes each time the popup is opened via a button click.

The popup opens and closes fine for the first time. Clicking on the same button again crashes the application with the below excpetion.

Stack trace:

[Managed to Native Transition] Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.VisualElementRenderer<Xamarin.Forms.ListView, Windows.UI.Xaml.FrameworkElement>.ArrangeOverride(Windows.Foundation.Size finalSize) Unknown [Native to Managed Transition]
[Managed to Native Transition]
Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.VisualElementRenderer<Xamarin.Forms.Layout, Windows.UI.Xaml.FrameworkElement>.ArrangeOverride(Windows.Foundation.Size finalSize) Unknown [Native to Managed Transition]
[Managed to Native Transition]
Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.PopupView.ArrangeOverride(Windows.Foundation.Size finalSize) Unknown [Native to Managed Transition]
[Managed to Native Transition]
Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.VisualContainer.ArrangeOverride(Windows.Foundation.Size finalSize) Unknown [Native to Managed Transition]
[Managed to Native Transition]
Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.PopupView.UpdatePopupView() Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.SfPopupLayout.DisplayPopup() Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.SfPopupLayout.IsOpen.set(bool value) Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.RendererHelper.MapSfPopupLayoutProperties(string propertyName, Syncfusion.XForms.PopupLayout.SfPopupLayout formsPopupLayout, Syncfusion.XForms.UWP.PopupLayout.SfPopupLayout nativePopupLayout) Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.SfPopupRenderer.OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) Unknown Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.OnPropertyChanged(string propertyName) Unknown Xamarin.Forms.Core.dll!Xamarin.Forms.Element.OnPropertyChanged(string propertyName) Unknown Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.SetValueActual(Xamarin.Forms.BindableProperty property, Xamarin.Forms.BindableObject.BindablePropertyContext context, object value, bool currentlyApplying, Xamarin.Forms.Internals.SetValueFlags attributes, bool silent) Unknown Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.SetValueCore(Xamarin.Forms.BindableProperty property, object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject.SetValuePrivateFlags privateAttributes) Unknown Xamarin.Forms.Core.dll!Xamarin.Forms.BindableObject.SetValue(Xamarin.Forms.BindableProperty property, object value, bool fromStyle, bool checkAccess) Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.SfPopupLayout.OpenOrClosePopup(bool open) Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.SfPopupLayout.Show() Unknown Syncfusion.SfPopupLayout.XForms.UWP.dll!Syncfusion.XForms.UWP.PopupLayout.PopupLayoutDependencyService.Show(object nativeObject, Syncfusion.XForms.PopupLayout.SfPopupLayout formsPopup) Unknown Syncfusion.SfPopupLayout.XForms.dll!Syncfusion.XForms.PopupLayout.SfPopupLayout.Show() Unknown

FrameWorkIssue.dll!FrameWorkIssue.MainPage.ClickToShowPopup_Clicked(object sender, System.EventArgs e) Line 26 C# Xamarin.Forms.Core.dll!Xamarin.Forms.Button.SendClicked() Unknown Xamarin.Forms.Platform.UAP.dll!Xamarin.Forms.Platform.UWP.ButtonRenderer.OnButtonClick(object sender, Windows.UI.Xaml.RoutedEventArgs e) Unknown

This issue however does not happen if we create a new instance of the list view each time. Having the same instance and trying to open the same only causes the issue. If we are not wrong, we understand that this issue is caused due to accessing a disposed object. But however we have not disposed the list view anywhere in our sample.

Steps to Reproduce:

  1. Run the attached sample.
  2. Click on "Click to show popup" button.
  3. Tap on "Accept" button in Popup.
  4. Then again click on ""Click to show popup" button.
  5. Note that "System.AccessViolation" exception has been thrown.

Expected Behavior: Popup must be open for second time.

Actual Behavior: "System.AccessViolation" exception has been thrown.

Basic Information: Xamarin. Forms version: 2.4.0.280 UWP Target version: Windows 10 Anniversary Edition (10.0;Build 14393) UWP Min version: Windows 10 (10.0;Build 10586)

Issue Reproducing video:

IssueReproducingVideo.zip

Reproduction Sample link: ListViewWithPopup.zip

Thanks, Pavithra S

PavithraSyncfusion commented 6 years ago

Hi Team,

Can you please update the status of this issue?

chrisweihu commented 5 years ago

Hi Xamarin Team, Many people have reported this System.AccessViolationException issue in latest Xamarin.Forms UWP. Not just in ListView but in many other places. I even got this exception when setting my BindingContext to NULL. As of today Jan 11, 2019. This issue is still not resolved! Can you please take a look at this bug ! The lastest Xamarin.Forms on UWP is totally un-usable!

chrisweihu commented 5 years ago

I pin pointed the code that causes this exception. Frame.OutlineColor API is deprecated. Once I removed it, project works fine. But now I don't have outline in Frame, the BorderColor API will also cause this exception even though it is not deprecated.