The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
[UWP] VisualStateManager inside ContentControl Datatemplate throws NullReferenceException RRS feed

  • Question

  • Hi,

    I basically have this problem, but with a ContentControl instead of a ListView:

    https://stackoverflow.com/questions/42682693/uwp-statetrigger-in-listview-itemtemplate

    I've added the UserControl inside the DataTemplate, but the app will still throw a NullReferenceException. I found out, that it is possible to skip that exception and the App will continue to run. In my real app this still is an issue, as other bindings inside the Datatemplate do not work when skipping/ignoring this exception. I've enabled this exception in the Exception Settings to get notified for it. However this does not happen with the ListView. I could imagine this is a bug in the UWP framework, so someone should have a look at it. Here is a quick demo:

        public class B : INotifyPropertyChanged
        {
            private bool isReady;
            public bool IsReady
            {
                get => isReady;
                set { if (isReady != value) { isReady = value; RaisePropertyChanged(); } }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void RaisePropertyChanged([CallerMemberName]string propertyName = null)
            {
                this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public sealed partial class MainPage : Page
        {
            public B MyContent { get; } = new B();
    
            public MainPage()
            {
                this.InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                MyContent.IsReady = !MyContent.IsReady;
            }
        }


            <ContentControl Content="{x:Bind MyContent}">
                <ContentControl.ContentTemplate>
                    <DataTemplate x:DataType="local:B">
                        <UserControl>
                            <Grid VerticalAlignment="Stretch">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="group">
                                        <VisualState x:Name="state1">
                                            <VisualState.StateTriggers>
                                                <StateTrigger IsActive="{x:Bind IsReady, Mode=OneWay}"/>
                                            </VisualState.StateTriggers>
                                            <Storyboard>
                                                <ColorAnimation Duration="0:0:1.8" To="Red" Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" Storyboard.TargetName="rect" />
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <Rectangle x:Name="rect" Fill="Blue" Width="20" Height="20" />
                            </Grid>
                        </UserControl>
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>
    
            <Button Click="Button_Click">Test</Button>

    Cheers,

    Nico


    • Edited by AtosNicoS Friday, October 25, 2019 11:22 AM
    Friday, October 25, 2019 11:11 AM

All replies

  • Hi Nico,

    I am sorry that you have encountered this problem.

    I copied your code and ran it, but did not throw an Null error, the animation also played as expected.

    My work environment is Windows 10 1903(18362). Have you tried running your application on the latest system? This may be a problem that has been fixed.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 25, 2019 11:59 AM
  • Go to Exception Settings and enable "Common Language Runtime Exceptions" -> "System.NullReferenceException" and you will see it. But only for the ContentControl, not for the ListView.

    As I said the app will continue to run, but in my real world application with a bit more bindings inside the DataTemplate they do not work when the exception is thrown. So there must be something wrong.

    Friday, October 25, 2019 12:01 PM
  • Hi,

    You can try replace x:Bind to Binding, which will avoid errors.

    <StateTrigger IsActive="{Binding IsReady, Mode=OneWay}"/>
    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 25, 2019 1:14 PM
  • Okay, that workaround works. But shouldnt this bug be fixed in UWP as well?
    Monday, October 28, 2019 8:50 AM
  • Hi,

    Sorry for this design to make you confused. But I don't think this is enough to be a bug.

    x:Bind is a compile-time binding, while Binding is a runtime.

    When the application creates the Page, the instance of class B has not been created yet. We can understand it as an exception caused by the generation order, but this is the design of the uwp framework itself.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, October 28, 2019 12:08 PM
  • But this does not happen with a ListView!? And even if B was not yet created, why can't I add a fallbackvalue then? I'd expect that I can add a Fallback value without a crash, but that does not work.

    • Edited by AtosNicoS Monday, October 28, 2019 12:50 PM
    Monday, October 28, 2019 12:09 PM
  • Hi,

    This is the behavior inside the control, there is no document to explain this situation.

    In addition, since StateTrigger.IsActive is a Boolean value, only the {x:Null} and String types support FallbackValue.

    So I suggest using Binding instead of x:Bind.

    Best regards.


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, October 30, 2019 1:48 AM