1

I have an issue with a Xamarin.Forms application I am currently working on. The resource I have created is being used within multiple controls, the controls are using the same object to render out onto the screen. I thought using the x:Shared="False" attribute (as per https://learn.microsoft.com/en-us/dotnet/framework/xaml-services/x-shared-attribute) would get around this. It does not :( Any ideas how to create the same object in .xaml without having to define multiple resources?

<Grid.Resources>
 <ResourceDictionary>
   <SharedShapes:Circle x:Shared="False" x:Key="SpecCircle" Color="{StaticResource AccentColor}"/>
 </ResourceDictionary>
</Grid.Resources>

<SharedControls:ImageButton Grid.Row="0" Grid.Column="0" Shape="{StaticResource SpecCircle}"...
<SharedControls:ImageButton Grid.Row="1" Grid.Column="0" Shape="{StaticResource SpecCircle}"...
.
.
.

Many thanks in advance!

  • I don't believe this is supported in forms. In the link you mentioned - it talks about WPF framework - `When set to false, modifies WPF resource-retrieval behavior so that requests for the attributed resource ..` – Sharada Gururaj Nov 17 '17 at 17:48

2 Answers2

1

The link you mentioned talks primarily about WPF framework - I don't think x:Shared is supported in Xamarin forms.

To avoid having to declare shape multiple times - you can make use of DataTemplate, and register that as a StaticResource.

And, modify/extend ImageButton to add a bindable property of type DataTemplate, which when set inflates the template into Shape object using CreateContent() method.

public DataTemplate ShapeTemplate
{
    get { return (DataTemplate)GetValue(ShapeTemplateProperty); }
    set { SetValue(ShapeTemplateProperty, value); }
}

public static readonly BindableProperty ShapeTemplateProperty = BindableProperty.Create(
    nameof(ShapeTemplate),
    typeof(DataTemplate),
    typeof(ImageButton),
    propertyChanged: (bObj, oldValue, newValue) =>
    {
        var view = bObj as ImageButton;
        if (view != null && newValue != null)
            view.Shape = (View)newValue.CreateContent();
    }
);

Sample usage

<Grid.Resources>
 <ResourceDictionary>
   <DataTemplate x:Key="SpecCircleTemplate">
      <SharedShapes:Circle Color="{StaticResource AccentColor}"/>
   </DataTemplate>
 </ResourceDictionary>
</Grid.Resources>

<SharedControls:ImageButton Grid.Row="0" Grid.Column="0" 
    ShapeTemplate="{StaticResource SpecCircleTemplate}"...
<SharedControls:ImageButton Grid.Row="1" Grid.Column="0"
    ShapeTemplate="{StaticResource SpecCircleTemplate}"...
Sharada Gururaj
  • 13,471
  • 1
  • 22
  • 50
0

That's not supported in Xamarin.Forms. To get a new instance you'd have to specify it via property element syntax using something like this:

<SharedControls:ImageButton Grid.Row="0" Grid.Column="0">
  <SharedControls:ImageButton.Shape>
    <SharedShapes:Circle x:Shared="False" x:Key="SpecCircle" Color="...."/>
  </SharedControls:ImageButton.Shape>
</SharedControls:ImageButton>
Krumelur
  • 32,180
  • 27
  • 124
  • 263