Implement [BindableProperty] for MediaElement#2933
Implement [BindableProperty] for MediaElement#2933TheCodeTraveler merged 51 commits intoCommunityToolkit:mainfrom
[BindableProperty] for MediaElement#2933Conversation
…b.com/ne0rrmatrix/MauiOld into Use-BindableProperty-for-MediaElement
There was a problem hiding this comment.
Pull request overview
This PR migrates the MediaElement and MediaSource classes from manually-created BindableProperty declarations to using the [BindableProperty] source generator attribute. It also enhances the source generator to better support Int32, double, and other primitive types as default values.
Key Changes:
- Replaced manual
BindablePropertydeclarations with[BindableProperty]attributes across MediaElement and MediaSource classes - Enhanced
AttributeExtensions.cswith improved type handling for Int32, double, TimeSpan, and Enum types - Added
Range.csas a polyfill for older framework versions
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 17 comments.
Show a summary per file
| File | Description |
|---|---|
src/CommunityToolkit.Maui.SourceGenerators.Internal/Range.cs |
Added polyfill implementation for Index and Range types to support source generator in older frameworks |
src/CommunityToolkit.Maui.SourceGenerators.Internal/Helpers/AttributeExtensions.cs |
Enhanced attribute value parsing with support for Int32, double, TimeSpan, and Enum types, though contains code duplication |
src/CommunityToolkit.Maui.MediaElement/MediaSource/UriMediaSource.shared.cs |
Migrated Uri property from manual BindableProperty to [BindableProperty] attribute |
src/CommunityToolkit.Maui.MediaElement/MediaSource/ResourceMediaSource.shared.cs |
Migrated Path property from manual BindableProperty to [BindableProperty] attribute |
src/CommunityToolkit.Maui.MediaElement/MediaSource/FileMediaSource.shared.cs |
Migrated Path property from manual BindableProperty to [BindableProperty] attribute |
src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs |
Migrated all MediaElement bindable properties to use [BindableProperty] attribute, with several issues in default values, property callbacks, and interface implementations |
src/CommunityToolkit.Maui.MediaElement/CommunityToolkit.Maui.MediaElement.csproj |
Added source generator project reference |
samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementCollectionViewPage.xaml |
Added AndroidViewType attribute to MediaElement instance |
samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementCarouselViewPage.xaml |
Added AndroidViewType attribute and reformatted MediaElement element |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
pictos
left a comment
There was a problem hiding this comment.
Using the request change, since the code as it's right now will break at some point
src/CommunityToolkit.Maui.SourceGenerators.Internal/Helpers/AttributeExtensions.cs
Outdated
Show resolved
Hide resolved
Do not merge until after CommunityToolkit#2933 is merged
There was a problem hiding this comment.
Thanks James!
I updated BindablePropertyAttributeSourceGenerator to support all accessibility types (private, private protected, protected, internal, protected internal and public), and I added Unit Tests for each setter accessibility type.
@pictos Could you give this a quick review too? Once you approve, we'll merge this 🙌
7c46c3d
into
CommunityToolkit:main
Description of Change
This pull request refactors the way bindable properties are declared and managed in the
MediaElementand related media source classes. It transitions from manually definingBindablePropertyfields and associated property wrappers to using the[BindableProperty]source generator attribute, which simplifies code and reduces boilerplate. Additionally, it updates project configuration to support source generators and analyzers.Key changes include:
Refactoring to Source Generator Bindable Properties:
Replaces manual
BindablePropertydeclarations inMediaElement,FileMediaSource,ResourceMediaSource, andUriMediaSourcewith[BindableProperty]attributes, reducing boilerplate and improving maintainability. This also updates property access patterns and documentation.Removes now-unnecessary property wrappers and backing fields, relying on the generated partial properties instead.
Validation and Property Changed Logic:
Refactors the
ValidateVolumemethod to match the new source generator signature and return a boolean, throwing an exception for out-of-range values.Ensures property changed/callback methods are correctly linked via attribute parameters, maintaining previous behaviors for properties like
Source,CurrentState, andUri.Project and Build Configuration:
CommunityToolkit.Maui.MediaElement.csprojto enable compiler-generated files output and adds a reference to the internal source generators project as an analyzer, supporting the new property declaration style.These changes modernize the codebase, making it easier to maintain and reducing the risk of errors in property declaration and management.
As Discussed in our November 2025 Standup, this PR implements the
[BindableProperty]attribute, replacing the manually created BindableProperties.This PR also updates the BindableProperty Source Generator:
Int32when used as a DefaultValuedoublewhen used as DefaultValue[BindableProperty]Support for Additional Scenarios, AddCommunityToolkit.Maui.SourceGenerators.Internal.UnitTests, Implement[BindableProperty]for Behaviors #2932PR Checklist
mainat time of PRAdditional information