Skip to content
38 changes: 16 additions & 22 deletions src/CommunityToolkit.Maui.Camera/Views/CameraView.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,53 +230,47 @@ protected virtual void Dispose(bool disposing)
}
}

static Command<CancellationToken> CreateCaptureImageCommand(BindableObject bindable)
Command<CancellationToken> CreateCaptureImageCommand()
{
var cameraView = (CameraView)bindable;
return new(async token => await cameraView.CaptureImage(token).ConfigureAwait(false));
return new(async token => await CaptureImage(token).ConfigureAwait(false));
}

static Command<CancellationToken> CreateStartCameraPreviewCommand(BindableObject bindable)
Command<CancellationToken> CreateStartCameraPreviewCommand()
{
var cameraView = (CameraView)bindable;
return new(async token => await cameraView.StartCameraPreview(token).ConfigureAwait(false));
return new(async token => await StartCameraPreview(token).ConfigureAwait(false));
}

static Command CreateStopCameraPreviewCommand(BindableObject bindable)
Command CreateStopCameraPreviewCommand()
{
var cameraView = (CameraView)bindable;
return new(_ => cameraView.StopCameraPreview());
return new(_ => StopCameraPreview());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_ => will swallow exceptions. Switch to capturing the exception and maybe show a log message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _ => was already in the original code and remains unchanged. The only update here was converting the method from static to non-static, so the behavior should be identical to before.

}

static Command<Stream> CreateStartVideoRecordingCommand(BindableObject bindable)
Command<Stream> CreateStartVideoRecordingCommand()
{
var cameraView = (CameraView)bindable;
return new(async stream => await cameraView.StartVideoRecording(stream).ConfigureAwait(false));
return new(async stream => await StartVideoRecording(stream).ConfigureAwait(false));
}

static Command<CancellationToken> CreateStopVideoRecordingCommand(BindableObject bindable)
Command<CancellationToken> CreateStopVideoRecordingCommand()
{
var cameraView = (CameraView)bindable;
return new(async token => await cameraView.StopVideoRecording(token).ConfigureAwait(false));
return new(async token => await StopVideoRecording(token).ConfigureAwait(false));
}

static object CoerceZoom(BindableObject bindable, object value)
object CoerceZoom(object value)
{
var cameraView = (CameraView)bindable;
var input = (float)value;

if (cameraView.SelectedCamera is null)
if (SelectedCamera is null)
{
return input;
}

if (input < cameraView.SelectedCamera.MinimumZoomFactor)
if (input < SelectedCamera.MinimumZoomFactor)
{
input = cameraView.SelectedCamera.MinimumZoomFactor;
input = SelectedCamera.MinimumZoomFactor;
}
else if (input > cameraView.SelectedCamera.MaximumZoomFactor)
else if (input > SelectedCamera.MaximumZoomFactor)
{
input = cameraView.SelectedCamera.MaximumZoomFactor;
input = SelectedCamera.MaximumZoomFactor;
}

return input;
Expand Down
25 changes: 11 additions & 14 deletions src/CommunityToolkit.Maui.MediaElement/MediaElement.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -338,41 +338,38 @@ protected virtual void Dispose(bool disposing)
isDisposed = true;
}

static void OnSourcePropertyChanged(BindableObject bindable, object oldValue, object newValue)
void OnSourcePropertyChanged(object oldValue, object newValue)
{
var mediaElement = (MediaElement)bindable;
var source = (MediaSource?)newValue;

mediaElement.ClearTimer();
ClearTimer();

if (source is not null)
{
source.SourceChanged += mediaElement.OnSourceChanged;
SetInheritedBindingContext(source, mediaElement.BindingContext);
source.SourceChanged += OnSourceChanged;
SetInheritedBindingContext(source, BindingContext);
}

mediaElement.InvalidateMeasure();
mediaElement.InitializeTimer();
InvalidateMeasure();
InitializeTimer();
}

static void OnSourcePropertyChanging(BindableObject bindable, object oldValue, object newValue)
void OnSourcePropertyChanging(object oldValue, object newValue)
{
var mediaElement = (MediaElement)bindable;
var oldMediaSource = (MediaSource?)oldValue;

oldMediaSource?.SourceChanged -= mediaElement.OnSourceChanged;
oldMediaSource?.SourceChanged -= OnSourceChanged;
}

static void OnCurrentStatePropertyChanged(BindableObject bindable, object oldValue, object newValue)
void OnCurrentStatePropertyChanged(object oldValue, object newValue)
{
var mediaElement = (MediaElement)bindable;
var previousState = (MediaElementState)oldValue;
var newState = (MediaElementState)newValue;

mediaElement.OnStateChanged(new MediaStateChangedEventArgs(previousState, newState));
OnStateChanged(new MediaStateChangedEventArgs(previousState, newState));
}

static void OnVolumeChanging(BindableObject bindable, object oldValue, object newValue)
void OnVolumeChanging(object oldValue, object newValue)
{
var updatedVolume = (double)newValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public sealed partial class FileMediaSource : MediaSource
/// <inheritdoc/>
public override string ToString() => $"File: {Path}";

static void OnFileMediaSourceChanged(BindableObject bindable, object oldValue, object newValue) =>
((FileMediaSource)bindable).OnSourceChanged();
void OnFileMediaSourceChanged(object oldValue, object newValue) =>
OnSourceChanged();
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public sealed partial class ResourceMediaSource : MediaSource
/// <inheritdoc/>
public override string ToString() => $"Resource: {Path}";

static void OnResourceMediaSourceMediaSourceChanged(BindableObject bindable, object oldValue, object newValue) =>
((ResourceMediaSource)bindable).OnSourceChanged();
void OnResourceMediaSourceMediaSourceChanged(object oldValue, object newValue) =>
OnSourceChanged();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public sealed partial class UriMediaSource : MediaSource
/// <inheritdoc/>
public override string ToString() => $"Uri: {Uri}";

static bool UriValueValidator(BindableObject bindable, object value) =>
bool UriValueValidator(object value) =>
value is null || ((Uri)value).IsAbsoluteUri;

static void OnUriSourceChanged(BindableObject bindable, object oldValue, object newValue) =>
((UriMediaSource)bindable).OnSourceChanged();
void OnUriSourceChanged(object oldValue, object newValue) =>
OnSourceChanged();
}
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,11 @@ public partial class {{defaultTestClassName}} : View
DefaultValueCreatorMethodName = "CreateDefaultValue")]
public partial int Value { get; set; } = 42;

static bool ValidateValue(BindableObject bindable, object value) => true;
static void OnPropertyChanged(BindableObject bindable, object oldValue, object newValue) { }
static void OnPropertyChanging(BindableObject bindable, object oldValue, object newValue) { }
static object CoerceValue(BindableObject bindable, object value) => value;
static object CreateDefaultValue(BindableObject bindable) => 0;
bool ValidateValue(object value) => true;
void OnPropertyChanged(object oldValue, object newValue) { }
void OnPropertyChanging(object oldValue, object newValue) { }
object CoerceValue(object value) => value;
object CreateDefaultValue() => 0;
}
""";

Expand All @@ -222,7 +222,7 @@ public partial class {{defaultTestClassName}}
/// <summary>
/// Backing BindableProperty for the <see cref = "Value"/> property.
/// </summary>
public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(int), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, (Microsoft.Maui.Controls.BindingMode)1, ValidateValue, OnPropertyChanged, OnPropertyChanging, CoerceValue, CreateDefaultValue);
public static readonly global::Microsoft.Maui.Controls.BindableProperty ValueProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Value", typeof(int), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, (Microsoft.Maui.Controls.BindingMode)1, (b, v) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).ValidateValue(v), (b, o, n) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).OnPropertyChanged(o, n), (b, o, n) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).OnPropertyChanging(o, n), (b, v) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).CoerceValue(v), (b) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).CreateDefaultValue());
public partial int Value { get => false ? field : (int)GetValue(ValueProperty); set => SetValue(ValueProperty, value); }
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public partial class {{defaultTestClassName}} : View
[BindablePropertyAttribute(DefaultValueCreatorMethodName = nameof(CreateDefaultText))]
public partial string Text { get; set; } = "Initial Value";

static string CreateDefaultText(BindableObject bindable)
string CreateDefaultText()
{
return "Initial Value";
}
Expand All @@ -623,7 +623,7 @@ public partial class {{defaultTestClassName}}
/// <summary>
/// Backing BindableProperty for the <see cref = "Text"/> property.
/// </summary>
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, CreateDefaultText);
public static readonly global::Microsoft.Maui.Controls.BindableProperty TextProperty = global::Microsoft.Maui.Controls.BindableProperty.Create("Text", typeof(string), typeof({{defaultTestNamespace}}.{{defaultTestClassName}}), null, Microsoft.Maui.Controls.BindingMode.OneWay, null, null, null, null, (b) => (({{defaultTestNamespace}}.{{defaultTestClassName}})b).CreateDefaultText());
public partial string Text { get => false ? field : (string)GetValue(TextProperty); set => SetValue(TextProperty, value); }
}
""";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ public void BindablePropertyModel_WithAllParameters_StoresCorrectValues()
Assert.Equal(newKeywordText, model.NewKeywordText);
Assert.Equal(hasInitializer, model.HasInitializer);
Assert.Equal("TestPropertyProperty", model.BindablePropertyName);
Assert.Equal(defaultValueCreatorMethodName, model.EffectiveDefaultValueCreatorMethodName);
Assert.Equal(model.DefaultValueCreatorMethodName, model.EffectiveDefaultValueCreatorMethodName);
Assert.Equal($"(b,v) => ((TestClass)b).{model.ValidateValueMethodName}(v)", model.ValidateValueMethodExpression);
Assert.Equal($"(b,o,n) => ((TestClass)b).{model.PropertyChangedMethodName}(o,n)", model.PropertyChangedMethodExpression);
Assert.Equal($"(b,o,n) => ((TestClass)b).{model.PropertyChangingMethodName}(o,n)", model.PropertyChangingMethodExpression);
Assert.Equal($"(b,v) => ((TestClass)b).{model.CoerceValueMethodName}(v)", model.CoerceValueMethodExpression);
Assert.Equal($"(b) => ((TestClass)b).{defaultValueCreatorMethodName}()", model.DefaultValueCreatorMethodExpression);
Assert.Equal("IsInitializingTestProperty", model.InitializingPropertyName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,13 @@ static void GenerateReadOnlyBindableProperty(StringBuilder sb, in BindableProper
.Append("), null, ")
.Append(info.DefaultBindingMode)
.Append(", ")
.Append(info.ValidateValueMethodName)
.Append(info.ValidateValueMethodExpression)
.Append(", ")
.Append(info.PropertyChangedMethodName)
.Append(info.PropertyChangedMethodExpression)
.Append(", ")
.Append(info.PropertyChangingMethodName)
.Append(info.PropertyChangingMethodExpression)
.Append(", ")
.Append(info.CoerceValueMethodName)
.Append(info.CoerceValueMethodExpression)
.Append(", ");

if (info.ShouldUsePropertyInitializer)
Expand All @@ -275,7 +275,7 @@ static void GenerateReadOnlyBindableProperty(StringBuilder sb, in BindableProper
.Append('.');
}

sb.Append(info.EffectiveDefaultValueCreatorMethodName)
sb.Append(info.DefaultValueCreatorMethodExpression)
.Append(");\n");

// Generate public BindableProperty from the key
Expand Down Expand Up @@ -321,13 +321,13 @@ static void GenerateBindableProperty(StringBuilder sb, in BindablePropertyModel
.Append("), null, ")
.Append(info.DefaultBindingMode)
.Append(", ")
.Append(info.ValidateValueMethodName)
.Append(info.ValidateValueMethodExpression)
.Append(", ")
.Append(info.PropertyChangedMethodName)
.Append(info.PropertyChangedMethodExpression)
.Append(", ")
.Append(info.PropertyChangingMethodName)
.Append(info.PropertyChangingMethodExpression)
.Append(", ")
.Append(info.CoerceValueMethodName)
.Append(info.CoerceValueMethodExpression)
.Append(", ");

if (info.ShouldUsePropertyInitializer)
Expand All @@ -336,7 +336,7 @@ static void GenerateBindableProperty(StringBuilder sb, in BindablePropertyModel
.Append('.');
}

sb.Append(info.EffectiveDefaultValueCreatorMethodName)
sb.Append(info.DefaultValueCreatorMethodExpression)
.Append(");\n")
.Append('\n');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ public record BindablePropertyModel(string PropertyName, ITypeSymbol ReturnType,
public string BindablePropertyName => $"{PropertyName}Property";
public string BindablePropertyKeyName => $"{char.ToLower(PropertyName[0])}{PropertyName[1..]}PropertyKey";
public string EffectiveDefaultValueCreatorMethodName => ShouldUsePropertyInitializer ? $"CreateDefault{PropertyName}" : DefaultValueCreatorMethodName;
public string ValidateValueMethodExpression => ValidateValueMethodName is "null" ? ValidateValueMethodName : $"(b,v) => (({DeclaringType})b).{ValidateValueMethodName}(v)";
public string PropertyChangedMethodExpression => PropertyChangedMethodName is "null" ? PropertyChangedMethodName : $"(b,o,n) => (({DeclaringType})b).{PropertyChangedMethodName}(o,n)";
public string PropertyChangingMethodExpression => PropertyChangingMethodName is "null" ? PropertyChangingMethodName : $"(b,o,n) => (({DeclaringType})b).{PropertyChangingMethodName}(o,n)";
public string CoerceValueMethodExpression => CoerceValueMethodName is "null" ? CoerceValueMethodName : $"(b,v) => (({DeclaringType})b).{CoerceValueMethodName}(v)";
public string DefaultValueCreatorMethodExpression => ShouldUsePropertyInitializer
? EffectiveDefaultValueCreatorMethodName
: (DefaultValueCreatorMethodName is "null" ? DefaultValueCreatorMethodName : $"(b) => (({DeclaringType})b).{DefaultValueCreatorMethodName}()");

public string InitializingPropertyName => $"IsInitializing{PropertyName}";

}
Expand Down
21 changes: 7 additions & 14 deletions src/CommunityToolkit.Maui/Behaviors/AnimationBehavior.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,20 +82,15 @@ protected override async void OnTriggerHandled(object? sender = null, object? ev
base.OnTriggerHandled(sender, eventArgs);
}

static void OnAnimateOnTapPropertyChanged(BindableObject bindable, object oldValue, object newValue)
void OnAnimateOnTapPropertyChanged(object oldValue, object newValue)
{
if (bindable is not AnimationBehavior behavior)
{
return;
}

if ((bool)newValue)
{
behavior.AddTapGestureRecognizer();
AddTapGestureRecognizer();
}
else
{
behavior.RemoveTapGestureRecognizer();
RemoveTapGestureRecognizer();
}
}

Expand Down Expand Up @@ -128,18 +123,16 @@ void RemoveTapGestureRecognizer()
tapGestureRecognizer = null;
}

static Command<CancellationToken> CreateAnimateCommand(BindableObject bindable)
Command<CancellationToken> CreateAnimateCommand()
{
var animationBehavior = (AnimationBehavior)bindable;
return new Command<CancellationToken>(async token => await animationBehavior.OnAnimate(token).ConfigureAwait(false));
return new Command<CancellationToken>(async token => await OnAnimate(token).ConfigureAwait(false));
}

static void OnAnimateCommandChanging(BindableObject bindable, object oldValue, object newValue)
void OnAnimateCommandChanging(object oldValue, object newValue)
{
if (newValue is not Command<CancellationToken>)
{
var animationBehavior = (AnimationBehavior)bindable;
throw new InvalidOperationException($"{nameof(AnimateCommand)} must of Type {animationBehavior.AnimateCommand.GetType().FullName}");
throw new InvalidOperationException($"{nameof(AnimateCommand)} must of Type {AnimateCommand.GetType().FullName}");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ protected override void OnDetachingFrom(VisualElement bindable)
base.OnDetachingFrom(bindable);
}

static void OnEventNamePropertyChanged(BindableObject bindable, object oldValue, object newValue)
=> ((EventToCommandBehavior)bindable).RegisterEvent();
void OnEventNamePropertyChanged(object oldValue, object newValue)
=> RegisterEvent();

void RegisterEvent()
{
Expand Down
12 changes: 5 additions & 7 deletions src/CommunityToolkit.Maui/Behaviors/MaskedBehavior.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,18 @@ protected override async void OnViewPropertyChanged(InputView sender, PropertyCh
}
}

static object UnmaskedCharacterValueCreator(BindableObject bindable) => 'X';
object UnmaskedCharacterValueCreator() => 'X';

static void OnMaskPropertyChanged(BindableObject bindable, object oldValue, object newValue)
void OnMaskPropertyChanged(object oldValue, object newValue)
{
var mask = (string?)newValue;
var maskedBehavior = (MaskedBehavior)bindable;

maskedBehavior.SetMaskPositions(mask);
SetMaskPositions(mask);
}

static async void OnUnmaskedCharacterPropertyChanged(BindableObject bindable, object oldValue, object newValue)
async void OnUnmaskedCharacterPropertyChanged(object oldValue, object newValue)
{
var maskedBehavior = (MaskedBehavior)bindable;
await maskedBehavior.OnMaskChanged(maskedBehavior.Mask, CancellationToken.None).ConfigureAwait(false);
await OnMaskChanged(Mask, CancellationToken.None).ConfigureAwait(false);
}

Task OnTextPropertyChanged(CancellationToken token)
Expand Down
Loading
Loading