I have some methods that I x:bind to, to set element size based on the size of the host control. All works well except when binding a function to the Margin property in my user control (from style dictionary) I am getting a System.AccessViolationException.
I do not get the exception if binding to the element directly -just when used in a style setter. Obviously can work around this by setting the property on the xaml for the object but would like to know if this is a bug or if I am missing something? The function binding from the dictionary works with all other properties that I am using (just not on Margin). Any advice or guidance would be appreciated.
XAML Snippet:
<UserControl.Resources>
<ResourceDictionary>
<Style TargetType="Path" x:Key="Ball.Shadow">
<Setter Property="Height" Value="{x:Bind DimensionAsFraction(0.5)}" />
...
</Style>
<Style TargetType="TextBlock" x:Key="Ball.Letter" BasedOn="{StaticResource Ball.Text}">
<Setter Property="Margin" Value="{x:Bind MarginAsFraction(0.075), Mode=OneWay}" />
...
</style>
object.Height is the height of the host control - this has been set
before the component is initialised and works fine when referenced
from other code and style setters.
Code Snippet:
public sealed partial class Example : UserControl
{
private void SetDefaults()
{
Height = 200;
Width = 200;
}
private int calculateAsInt(double fraction) => (int)(object.Height * fraction);
private double DimensionAsFraction(double fraction) => calculateAsInt(fraction);
private Thickness MarginAsFraction(double fraction) => new Thickness(0, calculateAsInt(fraction), 0, 0);
private Thickness BorderAsFraction(double fraction) => new Thickness(calculateAsInt(fraction));
public Example()
{
SetDefaults();
this.InitializeComponent();
}
}
Thanks :D
question from:
https://stackoverflow.com/questions/65837940/uwp-style-xbinding-of-margin-to-a-function-throws-a-system-accessviolationexcep 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…