Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C# Markup] TemplateBind to PathFigure.StartPoint and LineSegment.Point crashes #2406

Closed
MartinZikmund opened this issue Jul 2, 2024 Discussed in #2405 · 1 comment
Closed
Assignees

Comments

@MartinZikmund
Copy link
Member

Discussed in #2405

Originally posted by kucint July 2, 2024
Create a simple Control that uses Path as Template:
Path is just a simple line between two points that are represented by dependency properties: StartHotspot and EndHotspot

public MyControl()
{
    DefaultStyleKey = typeof(MyControl);

    Style = new Style<MyControl>().Setters(s => s.Template(
        ctrl => new Path()
        .Stroke(Colors.Blue)
        .StrokeThickness(3)
        .Data(new PathGeometry().Figures(new PathFigureCollection()
        {
            new PathFigure()
            //.StartPoint(new Point(10, 30))                                // works fine
            .StartPoint(x => x.TemplateBind(() => ctrl.StartHotspot))       // crashes
            .Segments(new PathSegmentCollection()
            {
                new LineSegment()
                //.Point(new Point(80, 50))                                 // works fine
                .Point(x => x.TemplateBind(() => ctrl.EndHotspot))          // crashes
            })
        })))
    );
}

#region StartHotspot
public Point StartHotspot
{
    get { return (Point)GetValue(StartHotspotProperty); }
    set { SetValue(StartHotspotProperty, value); }
}

public static readonly DependencyProperty StartHotspotProperty =
    DependencyProperty.Register(
        nameof(StartHotspot),
        typeof(Point),
        typeof(MyControl),
        new PropertyMetadata(new Point(0, 0)));
#endregion

#region EndHotspot
public Point EndHotspot
{
    get { return (Point)GetValue(EndHotspotProperty); }
    set { SetValue(EndHotspotProperty, value); }
}

public static readonly DependencyProperty EndHotspotProperty =
    DependencyProperty.Register(
        nameof(EndHotspot),
        typeof(Point),
        typeof(MyControl),
        new PropertyMetadata(new Point(0, 0)));
#endregion

The control is now consumed in a frame:

public MainPage() => this
    .DataContext(new BindableMainPageModel(), (page, vm) => page
    .Background(ThemeResource.Get<Brush>("ApplicationPageBackgroundThemeBrush"))
    .Content(new StackPanel()
    .VerticalAlignment(VerticalAlignment.Center)
    .HorizontalAlignment(HorizontalAlignment.Center)
    .Children(
        new TextBlock()
        .Text("Hello Uno Platform!"),

        // implementing Path directly works fine:
        new Path()
        .Stroke(Colors.DarkGreen)
        .StrokeThickness(3)
        .Data(new PathGeometry().Figures(new PathFigureCollection()
        {
            new PathFigure()
            .StartPoint(new Point(10,10))
            .Segments(new PathSegmentCollection() {
                new LineSegment()
                .Point(new Point(80,30))
            })
        })),

        // implementing Path bound to MVU model works fine:
        new Path()
        .Stroke(Colors.HotPink)
        .StrokeThickness(3)
        .Data(new PathGeometry().Figures(new PathFigureCollection()
        {
            new PathFigure()
            .StartPoint(() => vm.StartPoint)
            .Segments(new PathSegmentCollection() {
                new LineSegment()
                .Point(() => vm.EndPoint)
            })
        })),

         // implementing Path inside MyControl bound to control's properties fails:
        new MyControl()
        .StartHotspot(new Point(10, 30))
        .EndHotspot(new Point(80, 50))
    )));

Environment:

  "Uno.Sdk": "5.2.175"

<TargetFrameworks>
  net8.0-windows10.0.19041;
  net8.0-desktop;
</TargetFrameworks>
  • under net8.0-desktop; following exception is thrown:
    Exception thrown: 'System.InvalidCastException' in Uno.Extensions.Markup.dll
    Unable to cast object of type 'Microsoft.UI.Xaml.Media.PathFigure' to type 'Microsoft.UI.Xaml.FrameworkElement'.

  • under net8.0-windows10.0.19041; following exception is thrown:
    No installed components were detected.
    Cannot apply a Style with TargetType 'Microsoft.UI.Xaml.Controls.ControlTemplate' to an object of type 'UnoPathApp.Controls.MyControl'.

    Note that the following fix has been applied:
    Setting styles in templated control causes control to crash under windows #2316

repro: UnoPathApp.zip

@kazo0
Copy link
Contributor

kazo0 commented Jul 13, 2024

This should be fixed in the latest -dev packages for markup and will be in the next stable release.

Let us know if you are still having issues after updating!

cc: @MartinZikmund

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants