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

x:Bind System.Runtime.InteropServices.COMException: „Unspecified error” #9251

Closed
mx2rel opened this issue Jan 14, 2024 · 0 comments
Closed

Comments

@mx2rel
Copy link

mx2rel commented Jan 14, 2024

Hi, I've x:Bind with a custom control. Whenever I display the page (with the control), it crashes with the exception - System.Runtime.InteropServices.COMException: 'Unspecified error Cannot find a resource with the given key: BoolToVisibilityConverter.'

MessagesPage.xaml

<ListView ItemsSource="{x:Bind Received, Mode=OneWay}" IsItemClickEnabled="True" SelectionMode="None" Grid.Row="1" Margin="0,10,0,0" ItemClick="ListView_ItemClick">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:MessageViewModel">
                    <controls:SingleMessageBig Message="{Binding}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

SingleMessageBig.xaml

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

    <Grid DataContext="{x:Bind Message, Mode=OneWay}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="30"/>
            <ColumnDefinition Width="250"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="auto"/>
        </Grid.ColumnDefinitions>
        <CheckBox IsChecked="{x:Bind Message.IsSelected, Mode=TwoWay}" VerticalAlignment="Center"/>
        <TextBlock Text="{x:Bind Message.message.Sender.Name, Mode=OneWay}"
                               TextWrapping="NoWrap" TextTrimming="CharacterEllipsis"
                               FontWeight="Medium" FontSize="16" VerticalAlignment="Center" Grid.Column="1"/>
        <Grid Grid.Column="2">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="20"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{x:Bind Message.message.Subject, Mode=OneWay}" VerticalAlignment="Center" FontWeight="Bold"/>
            <TextBlock Text="|" TextAlignment="Center" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.Column="1"/>
            <TextBlock Text="{x:Bind Message.PlainContent, Mode=OneWay}" 
                                   VerticalAlignment="Center" Grid.Column="2"
                                   TextTrimming="CharacterEllipsis" />

        </Grid>
        <StackPanel Orientation="Horizontal" Grid.Column="3" Padding="10,0,0,0" Spacing="5">
            <TextBlock Text="{x:Bind Message.message.DateSent, Mode=OneWay}" HorizontalAlignment="Right" VerticalAlignment="Center"
                                       Visibility="{x:Bind Message.Hover, Mode=OneWay, Converter={StaticResource VisibilityConverter},ConverterParameter=True}"/>
            <Button HorizontalAlignment="Right" VerticalAlignment="Center"
                                       Visibility="{x:Bind Message.Hover, Mode=OneWay, 
                                    Converter={StaticResource VisibilityConverter},ConverterParameter=False}"
                                        Width="32" Height="32" Padding="0">
                <SymbolIcon Symbol="Delete" />
            </Button>
            <Button HorizontalAlignment="Right" VerticalAlignment="Center"
                                       Visibility="{x:Bind Message.Hover, Mode=OneWay, 
                                    Converter={StaticResource VisibilityConverter},ConverterParameter=False}"
                                        Width="32" Height="32" Padding="0">
                <SymbolIcon Symbol="Read" />
            </Button>
        </StackPanel>
    </Grid>
</UserControl>

SingleMessageBig.xaml.cs

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

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

namespace VulcanForWindows.UserControls
{
    public sealed partial class SingleMessageBig : UserControl, INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); //error there

        }

        public static readonly DependencyProperty MessageProperty =
            DependencyProperty.Register("Message", typeof(MessageViewModel), typeof(SingleMessageBig), new PropertyMetadata(null, Message_Changed));

        public MessageViewModel Message
        {
            get => (MessageViewModel)GetValue(MessageProperty);
            set => SetValue(MessageProperty, value); //error there
        }

        private static void Message_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is SingleMessageBig control && e.NewValue is MessageViewModel newValue)
            {
                control.OnPropertyChanged(nameof(Message)); //error there
            }
        }
        void HoverOn(object sender, PointerRoutedEventArgs e)
        {
            if (e.OriginalSource is FrameworkElement element)
            {
                // Access the data of the item being hovered
                MessageViewModel itemData = element.DataContext as MessageViewModel;

                // Now, you can use itemData to access properties or perform actions
                // For example, show additional information based on the data
                if (itemData != null)
                {
                    itemData.Hover = true;
                }
            }
        }
        void HoverOff(object sender, PointerRoutedEventArgs e)
        {
            if (e.OriginalSource is FrameworkElement element)
            {
                // Access the data of the item being hovered
                MessageViewModel itemData = element.DataContext as MessageViewModel;

                // Now, you can use itemData to access properties or perform actions
                // For example, show additional information based on the data
                if (itemData != null)
                {
                    itemData.Hover = true;
                }
            }
        }
        public SingleMessageBig()
        {
            OnPropertyChanged(nameof(Message));
            this.InitializeComponent();
            AddHandler(PointerEnteredEvent, new PointerEventHandler(HoverOn), true);
            AddHandler(PointerExitedEvent, new PointerEventHandler(HoverOff), true);
        }
    }
}

Couldn't find any way to fix it. Changing to Binding doesn't work. What's the issue?

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 14, 2024
@mx2rel mx2rel closed this as completed Jan 14, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot removed the needs-triage Issue needs to be triaged by the area owners label Jan 14, 2024
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

1 participant