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] Make OnPlatformExtension and OnIdiomExtension trim-compatible #26119

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Controls/src/Xaml/MarkupExtensions/OnIdiomExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Microsoft.Maui.Controls.Xaml
typeof(IValueConverterProvider),
typeof(IXmlLineInfoProvider),
typeof(IConverterOptions)])]
[RequiresUnreferencedCode("The OnIdiomExtension is not trim safe. Use OnIdiom<T> instead.")]
public class OnIdiomExtension : IMarkupExtension
{
// See Device.Idiom
Expand Down Expand Up @@ -61,7 +60,9 @@ public object ProvideValue(IServiceProvider serviceProvider)

var value = GetValue();
if (value == null && propertyType.IsValueType)
return Activator.CreateInstance(propertyType);
{
throw new XamlParseException($"Missing value for idiom {DeviceInfo.Idiom} or Default", serviceProvider);
}

if (Converter != null)
return Converter.Convert(value, propertyType, ConverterParameter, CultureInfo.CurrentUICulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Microsoft.Maui.Controls.Xaml
typeof(IValueConverterProvider),
typeof(IXmlLineInfoProvider),
typeof(IConverterOptions)])]
[RequiresUnreferencedCode("The OnPlatformExtension is not trim safe. Use OnPlatform<T> instead.")]
public class OnPlatformExtension : IMarkupExtension
{
static object s_notset = new object();
Expand Down Expand Up @@ -81,7 +80,9 @@ public object ProvideValue(IServiceProvider serviceProvider)
return bp.GetDefaultValue(targetObject as BindableObject);
}
if (propertyType.IsValueType)
return Activator.CreateInstance(propertyType);
{
throw new XamlParseException($"Missing value for platform {DeviceInfo.Platform} or Default", serviceProvider);
}
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,6 @@ public void OnPlatformExtension(string markup, string platform, int expected)
[TestCase("{OnIdiom Phone=23, Tablet=25, Desktop=26, TV=30, Watch=10}", "Desktop", 26)]
[TestCase("{OnIdiom Phone=23, Tablet=25, Desktop=26, TV=30, Watch=10}", "TV", 30)]
[TestCase("{OnIdiom Phone=23, Tablet=25, Desktop=26, TV=30, Watch=10}", "Watch", 10)]
[TestCase("{OnIdiom Phone=23}", "Desktop", default(int))]
public void OnIdiomExtension(string markup, string idiom, int expected)
{
mockDeviceInfo.Idiom = DeviceIdiom.Create(idiom);
Expand All @@ -403,6 +402,40 @@ public void OnIdiomExtension(string markup, string idiom, int expected)
Assert.AreEqual(expected, actual);
}

[TestCase("{OnIdiom Phone=23}", "Desktop")]
public void OnIdiomExtensionMissingValue(string markup, string idiom)
{
mockDeviceInfo.Idiom = DeviceIdiom.Create(idiom);

var serviceProvider = new Internals.XamlServiceProvider(null, null)
{
IXamlTypeResolver = typeResolver,
IProvideValueTarget = new MockValueProvider("foo", new object())
{
TargetProperty = GetType().GetProperty(nameof(FontSize))
}
};

Assert.Throws<XamlParseException>(() => (new MarkupExtensionParser()).ParseExpression(ref markup, serviceProvider));
}

[TestCase("{OnPlatform Android=23}", "iOS")]
public void OnPlatformExtensionMissingValue(string markup, string platform)
{
mockDeviceInfo.Platform = DevicePlatform.Create(platform);

var serviceProvider = new Internals.XamlServiceProvider(null, null)
{
IXamlTypeResolver = typeResolver,
IProvideValueTarget = new MockValueProvider("foo", new object())
{
TargetProperty = GetType().GetProperty(nameof(FontSize))
}
};

Assert.Throws<XamlParseException>(() => (new MarkupExtensionParser()).ParseExpression(ref markup, serviceProvider));
}

[TestCase("{Binding")]
[TestCase("{Binding 'Foo}")]
[TestCase("{Binding Foo, Converter={StaticResource Bar}")]
Expand Down
Loading