Co-authored-by: Rui Marinho <me@ruimarinho.net>
This commit is contained in:
Родитель
327ea57b69
Коммит
26584c588e
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using AppKit;
|
||||
using Xamarin.Forms;
|
||||
using Xamarin.Forms.ControlGallery.MacOS;
|
||||
using Xamarin.Forms.Controls;
|
||||
|
@ -7,6 +8,7 @@ using Xamarin.Forms.Platform.MacOS;
|
|||
|
||||
[assembly: Dependency(typeof(TestCloudService))]
|
||||
[assembly: Dependency(typeof(CacheService))]
|
||||
[assembly: Dependency(typeof(NativeColorService))]
|
||||
[assembly: ExportRenderer(typeof(DisposePage), typeof(DisposePageRenderer))]
|
||||
[assembly: ExportRenderer(typeof(DisposeLabel), typeof(DisposeLabelRenderer))]
|
||||
|
||||
|
@ -25,6 +27,17 @@ namespace Xamarin.Forms.ControlGallery.MacOS
|
|||
}
|
||||
}
|
||||
|
||||
public class NativeColorService : INativeColorService
|
||||
{
|
||||
public Color GetConvertedColor(bool shouldCrash)
|
||||
{
|
||||
if (shouldCrash)
|
||||
return NSColor.ControlText.ToColor();
|
||||
|
||||
return NSColor.ControlText.ToColor(NSColorSpace.DeviceRGBColorSpace);
|
||||
}
|
||||
}
|
||||
|
||||
public class DisposePageRenderer : PageRenderer
|
||||
{
|
||||
protected override void Dispose(bool disposing)
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
namespace Xamarin.Forms.Controls
|
||||
{
|
||||
public interface INativeColorService
|
||||
{
|
||||
Color GetConvertedColor(bool shouldCrash);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,84 @@
|
|||
using System;
|
||||
using Xamarin.Forms.CustomAttributes;
|
||||
using Xamarin.Forms.Internals;
|
||||
|
||||
namespace Xamarin.Forms.Controls.Issues
|
||||
{
|
||||
[Preserve(AllMembers = true)]
|
||||
[Issue(IssueTracker.Github, 9962, "NSException thrown when calling NSColor.ControlBackground.ToColor()", PlatformAffected.macOS)]
|
||||
public class Issue9962 : TestContentPage
|
||||
{
|
||||
public Issue9962()
|
||||
{
|
||||
}
|
||||
|
||||
protected override void Init()
|
||||
{
|
||||
var stackLayout = new StackLayout
|
||||
{
|
||||
HorizontalOptions = LayoutOptions.Center,
|
||||
VerticalOptions = LayoutOptions.Center
|
||||
};
|
||||
|
||||
var debugLabel = new Label
|
||||
{
|
||||
Text = "The first button should show an alert with the exception message, second button should retrieve an actual color and put the value in this label and in the BoxView"
|
||||
};
|
||||
|
||||
var boxView = new BoxView
|
||||
{
|
||||
BackgroundColor = Color.Blue,
|
||||
WidthRequest = 100,
|
||||
HeightRequest = 100
|
||||
};
|
||||
|
||||
var buttonBoom = new Button
|
||||
{
|
||||
Text = "This button should throw an Exception"
|
||||
};
|
||||
|
||||
buttonBoom.Clicked += (_, __) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var color = DependencyService.Get<INativeColorService>()?.GetConvertedColor(true);
|
||||
|
||||
boxView.BackgroundColor = color ?? Color.Black;
|
||||
|
||||
}
|
||||
catch (InvalidOperationException ex)
|
||||
{
|
||||
DisplayAlert("Exception!", ex.Message, "Gotcha");
|
||||
}
|
||||
};
|
||||
|
||||
var buttonNotBoom = new Button
|
||||
{
|
||||
Text = "This button should NOT throw an Exception"
|
||||
};
|
||||
|
||||
buttonNotBoom.Clicked += (_, __) =>
|
||||
{
|
||||
try
|
||||
{
|
||||
var color = DependencyService.Get<INativeColorService>()?.GetConvertedColor(false);
|
||||
|
||||
debugLabel.Text = color?.ToString();
|
||||
boxView.BackgroundColor = color ?? Color.Black;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DisplayAlert("Exception!", ex.Message, "Gotcha");
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
stackLayout.Children.Add(buttonBoom);
|
||||
stackLayout.Children.Add(buttonNotBoom);
|
||||
stackLayout.Children.Add(debugLabel);
|
||||
stackLayout.Children.Add(boxView);
|
||||
|
||||
Content = stackLayout;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1342,6 +1342,8 @@
|
|||
<Compile Include="$(MSBuildThisFileDirectory)Issue8272.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Issue8964.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Issue9951.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Issue9962.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Controls\INativeColorService.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Issue9794.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -187,11 +187,23 @@ namespace Xamarin.Forms.Platform.MacOS
|
|||
#if __MOBILE__
|
||||
color.GetRGBA(out red, out green, out blue, out alpha);
|
||||
#else
|
||||
if (color.Type == NSColorType.Catalog)
|
||||
throw new InvalidOperationException("Cannot convert a NSColorType.Catalog color without specifying the color space, use the overload to specify an NSColorSpace");
|
||||
|
||||
color.GetRgba(out red, out green, out blue, out alpha);
|
||||
#endif
|
||||
return new Color(red, green, blue, alpha);
|
||||
}
|
||||
|
||||
#if __MACOS__
|
||||
public static Color ToColor(this UIColor color, NSColorSpace colorSpace)
|
||||
{
|
||||
var convertedColor = color.UsingColorSpace(colorSpace);
|
||||
|
||||
return convertedColor.ToColor();
|
||||
}
|
||||
#endif
|
||||
|
||||
#if __MOBILE__
|
||||
public static UIColor ToUIColor(this Color color)
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче