Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

c# - UWP Style X:binding of margin to a function throws a System.AccessViolationException. Any ideas why, when this works fine if set in inline xaml?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UWP Style X:binding of margin to a function throws a System.AccessViolationException. Any ideas why, when this works fine if set in inline xaml?

I'm afraid you can't use binding or x:bind in the style setter, derive from official document

Windows Presentation Foundation (WPF) and Microsoft Silverlight supported the ability to use a Binding expression to supply the Value for a Setter in a Style. The Windows Runtime doesn't support a Binding usage for Setter.Value (the Binding won't evaluate and the Setter has no effect, you won't get errors, but you won't get the desired result either). When you convert XAML styles from Windows Presentation Foundation (WPF) or Microsoft Silverlight XAML, replace any Binding expression usages with strings or objects that set values, or refactor the values as shared {StaticResource} markup extension values rather than Binding -obtained values.

For this scenario, we suggest set margin in the control directly

<TextBlock Text="Hello World" Margin="{x:Bind MarginAsFraction(0.75)}" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...