xamarin/Xamarin.Forms

[Bug] Control with DataTemplate can only be added to a page once

Aperta

#6476 aperta il 10 giu 2019

 (5 commenti) (0 reazioni) (0 assegnatari)C# (1926 fork)batch import
e/5 :clock5:help wantedinactivet/bug :bug:up-for-grabs

Metriche repository

Star
 (5644 stelle)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

Description

When defining a control - and instantiating a new DataTemplate with it new DataTemplate(() => control) in code behind the content created by createContent() can only be added once to the page. If it was already there it won't create a new control object and add it.

Steps to Reproduce

  1. Define a control
  2. Create a Button with a clicked event handler just for testing.
  3. In the clicked event handler instantiate a DataTemplate with the control
  4. call createContent and add the content to the page

Expected Behavior

The control should be added to the page each time the button is clicked.

Actual Behavior

The control will only be added once. If the control was defined in xaml in the page - given to the DataTemplate with x:name- no content created by createContent() gets added.

Basic Information

  • Version with issue: 4.0.0.425677
  • Last known good version: Unknown
  • IDE: Visual Studio Community 2019 (Preview) for Mac Version 8.1 Preview (8.1 build 2697)
  • Platform Target Frameworks:
    • iOS: Version: 12.10.0.153
    • Android: Untested
    • UWP: Untested
  • Android Support Library Version: Not used
  • Nuget Packages: Xamarin.Essentials(1.1.0). Xamarin.Forms(4.0.0.425677)
  • Affected Devices: iPhone 6s simulator. Other not yet tested.

Reproduction Link

https://github.com/Lelelo1/DataTemplateReproduce

More Info

Content created by a DataTemplate that is defined in xaml can successfully be added to the page. The Id in Xamarin.Forms.Element of this content is unique - while for the content from DataTemplate created in code behind - stays the same. It did not help to change the _id field with reflection prior to adding the content to the page.

Guida contributor