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

winrt::hresult_error crash when trying to use TeachingTip in ControlTemplate #8572

Closed
TekuSP opened this issue Jun 22, 2023 · 8 comments
Closed
Labels
bug Something isn't working

Comments

@TekuSP
Copy link

TekuSP commented Jun 22, 2023

Describe the bug

When developing custom TextBox control with TeachingTIp I hit barrier with exception:
image
Removing TeachingTip solves the issue... but I wanted that.
I am attaching source codes

Steps to reproduce the bug

Generic.xaml

<Style TargetType="controls:ValidatingTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:ValidatingTextBox">
                    <StackPanel>
                        <TextBox Text="{TemplateBinding Text}" PlaceholderText="{TemplateBinding PlaceholderText}" Header="{TemplateBinding Header}" IsReadOnly="{TemplateBinding IsReadOnly}"/>
                        <TeachingTip PreferredPlacement="Right" Target="{Binding}" ShouldConstrainToRootBounds="False" Title="{TemplateBinding Title}" Subtitle="{TemplateBinding Subtitle}" IconSource="{TemplateBinding Symbol}" IsOpen="{TemplateBinding IsOpen}" Background="OrangeRed"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

ValidatingTextBox.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Documents;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Osiris.UWP.Controls
{
    public sealed class ValidatingTextBox : Control
    {
        public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register(
            nameof(Text),
            typeof(string),
            typeof(ValidatingTextBox),
            new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register(
            nameof(Header),
            typeof(string),
            typeof(ValidatingTextBox),
            new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty PlaceholderTextProperty =
        DependencyProperty.Register(
            nameof(PlaceholderText),
            typeof(string),
            typeof(ValidatingTextBox),
            new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty IsReadOnlyProperty =
        DependencyProperty.Register(
            nameof(IsReadOnly),
            typeof(bool),
            typeof(ValidatingTextBox),
            new PropertyMetadata(false));

        public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register(
            nameof(Title),
            typeof(string),
            typeof(ValidatingTextBox),
            new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty SubtitleProperty =
        DependencyProperty.Register(
            nameof(Subtitle),
            typeof(string),
            typeof(ValidatingTextBox),
            new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty SymbolProperty =
        DependencyProperty.Register(
            nameof(Symbol),
            typeof(SymbolIconSource),
            typeof(ValidatingTextBox),
            new PropertyMetadata(new SymbolIcon(Microsoft.UI.Xaml.Controls.Symbol.Edit)));

        public static readonly DependencyProperty IsOpenProperty =
        DependencyProperty.Register(
            nameof(IsOpen),
            typeof(bool),
            typeof(ValidatingTextBox),
            new PropertyMetadata(false));
        public ValidatingTextBox()
        {
            this.DefaultStyleKey = typeof(ValidatingTextBox);
        }
        public string Text
        {
            get => (string)GetValue(TextProperty);
            set => SetValue(TextProperty, value);
        }
        public string Header
        {
            get => (string)GetValue(HeaderProperty);
            set => SetValue(HeaderProperty, value);
        }
        public string PlaceholderText
        {
            get => (string)GetValue(PlaceholderTextProperty);
            set => SetValue(PlaceholderTextProperty, value);
        }

        public bool IsReadOnly
        {
            get => (bool)GetValue(IsReadOnlyProperty);
            set => SetValue(IsReadOnlyProperty, value);
        }

        public string Title
        {
            get => (string)GetValue(TitleProperty);
            set => SetValue(TitleProperty, value);
        }

        public string Subtitle
        {
            get => (string)GetValue(SubtitleProperty);
            set => SetValue(SubtitleProperty, value);
        }
        public SymbolIconSource Symbol
        {
            get => (SymbolIconSource)GetValue(SymbolProperty);
            set => SetValue(SymbolProperty, value);
        }
        public bool IsOpen
        {
            get => (bool)GetValue(IsOpenProperty);
            set => SetValue(IsOpenProperty, value);
        }

        public void Test()
        {
            Title = "Testing";
            Subtitle = "Testing";
            IsOpen = true;
        }
    }
}

Expected behavior

No crash, I want TeachingTip to display next to my TextBox element

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.3.2: 1.3.230602002

Windows version

Windows Insider Build (xxxxx)

Additional context

No response

@TekuSP TekuSP added the bug Something isn't working label Jun 22, 2023
@TekuSP
Copy link
Author

TekuSP commented Jun 23, 2023

I just wrote another control using Syncfusion library and it throws same error.
Writing only TextBoxes works tho.

This is real issue, custom controls are to make life easy, without them its going to be hell.

@TekuSP
Copy link
Author

TekuSP commented Jun 23, 2023

Probably dupe of #3657

@TekuSP
Copy link
Author

TekuSP commented Jun 23, 2023

Ignoring exception ends with
image

   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverridesMethods.MeasureOverride(IObjectReference _obj, Size availableSize)
   at Microsoft.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
   at Microsoft.UI.Xaml.FrameworkElement.Microsoft.UI.Xaml.IFrameworkElementOverrides.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverridesMethods.MeasureOverride(IObjectReference _obj, Size availableSize)
   at Microsoft.UI.Xaml.FrameworkElement.MeasureOverride(Size availableSize)
   at Microsoft.UI.Xaml.FrameworkElement.Microsoft.UI.Xaml.IFrameworkElementOverrides.MeasureOverride(Size availableSize)
   at ABI.Microsoft.UI.Xaml.IFrameworkElementOverrides.Do_Abi_MeasureOverride_0(IntPtr thisPtr, Size availableSize, Size* result)
--- End of stack trace from previous location ---
   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.ITabViewMethods.set_SelectedIndex(IObjectReference _obj, Int32 value)
   at Microsoft.UI.Xaml.Controls.TabView.set_SelectedIndex(Int32 value)
   at Osiris.UWP.Windows.MainScreen.<MenuPanel_SelectionChanged>d__26.MoveNext() in C:\Users\richa\source\repos\Osiris\Osiris.UWP\Osiris.UWP\Windows\MainScreen.xaml.cs:line 132```

@TekuSP
Copy link
Author

TekuSP commented Jun 23, 2023

If anyone is open with any workaround, I will accept them.

@DHancock
Copy link

The crashing issue is caused by your Symbol dependency property definition. You could change it to:

        public static readonly DependencyProperty SymbolProperty =
        DependencyProperty.Register(
            nameof(Symbol),
            typeof(IconSource),
            typeof(ValidatingTextBox),
            new PropertyMetadata(new SymbolIconSource() { Symbol = Microsoft.UI.Xaml.Controls.Symbol.Edit }));

        public IconSource Symbol
        {
            get => (IconSource)GetValue(SymbolProperty);
            set => SetValue(SymbolProperty, value);
        }

I don't think the teaching tip target binding is correct. I would give the text box a name and use it when binding the target:

   <Style TargetType="controls:ValidatingTextBox">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:ValidatingTextBox">
                    <StackPanel>
                        <TextBox x:Name="tb" Text="{TemplateBinding Text}" PlaceholderText="{TemplateBinding PlaceholderText}" Header="{TemplateBinding Header}" IsReadOnly="{TemplateBinding IsReadOnly}"/>
                        <TeachingTip PreferredPlacement="Right" Target="{Binding ElementName=tb}" ShouldConstrainToRootBounds="False" Title="{TemplateBinding Title}" Subtitle="{TemplateBinding Subtitle}" IconSource="{TemplateBinding Symbol}" IsOpen="{TemplateBinding IsOpen}" Background="OrangeRed"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

@TekuSP
Copy link
Author

TekuSP commented Jun 23, 2023

@DHancock You are absolutely right. Is there any way I could find out about it without external help?
I have issue with one more control as well.

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="controls:MinMaxAvgControl">
                <StackPanel>
                    <TextBlock Text="{TemplateBinding Label}"/>
                    <StackPanel Orientation="Horizontal">
                        <editors:SfNumberBox Header="Min" AllowNull="False" Minimum="{TemplateBinding MinLimit}" Maximum="{TemplateBinding MaxLimit}" IsEditable="True" UpDownPlacementMode="Inline" Value="{TemplateBinding Min}" Margin="10"/>
                        <controls:LogarithmicRangeSlider Minimum="{TemplateBinding MinLimit}" Maximum="{TemplateBinding MaxLimit}" RangeStart="{TemplateBinding Min}" RangeEnd="{TemplateBinding Max}" ShowLabels="True" ToolTipFormat="N0" ActiveTrackHeight="5" InactiveTrackHeight="5" Margin="10"/>
                        <editors:SfNumberBox Header="Max" AllowNull="False" Minimum="{TemplateBinding MinLimit}" Maximum="{TemplateBinding MaxLimit}" IsEditable="True" UpDownPlacementMode="Inline" Value="{TemplateBinding Max}" Margin="10"/>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal">
                        <editors:SfNumberBox Header="Avg" AllowNull="False" Minimum="{TemplateBinding Min}" Maximum="{TemplateBinding Max}" IsEditable="True" UpDownPlacementMode="Inline" Value="{TemplateBinding Avg}" Margin="10"/>
                        <sliders:SfSlider Minimum="{TemplateBinding Min}" Maximum="{TemplateBinding Max}" Value="{TemplateBinding Avg}" ShowLabels="True" ToolTipFormat="N0" ActiveTrackHeight="5" InactiveTrackHeight="5" Margin="10"/>
                    </StackPanel>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>```

It uses Syncfusion controls, do you have any idea what that could cause?

@DHancock
Copy link

I cannot tell from just the xaml. I think the correct thing to do would be to close this bug, then post a question in the discussions section, preferably including a min repro project.

@TekuSP TekuSP closed this as completed Jun 23, 2023
@TekuSP
Copy link
Author

TekuSP commented Jun 23, 2023

@DHancock Done as #8580

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants