CommunityToolkit / WindowsCommunityToolkit

The Windows Community Toolkit is a collection of helpers, extensions, and custom controls. It simplifies and demonstrates common developer tasks building .NET apps with UWP and the Windows App SDK / WinUI 3 for Windows 10 and Windows 11. The toolkit is part of the .NET Foundation.
https://docs.microsoft.com/windows/communitytoolkit/
Other
5.9k stars 1.37k forks source link

AdaptiveGridView throws argument exception #3142

Open ranaprathap2 opened 4 years ago

ranaprathap2 commented 4 years ago

Upon resizing the control the adaptive grid view throws argument exception.

This happens when toggling the visibility of a control using the adaptive grid view. On some cases theCalculateItemWidth method returns an invalid Item width.

[Update] var columns = CalculateColumns(containerWidth, DesiredWidth); CalculateColumns method returns an invalid value (negative value) when DesiredWidth value is 0, But still can't get enough clue why the desired width is set to 0.

StackTrace is as below System.ArgumentException: Value does not fall within the expected range. at Windows.UI.Xaml.DependencyObject.SetValue(DependencyProperty dp, Object value) at Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.RecalculateLayout(Double containerWidth) at Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.OnSizeChanged(Object sender, SizeChangedEventArgs e)

ghost commented 4 years ago

Hello ranaprathap2, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will investigate the issue and help solve it ASAP. Other community members may also look into the issue and provide feedback 🙌

ghost commented 4 years ago

Thanks for submitting a new feature request! I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future!

Kyaa-dost commented 4 years ago

@ranaprathap2 I am not able to reproduce this. Can you please provide screenshot or show the detailed process so we can see how you are receiving this error?

michael-hawker commented 4 years ago

@ranaprathap2 could you maybe provide a small repro project that shows there error? Or is it possible to get the Sample App to hit this scenario, that's the easiest way for us to test issues and fixes?

Kyaa-dost commented 4 years ago

@ranaprathap2 Please let us know when you have an update on this.

michael-hawker commented 4 years ago

@ranaprathap2 can you clarify what Toolkit and OS versions you are on? I thought we fixed some issues in a similar sounding area in the last release?

ranaprathap2 commented 4 years ago

OS Name Microsoft Windows 10 Pro Version 10.0.18362 Build 18362

@michael-hawker

Kyaa-dost commented 3 years ago

@ranaprathap2 is this issue still persistent?

teohumeau commented 3 years ago

This is happening to me too, here is my AppCenter report from a 10.0.19043 OS.

System.Runtime.InteropServices McgMarshal.ThrowOnExternalCallFailed (Int32, RuntimeTypeHandle) Interop ComCallHelpers.Call ($ComObject this, RuntimeTypeHandle typeHnd, Int32 targetIndex, Void arg0, Void arg1) Call at 15732480 System.Canon> (Void InstParam, $ComObject this, Canon dp, Object value, Int32 targetIndex) Stub12 at 16707566 Microsoft.Toolkit.Uwp.UI.Controls AdaptiveGridView.RecalculateLayout (Double containerWidth) //Microsoft.Toolkit.Uwp.UI.Controls.Primitives/AdaptiveGridView/AdaptiveGridView.cs at 264:17 Microsoft.Toolkit.Uwp.UI.Controls AdaptiveGridView.OnSizeChanged (Object sender, $SizeChangedEventArgs e) /_/Microsoft.Toolkit.Uwp.UI.Controls.Primitives/AdaptiveGridView/AdaptiveGridView.cs at 176:17 System.Xml.Serialization XmlNodeEventHandler.Invoke (Object sender, $XmlNodeEventArgs e) Invoke at 16707566 System.__Canon> (Void InstParam, Object this, IntPtr pfn, Canon arg0, Canon arg1) Interop.ReverseComStubs Stub_6 (Void InstParam, Object __this, Void unsafe_sender, Void unsafe_e, IntPtr methodPtr) Stub_6 at 16707566 System.Runtime.InteropServices McgMarshal.ThrowOnExternalCallFailed (Int32, RuntimeTypeHandle) Interop ComCallHelpers.Call ($ComObject this, RuntimeTypeHandle typeHnd, Int32 targetIndex) Call at 15732480 Interop.ForwardComStubs Stub_19<System.Canon> (Void InstParam, $ComObject this, Int32 __targetIndex) Stub_19 at 16707566 Microsoft.AppCenter.Utils ApplicationLifecycleHelper.b__17_1 (Object sender, $UnhandledErrorDetectedEventArgs eventArgs)

teohumeau commented 2 years ago

Is there any fix available ?

From the v7.1.2 on a 10.0.22000 OS, AppCenter report this:

Date/Time: 2022-03-03T15:26:42.3980346Z OS Version: 10.0.22000 (10.0.22000.493)

Exception Type: System.ArgumentException

Application Specific Information: *** Terminating app due to uncaught exception 'System.ArgumentException', reason: 'Value does not fall within the expected range.'

Last Exception Backtrace: 0 System.Runtime.InteropServices 0x7fffdc24ef71 System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32, RuntimeTypeHandle) + 0x21 1 Interop 0x7fff7d55d652 Interop.ComCallHelpers.Call(ComObject, RuntimeTypeHandle, Int32, Void, Void) + 0xc2 2 ??? 0x7fff7d55f8f4 Interop.ForwardComStubs.Stub_12[TThis, TArg0](ComObject, TArg0, Object, Int32) + 0x74 3 Microsoft.Toolkit.Uwp.UI.Controls 0x7fff7daeb40a Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.RecalculateLayout(Double) + 0x1ba 4 Microsoft.Toolkit.Uwp.UI.Controls 0x7fff7e85cf3e Microsoft.Toolkit.Uwp.UI.Controls.AdaptiveGridView.OnSizeChanged(Object, SizeChangedEventArgs) + 0x23e 5 System.Xml.XmlTextReaderImpl 0x7fff7d5e804e System.Xml.XmlTextReaderImpl.OnDefaultAttributeUseDelegate.Invoke(IDtdDefaultAttributeInfo, XmlTextReaderImpl) + 0x2e 6 Interop 0x7fff7dc2a3c6 Interop.Intrinsics.HasThisCall24[TArg0](Object, IntPtr, Object, TArg0) + 0x36 7 ??? 0x7fff7dc2a320 AppName!+0x283a320 8 System.Runtime.InteropServices 0x7fffdc24ef71 System.Runtime.InteropServices.McgMarshal.ThrowOnExternalCallFailed(Int32, RuntimeTypeHandle) + 0x21 9 Interop 0x7fff7d558a08 Interop.ComCallHelpers.Call(ComObject, RuntimeTypeHandle, Int32) + 0xb8 10 Interop 0x7fff7d558944 __Interop.ForwardComStubs.Stub_21[TThis](ComObject, Int32) + 0x24 11 Microsoft.AppCenter.Utils 0x7fff7db8469d Microsoft.AppCenter.Utils.ApplicationLifecycleHelper.<.ctor>b17_1(Object, UnhandledErrorDetectedEventArgs) + 0x3d

Hope this will help.

kjean667 commented 1 year ago

Below is some additional information and reproduction case for a crash that may have the same origin as this issue.

Describe the bug

AdaptiveGridView crashes application when sizing page width to zero when setting DesiredWidth.

If using the DesiredWidth property of the CommunityToolkit.WinUI.UI.Controls.AdaptiveGridView it crashes the whole application when the user tries to size the width of a window (or sub-control) down to zero.

System.Exception {System.ArgumentException}
Microsoft.UI.Xaml.UnhandledExceptionEventArgs}
    Exception   {"Value does not fall within the expected range."}  

Steps to reproduce

The following XAML code causes the crash when the user resizes the window width below 200, i.e. just about when the second grid column reaches the size of 0:

<Window
    x:Class="AdaptiveGridViewCrashTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
    mc:Ignorable="d">

  <Grid>
    <Grid.ColumnDefinitions>
      <ColumnDefinition Width="200"/>
      <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Border Grid.Column="0" Background="DarkMagenta"/>

    <controls:AdaptiveGridView Grid.Column="1"
                               DesiredWidth="200">
      <controls:AdaptiveGridView.Items>
        <TextBlock Text="Sample text"/>
      </controls:AdaptiveGridView.Items>
    </controls:AdaptiveGridView>

  </Grid>

</Window>

Stack trace

>   AdaptiveGridViewCrashTest.dll!AdaptiveGridViewCrashTest.App.InitializeComponent.AnonymousMethod__4_1(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) Line 68    C#
    Microsoft.WinUI.dll!WinRT._EventSource_global__Microsoft_UI_Xaml_UnhandledExceptionEventHandler.EventState.GetEventInvoke.AnonymousMethod__1_0(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e)  Unknown
    Microsoft.WinUI.dll!ABI.Microsoft.UI.Xaml.UnhandledExceptionEventHandler.Do_Abi_Invoke(System.IntPtr thisPtr, System.IntPtr sender, System.IntPtr e)    Unknown
    [Native to Managed Transition]  
    Microsoft.ui.xaml.dll!DirectUI::CFTMEventSource<struct ABI::Microsoft::UI::Xaml::IUnhandledExceptionEventHandler,struct ABI::Microsoft::UI::Xaml::IApplication,struct ABI::Microsoft::UI::Xaml::IUnhandledExceptionEventArgs>::Raise(struct ABI::Microsoft::UI::Xaml::IApplication *,struct ABI::Microsoft::UI::Xaml::IUnhandledExceptionEventArgs *)   Unknown
    Microsoft.ui.xaml.dll!DirectUI::FrameworkApplication::RaiseUnhandledExceptionEvent(long,struct HSTRING__ *,bool *)  Unknown
    Microsoft.ui.xaml.dll!DirectUI::ErrorHelper::RaiseUnhandledExceptionEvent(long,class xstring_ptr const &,bool *)    Unknown
    Microsoft.ui.xaml.dll!DirectUI::ErrorHelper::ProcessUnhandledError(class DirectUI::ErrorInfo &,bool,bool *) Unknown
    Microsoft.ui.xaml.dll!DirectUI::FinalUnhandledErrorDetectedRegistration::OnFinalUnhandledErrorDetected(struct IInspectable *,struct ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)   Unknown
    Microsoft.ui.xaml.dll!Microsoft::WRL::Details::DelegateArgTraits<long >::DelegateInvokeHelper<struct Microsoft::WRL::Implements<struct Microsoft::WRL::RuntimeClassFlags<2>,struct ABI::Windows::Foundation::IEventHandler<class ABI::Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,class Microsoft::WRL::FtmBase>,class `Microsoft::WRL::Callback<struct Microsoft::WRL::Implements<struct Microsoft::WRL::RuntimeClassFlags<2>,struct ABI::Windows::Foundation::IEventHandler<class ABI::Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,class Microsoft::WRL::FtmBase>,long >(long )'::`2'::<lambda_1>,1,struct IInspectable *,struct ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *>::Invoke(struct IInspectable *,struct ABI::Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *) Unknown
    twinapi.appcore.dll!Windows::Internal::Details::GitInvokeHelper<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,class Windows::Internal::GitPtr,2>::Invoke(struct IInspectable *,struct Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)    Unknown
    twinapi.appcore.dll!Windows::ApplicationModel::Core::UnhandledErrorInvokeHelper::Invoke(struct IInspectable *,struct Windows::ApplicationModel::Core::IUnhandledErrorDetectedEventArgs *)   Unknown
    twinapi.appcore.dll!Microsoft::WRL::InvokeTraits<2>::InvokeDelegates<class <lambda_f665aa94b11ee3164e641e3706519d88>,struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *> >(class <lambda_f665aa94b11ee3164e641e3706519d88>,class Microsoft::WRL::Details::EventTargetArray *,class Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,struct Microsoft::WRL::InvokeModeOptions<2> > *) Unknown
    twinapi.appcore.dll!Microsoft::WRL::EventSource<struct Windows::Foundation::IEventHandler<class Windows::ApplicationModel::Core::UnhandledErrorDetectedEventArgs *>,struct Microsoft::WRL::InvokeModeOptions<2> >::DoInvoke<class <lambda_f665aa94b11ee3164e641e3706519d88> >(class <lambda_f665aa94b11ee3164e641e3706519d88>)  Unknown
    twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplication::ForwardLocalError(struct IRestrictedErrorInfo *)  Unknown
    twinapi.appcore.dll!Windows::ApplicationModel::Core::CoreApplicationFactory::ForwardLocalError(struct IRestrictedErrorInfo *)   Unknown
    combase.dll!CallErrorForwarder(void * hProcess, int fLocal, IRestrictedErrorInfo * pRestrictedErrorInfo) Line 1601  C++
    combase.dll!RoReportUnhandledError(IRestrictedErrorInfo * pRestrictedErrorInfo) Line 1704   C++
    Microsoft.ui.xaml.dll!DirectUI::ErrorHelper::ReportUnhandledError(long) Unknown
    Microsoft.ui.xaml.dll!DirectUI::DXamlCore::RaiseEvent() Unknown
    Microsoft.ui.xaml.dll!CLayoutManager::RaiseSizeChangedEvents()  Unknown
    Microsoft.ui.xaml.dll!CLayoutManager::UpdateLayout()    Unknown
    Microsoft.ui.xaml.dll!CCoreServices::NWDrawTree()   Unknown
    Microsoft.ui.xaml.dll!CCoreServices::NWDrawMainTree()   Unknown
    Microsoft.ui.xaml.dll!CWindowRenderTarget::Draw()   Unknown
    Microsoft.ui.xaml.dll!CXcpBrowserHost::OnTick() Unknown
    Microsoft.ui.xaml.dll!CXcpDispatcher::Tick()    Unknown
    Microsoft.ui.xaml.dll!CXcpDispatcher::OnReentrancyProtectedWindowMessage(struct HWND__ *,unsigned int,unsigned int,long)    Unknown
    Microsoft.ui.xaml.dll!CDeferredInvoke::DispatchQueuedMessage(bool *,bool *) Unknown
    [...]

Workaround

The crash does not occur if a MinWidth of atleast 4 is applied to the AdaptiveGridView.

    <controls:AdaptiveGridView Grid.Column="1"
                               MinWidth="4"
                               DesiredWidth="200">

Environment

Visual Studio 2022 17.5.4 CommunityToolkit.WinUI.UI.Controls 7.1.2 Microsoft.Windows.SDK.BuildTools 10.0.22621.756 Microsoft.WindowsAppSDK 1.3.230331000