Skip to content

Can't add derived MediaPlayerElement in XAML #8973

@brabebhin

Description

@brabebhin

Describe the bug

The crash that occurred when deriving MediaPlayerElement has been fixed in the latest version, however, the derived MediaPlayerElement still can't be used inside XAML trees.

Steps to reproduce the bug

  1. Create a basic WinUI 3 with package support template using Visual Studio
  2. Replace MainWindow.xaml.cs with the following
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.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 App1
{
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {            
            myButton.Content = "Clicked"
        }


    }

    public class Mpe: MediaPlayerElement
    {

    }
}
  1. Try to add the class to the XAML tree, like so
<Window
    x:Class="App1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App1"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
        <local:Mpe></local:Mpe>
    </StackPanel>
</Window>
  1. The following error will occur when running the app: WinRT originate error - 0x802B000A : 'Cannot add instance of type 'App1.Mpe' to a collection of type 'Microsoft.UI.Xaml.Controls.UIElementCollection'.

Expected behavior

It should be possible to add the derived MediaPlayerElement class inside an XAML tree

Screenshots

No response

NuGet package version

Windows App SDK 1.4.1: 1.4.230913002

Packaging type

Packaged (MSIX)

Windows version

Windows 10 version 22H2 (19045, 2022 Update)

IDE

Visual Studio 2022

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions