зеркало из https://github.com/DeGsoft/maui-linux.git
Added a util method to get services from the handler (#676)
This commit is contained in:
Родитель
6867350708
Коммит
36f94a28dc
|
@ -73,9 +73,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(ButtonHandler handler, IButton button)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(button, fontManager);
|
||||
}
|
||||
|
|
|
@ -65,9 +65,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(ButtonHandler handler, IButton button)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(button, fontManager);
|
||||
}
|
||||
|
|
|
@ -70,9 +70,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(ButtonHandler handler, IButton button)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(button, fontManager);
|
||||
}
|
||||
|
|
|
@ -76,9 +76,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(DatePickerHandler handler, IDatePicker datePicker)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(datePicker, fontManager);
|
||||
}
|
||||
|
|
|
@ -91,9 +91,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(DatePickerHandler handler, IDatePicker datePicker)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(datePicker, fontManager);
|
||||
}
|
||||
|
|
|
@ -69,9 +69,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(EditorHandler handler, IEditor editor)
|
||||
{
|
||||
var services = handler.Services
|
||||
?? throw new InvalidOperationException($"Unable to find service provider, the handler.Services was null.");
|
||||
var fontManager = services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(editor, fontManager);
|
||||
}
|
||||
|
|
|
@ -110,9 +110,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(EditorHandler handler, IEditor editor)
|
||||
{
|
||||
var services = handler.Services ??
|
||||
throw new InvalidOperationException($"Unable to find service provider, the handler.Services was null.");
|
||||
var fontManager = services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(editor, fontManager);
|
||||
}
|
||||
|
|
|
@ -95,9 +95,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(EntryHandler handler, IEntry entry)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(entry, fontManager);
|
||||
}
|
||||
|
|
|
@ -95,9 +95,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(EntryHandler handler, IEntry entry)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(entry, fontManager);
|
||||
}
|
||||
|
|
|
@ -68,9 +68,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(LabelHandler handler, ILabel label)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(label, fontManager);
|
||||
}
|
||||
|
|
|
@ -26,9 +26,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(LabelHandler handler, ILabel label)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.TextBlock?.UpdateFont(label, fontManager);
|
||||
}
|
||||
|
|
|
@ -53,9 +53,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(LabelHandler handler, ILabel label)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(label, fontManager);
|
||||
}
|
||||
|
|
|
@ -52,9 +52,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(PickerHandler handler, IPicker picker)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(picker, fontManager);
|
||||
}
|
||||
|
|
|
@ -109,9 +109,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(PickerHandler handler, IPicker picker)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(picker, fontManager);
|
||||
}
|
||||
|
|
|
@ -32,9 +32,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(SearchBarHandler handler, ISearchBar searchBar)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(searchBar, fontManager, handler._editText);
|
||||
}
|
||||
|
|
|
@ -34,9 +34,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(SearchBarHandler handler, ISearchBar searchBar)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.QueryEditor?.UpdateFont(searchBar, fontManager);
|
||||
}
|
||||
|
|
|
@ -62,9 +62,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(TimePickerHandler handler, ITimePicker timePicker)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(timePicker, fontManager);
|
||||
}
|
||||
|
|
|
@ -47,9 +47,7 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public static void MapFont(TimePickerHandler handler, ITimePicker timePicker)
|
||||
{
|
||||
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");
|
||||
|
||||
var fontManager = handler.Services.GetRequiredService<IFontManager>();
|
||||
var fontManager = handler.GetRequiredService<IFontManager>();
|
||||
|
||||
handler.NativeView?.UpdateFont(timePicker, fontManager);
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
using System;
|
||||
#if __IOS__
|
||||
using NativeView = UIKit.UIView;
|
||||
#elif __MACOS__
|
||||
|
@ -52,6 +53,8 @@ namespace Microsoft.Maui.Handlers
|
|||
|
||||
public IMauiContext? MauiContext { get; private set; }
|
||||
|
||||
public IServiceProvider? Services => MauiContext?.Services;
|
||||
|
||||
public object? NativeView { get; private protected set; }
|
||||
|
||||
public IView? VirtualView { get; private protected set; }
|
||||
|
|
|
@ -41,8 +41,6 @@ namespace Microsoft.Maui.Handlers
|
|||
private set => base.NativeView = value;
|
||||
}
|
||||
|
||||
public IServiceProvider? Services => MauiContext?.Services;
|
||||
|
||||
public override void SetVirtualView(IView view)
|
||||
{
|
||||
_ = view ?? throw new ArgumentNullException(nameof(view));
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Maui.Handlers;
|
||||
|
||||
namespace Microsoft.Maui
|
||||
{
|
||||
internal static class ViewHandlerExtensions
|
||||
{
|
||||
public static T GetRequiredService<T>(this ViewHandler handler)
|
||||
where T : notnull
|
||||
{
|
||||
var context = handler.MauiContext ??
|
||||
throw new InvalidOperationException($"Unable to find the context. The {nameof(ViewHandler.MauiContext)} property should have been set by the host.");
|
||||
|
||||
var services = context?.Services ??
|
||||
throw new InvalidOperationException($"Unable to find the service provider. The {nameof(ViewHandler.MauiContext)} property should have been set by the host.");
|
||||
|
||||
var service = services.GetRequiredService<T>();
|
||||
|
||||
return service;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +1,6 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Maui.Hosting.Internal;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
|
@ -36,5 +39,55 @@ namespace Microsoft.Maui.UnitTests
|
|||
Assert.Equal(2, handlerStub.ConnectHandlerCount);
|
||||
Assert.Equal(2, handlerStub.DisconnectHandlerCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRequiredServiceThrowsOnNoContext()
|
||||
{
|
||||
HandlerStub handlerStub = new HandlerStub();
|
||||
|
||||
Assert.Null(handlerStub.MauiContext);
|
||||
|
||||
var ex = Assert.Throws<InvalidOperationException>(() => handlerStub.GetRequiredService<IFooService>());
|
||||
|
||||
Assert.Contains("the context", ex.Message);
|
||||
Assert.Contains("MauiContext", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRequiredServiceThrowsOnNoServices()
|
||||
{
|
||||
HandlerStub handlerStub = new HandlerStub();
|
||||
|
||||
handlerStub.SetMauiContext(new InvalidHandlersContextStub());
|
||||
|
||||
Assert.NotNull(handlerStub.MauiContext);
|
||||
Assert.Null(handlerStub.MauiContext.Services);
|
||||
|
||||
var ex = Assert.Throws<InvalidOperationException>(() => handlerStub.GetRequiredService<IFooService>());
|
||||
|
||||
Assert.Contains("the service provider", ex.Message);
|
||||
Assert.Contains("MauiContext", ex.Message);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetRequiredServiceRetrievesService()
|
||||
{
|
||||
HandlerStub handlerStub = new HandlerStub();
|
||||
|
||||
var collection = new MauiServiceCollection();
|
||||
collection.TryAddSingleton<IMauiHandlersServiceProvider>(new MauiHandlersServiceProvider(new MauiServiceCollection()));
|
||||
collection.TryAddSingleton<IFooService, FooService>();
|
||||
|
||||
var provider = new MauiServiceProvider(collection, false);
|
||||
|
||||
handlerStub.SetMauiContext(new HandlersContextStub(provider));
|
||||
|
||||
Assert.NotNull(handlerStub.MauiContext);
|
||||
Assert.NotNull(handlerStub.MauiContext.Services);
|
||||
|
||||
var foo = handlerStub.GetRequiredService<IFooService>();
|
||||
|
||||
Assert.IsType<FooService>(foo);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Maui.Hosting;
|
||||
using Microsoft.Maui.Tests;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
|
|
|
@ -4,7 +4,6 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using Microsoft.Maui.Handlers;
|
||||
using Microsoft.Maui.Hosting;
|
||||
using Microsoft.Maui.Hosting.Internal;
|
||||
using Microsoft.Maui.Tests;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
|
|
|
@ -6,7 +6,6 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Maui.Hosting;
|
||||
using Microsoft.Maui.Tests;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Maui.Hosting;
|
||||
using Microsoft.Maui.Tests;
|
||||
using Xunit;
|
||||
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class ApplicationStub : IApplication
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.Maui.Handlers;
|
||||
|
||||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class ButtonHandlerStub : ButtonHandler
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using Microsoft.Maui.Controls;
|
||||
|
||||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class ButtonStub : View, IButton
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class HandlersContextStub : IMauiContext
|
||||
{
|
||||
|
@ -17,4 +17,4 @@ namespace Microsoft.Maui.Tests
|
|||
|
||||
public IMauiHandlersServiceProvider Handlers => _mauiHandlersServiceProvider;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
interface IViewStub : IView
|
||||
{
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class InvalidHandlersContextStub : IMauiContext
|
||||
{
|
||||
public InvalidHandlersContextStub()
|
||||
{
|
||||
}
|
||||
|
||||
public IServiceProvider Services => null!;
|
||||
|
||||
public IMauiHandlersServiceProvider Handlers => null!;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class NativeViewStub
|
||||
{
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
interface IFooService
|
||||
{
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
using Microsoft.Maui.Handlers;
|
||||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class ViewHandlerStub : ViewHandler<IViewStub, NativeViewStub>
|
||||
{
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
using System;
|
||||
using Microsoft.Maui.Primitives;
|
||||
|
||||
namespace Microsoft.Maui.Tests
|
||||
namespace Microsoft.Maui.UnitTests
|
||||
{
|
||||
class ViewStub : IViewStub
|
||||
{
|
||||
|
|
Загрузка…
Ссылка в новой задаче