NineSlice control does not immediately work correctly.
Posted: 26 Oct 2020, 22:18
I created a nine slice control, but it has odd behavior. When you first start the application, either through Unity's editor or a standalone, the control will not have any brushes applied. However, if you exit out of Unity's play mode and go back in (or load another nine slice control) it will then start to work. In the editor's case, it will work until after the domain (?) is reloaded (usually from compiling).
It is similar to what is shown in the Images documentation, but this only takes an image source and not individual brushes.
It seems that the image source is not applying when the application is started. Only after I recreate a nine slice control (or exit out of play mode and go back in) does it work correctly.
Here is a WPF example that uses the control I made. Not sure why it throws an exception in the designer, but running the control shows it works:
It is similar to what is shown in the Images documentation, but this only takes an image source and not individual brushes.
Code: Select all
<Grid x:Name="Root">
<Grid.Resources>
<Style TargetType="Rectangle">
<Setter Property="SnapsToDevicePixels" Value="True" />
<Setter Property="IsHitTestVisible" Value="True" />
<Setter Property="RenderOptions.BitmapScalingMode" Value="NearestNeighbor" />
</Style>
<ImageBrush
x:Key="TLeft"
ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
ViewboxUnits="Absolute" />
<ImageBrush
x:Key="T"
ImageSource="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=ImageSource}"
ViewboxUnits="Absolute" />
... Repeat
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="{Binding BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="{Binding BorderThickness.Right, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="{Binding BorderThickness.Top, RelativeSource={RelativeSource TemplatedParent}}" />
<RowDefinition Height="*" />
<RowDefinition Height="{Binding BorderThickness.Bottom, RelativeSource={RelativeSource TemplatedParent}}" />
</Grid.RowDefinitions>
<Rectangle
x:Name="TopLeft"
Grid.Row="0"
Grid.Column="0"
Fill="{StaticResource TLeft}" />
<Rectangle
x:Name="TopCenter"
Grid.Row="0"
Grid.Column="1"
Fill="{StaticResource T}" />
... Repeat
<Grid
x:Name="Middle"
Grid.Row="1"
Grid.Column="1"
IsHitTestVisible="True">
<Rectangle Fill="{StaticResource C}" IsHitTestVisible="True" />
<ContentPresenter x:Name="MiddleContent" Margin="{TemplateBinding Padding}" />
</Grid>
</Grid>Here is a WPF example that uses the control I made. Not sure why it throws an exception in the designer, but running the control shows it works: