Caliburn.Micro icon indicating copy to clipboard operation
Caliburn.Micro copied to clipboard

typeInfo.GetCustomAttribute<ContentPropertyAttribute>() returns null

Open mohamedraqeeb opened this issue 6 years ago • 5 comments

I have migrated a Windows Store 8.1 application to UWP using this guide from Windows. During this migration process It was required to remove all existing project references and to manually install them again using Nuget. (Previously installed version- 2.0.2) But when I install the latest version from Nuget (Version - 3.2.0) I'm getting the following error when opening popups. exception 3 2 0

Code used to create and open Popups

public virtual void ShowPopup(object rootModel, object context = null, IDictionary<string, object> settings = null)
        {
            var popup = this.CreatePopup(rootModel, settings);
            var view = ViewLocator.LocateForModel(rootModel, popup, context);

            popup.Child = view;
            popup.SetValue(View.IsGeneratedProperty, true);
            ViewModelBinder.Bind(rootModel, popup, null);
            Caliburn.Micro.Action.SetTargetWithoutContext(view, rootModel);

            var activatable = rootModel as IActivate;
            if (activatable != null)
            {
                activatable.Activate();
            }

            var deactivator = rootModel as IDeactivate;
            if (deactivator != null)
            {
                popup.Closed += (object sender, object e) => { deactivator.Deactivate(true); };
            }

            popup.IsOpen = true;
        }

However I was able to clone this repository and debug to the point of exception. It seems that this particular code (line 390) in View.cs returns null. var contentProperty = typeInfo.GetCustomAttribute<ContentPropertyAttribute>(); But when I replaced the current code with the corresponding code snippet in 2.0.2 (line 362) It works perfectly.

var contentProperty = typeInfo.CustomAttributes
                .FirstOrDefault(a => a.AttributeType == typeof(ContentPropertyAttribute));

            return contentProperty == null ?
                DefaultContentPropertyName :
                contentProperty.NamedArguments[0].TypedValue.Value.ToString();

I would like to know if there is a workaround. Thanks in advance.

mohamedraqeeb avatar Feb 08 '19 07:02 mohamedraqeeb

What's the type of the control being exampled here?

Looking at the line below it shows it being null safe https://github.com/Caliburn-Micro/Caliburn.Micro/blob/3.2.0/src/Caliburn.Micro.Platform/View.cs#L392

nigel-sampson avatar Feb 11 '19 01:02 nigel-sampson

Thanks for the response. But though the method returns a value, the expected return value is different on both implementations. In version 2.0.2 "Child" is returned compared to the default value "Content" being returned in version 3.2.0. This is because this line in version 3.2.0 returns null. var contentProperty = typeInfo.GetCustomAttribute<ContentPropertyAttribute>(); compared to this line in version 2.0.2 which returns a value.

 var contentProperty = typeInfo.CustomAttributes
                .FirstOrDefault(a => a.AttributeType == typeof(ContentPropertyAttribute));

mohamedraqeeb avatar Feb 11 '19 06:02 mohamedraqeeb

Interesting, how that comes about., Assume your control hierarchy has multiple definitions of ContentPropertyAttribute? Which control is the type for the view?

nigel-sampson avatar Feb 15 '19 02:02 nigel-sampson

The following is the XAML template we used for the Popup. All the popups in the application are similar to this. The type of Popup used is

Windows.UI.Xaml.Controls.Primitives.Popup

.I assume that this is what you are looking for, or else please let me know.

<UserControl
    x:Class="Items.Assets.AssetInfoView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Items.Assets"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <UserControl.Resources>
        <Style x:Key="AssetInfoListView" TargetType="ListViewItem">
            <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/>
            <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="TabNavigation" Value="Local"/>
            <Setter Property="IsHoldingEnabled" Value="True"/>
            <Setter Property="Margin" Value="0,0,18,2"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="VerticalContentAlignment" Value="Top"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ListViewItem">
                        <ListViewItemPresenter 
                            CheckBrush="#FF2E41FF" 
                            ContentMargin="4" 
                            ContentTransitions="{TemplateBinding ContentTransitions}" 
                            DragOpacity="{ThemeResource ListViewItemDragThemeOpacity}" 
                            DisabledOpacity="{ThemeResource ListViewItemDisabledThemeOpacity}" 
                            HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" 
                            Padding="{TemplateBinding Padding}" 
                            PointerOverBackgroundMargin="0,0,0,5" 
                            PlaceholderBackground="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" 
                            PointerOverBackground="{ThemeResource ListViewItemPointerOverBackgroundThemeBrush}" 
                            ReorderHintOffset="{ThemeResource ListViewItemReorderHintThemeOffset}" 
                            SelectionCheckMarkVisualEnabled="True" SelectedForeground="{ThemeResource ListViewItemSelectedForegroundThemeBrush}" 
                            SelectedBorderThickness="{ThemeResource ListViewItemCompactSelectedBorderThemeThickness}" 
                            VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" 
                            SelectedBackground="Transparent"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </UserControl.Resources>
    <Grid
        Width="340"
        Height="455"
        Opacity="0.99">

        <Grid.RowDefinitions>
            <RowDefinition Height="10"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>

        <Viewbox 
            Grid.Row="0"
            Width="30" Margin="90,0,0,0">
            <Path Data="m 10,0 L 0,10 20,10 Z" Fill="#f2f2f2" Opacity="0.99" Margin="0"></Path>
        </Viewbox>

        <Grid 
            Grid.Row="1" VerticalAlignment="Top">
            <Border
                Width="340"
                Height="445"
                Background="#f2f2f2"
                CornerRadius="15"
                Opacity="0.99">

                <Grid
                    Background="Transparent">

                    <Grid.RowDefinitions>
                        <RowDefinition Height="45"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>

                    <TextBlock 
                        Grid.Row="0" 
                        Text="{Binding AssetInfoTitle}" 
                        FontSize="21" 
                        HorizontalAlignment="Center" 
                        Foreground="{StaticResource AppTitleFontColor}"
                        VerticalAlignment="Center" Margin="0,0,0,1"/>

                    <ListView 
                        Grid.Row="1" 
                        ItemsSource="{Binding AssetInfoList}" 
                        Height="400"
                        Width="340"
                        IsItemClickEnabled="False" 
                        ItemContainerStyle="{StaticResource AssetInfoListView}" 
                        VerticalAlignment="Top" 
                        BorderBrush="#b8b8b8" BorderThickness="0,1,0,0"
                        SelectionMode="None"
                        Padding="15,0,0,0">

                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Border BorderBrush="#c9c7c7" BorderThickness="0,0,0,1">
                                    <Grid MinHeight="37" Width="305">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                                            <ColumnDefinition Width="*"></ColumnDefinition>
                                        </Grid.ColumnDefinitions>
                                        <TextBlock
                                            Grid.Column="0"
                                            FontSize="18" 
                                            Text="{Binding Name}" 
                                            FontWeight="Bold"
                                            Margin="0,0,5,0"
                                            MaxWidth="200"
                                            HorizontalAlignment="Left"
                                            TextTrimming="CharacterEllipsis">
                                        </TextBlock>
                                        <TextBlock
                                            Grid.Column="1"
                                            FontSize="18" 
                                            Text=":" 
                                            FontWeight="Bold"
                                            HorizontalAlignment="Left">
                                        </TextBlock>
                                        <TextBlock
                                            Grid.Column="2"
                                            FontSize="18" Width="Auto"
                                            Text="{Binding Value}" TextWrapping="WrapWholeWords" HorizontalAlignment="Left">
                                        </TextBlock>
                                    </Grid>
                                </Border>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>

            </Border>
        </Grid>
    </Grid>
</UserControl>

mohamedraqeeb avatar Feb 15 '19 09:02 mohamedraqeeb

Thanks for the extra info.

nigel-sampson avatar Feb 19 '19 22:02 nigel-sampson