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

Dev/startup di #529

Merged
merged 7 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 1 addition & 3 deletions src/Compatibility/Core/src/Android/AppCompat/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ internal static IVisualElementRenderer CreateRenderer(VisualElement element, Con
try
{
handler = Forms.MauiContext.Handlers.GetHandler(element.GetType());
handler.SetMauiContext(Forms.MauiContext);
Copy link
Member

Choose a reason for hiding this comment

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

I have doubts if we should't move this to IFrameworkElement

}
catch
{
Expand Down Expand Up @@ -340,10 +341,7 @@ internal static IVisualElementRenderer CreateRenderer(VisualElement element, Con
else if (handler is IVisualElementRenderer ver)
renderer = ver;
else if (handler is IAndroidViewHandler vh)
{
vh.SetContext(context);
renderer = new HandlerToRendererShim(vh);
}
}

renderer.SetElement(element);
Expand Down
1 change: 1 addition & 0 deletions src/Compatibility/Core/src/iOS/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ public static IVisualElementRenderer CreateRenderer(VisualElement element)
try
{
handler = Forms.ActivationState.Context.Handlers.GetHandler(element.GetType());
handler.SetMauiContext(Forms.ActivationState.Context);
}
catch
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
namespace Maui.Controls.Sample.Droid
{
[Application]
public class MainApplication : MauiApplication<MyApp>
public class MainApplication : MauiApplication<Startup>
{
public MainApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/samples/Controls.Sample.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Sample.iOS
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate<MyApp>
public class AppDelegate : MauiUIApplicationDelegate<Startup>
{
}
}
7 changes: 1 addition & 6 deletions src/Controls/samples/Controls.Sample/MainWindow.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
using Maui.Controls.Sample.Controls;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;

namespace Maui.Controls.Sample
{
public class MainWindow : Window
{
public MainWindow() : this(App.Current.Services.GetRequiredService<IPage>())
{
}

public MainWindow(IPage page)
{
Page = page;
}
}
}
}
8 changes: 7 additions & 1 deletion src/Controls/samples/Controls.Sample/MyApp.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System;
using Maui.Controls.Sample.Services;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Compatibility;
Expand All @@ -6,7 +8,11 @@ namespace Maui.Controls.Sample
{
public class MyApp : MauiApp
{
// IAppState state
public MyApp(ITextService textService)
{
Console.WriteLine($"The injected text service had a message: '{textService.GetText()}'");
}

public override IWindow CreateWindow(IActivationState state)
{
Forms.Init(state);
Expand Down
13 changes: 6 additions & 7 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ public class MainPage : ContentPage, IPage
{
MainPageViewModel _viewModel;

public MainPage() : this(App.Current.Services.GetService<MainPageViewModel>())
{

}

public MainPage(MainPageViewModel viewModel)
{
BindingContext = _viewModel = viewModel;

SetupMauiLayout();
//SetupCompatibilityLayout();
}
Expand Down Expand Up @@ -89,7 +85,7 @@ void SetupMauiLayout()
};

verticalStack.Add(entry);
verticalStack.Add(new Entry { Text = "Entry", TextColor = Color.DarkRed });
verticalStack.Add(new Entry { Text = "Entry", TextColor = Color.DarkRed, FontFamily = "Dokdo" });
verticalStack.Add(new Entry { IsPassword = true, TextColor = Color.Black });
verticalStack.Add(new Entry { IsTextPredictionEnabled = false });
verticalStack.Add(new Entry { Placeholder = "This should be placeholder text" });
Expand Down Expand Up @@ -120,7 +116,10 @@ void SetupMauiLayout()

verticalStack.Add(new Image() { Source = "dotnet_bot.png" });

Content = verticalStack;
Content = new ScrollView
{
Content = verticalStack
};
}

void SetupCompatibilityLayout()
Expand Down
4 changes: 3 additions & 1 deletion src/Controls/samples/Controls.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using Microsoft.Maui.Controls.Compatibility;

namespace Maui.Controls.Sample
{
Expand All @@ -18,7 +19,8 @@ public class Startup : IStartup
public void Configure(IAppHostBuilder appBuilder)
{
appBuilder
//.RegisterCompatibilityRenderers()
.RegisterCompatibilityRenderers()
.UseMauiApp<MyApp>()
.ConfigureAppConfiguration((hostingContext, config) =>
{
config.AddInMemoryCollection(new Dictionary<string, string>
Expand Down
33 changes: 10 additions & 23 deletions src/Controls/samples/Controls.Sample/ViewModel/MainPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Maui.Controls.Sample.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Maui;

namespace Maui.Controls.Sample.ViewModel
{
public class MainPageViewModel : ViewModelBase
{
private readonly IConfiguration Configuration;
ITextService textService;

public MainPageViewModel() : this(new ITextService[] { App.Current.Services.GetService<ITextService>() })
{
}
readonly IConfiguration _configuration;
readonly ITextService _textService;
string _text;

public MainPageViewModel(IEnumerable<ITextService> textServices)
public MainPageViewModel(IConfiguration configuration, IEnumerable<ITextService> textServices)
{
Configuration = App.Current.Services.GetService<IConfiguration>();
_configuration = configuration;
_textService = textServices.FirstOrDefault();

//var logger = App.Current.Services.GetService<ILogger<MainPageViewModel>>();
Console.WriteLine($"Value from config: {_configuration["MyKey"]}");

//logger.LogInformation("hello");

textService = textServices.FirstOrDefault();
Text = textService.GetText();
Text = _textService.GetText();
}

//public MainPageViewModel(ITextService textService)
//{
// Text = textService.GetText();
//}

string _text;
public string Text
{
get => _text;
set => SetProperty(ref _text, value);
}
}
}
}
6 changes: 6 additions & 0 deletions src/Controls/src/Core/HandlerImpl/SearchBar.Impl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Microsoft.Maui.Controls
{
public partial class SearchBar : ISearchBar
Copy link
Member

Choose a reason for hiding this comment

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

why are you adding this?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because the SearchBar PR did not add this or register it with the handlers. Seems I missed that when reviewing the PR. Without this it actually throes so I am not even sure how it ran locally...

{
}
}
2 changes: 1 addition & 1 deletion src/Controls/src/Core/SearchBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Microsoft.Maui.Controls
{
public class SearchBar : InputView, IFontElement, ITextAlignmentElement, ISearchBarController, IElementConfiguration<SearchBar>
public partial class SearchBar : InputView, IFontElement, ITextAlignmentElement, ISearchBarController, IElementConfiguration<SearchBar>
{
public static readonly BindableProperty SearchCommandProperty = BindableProperty.Create("SearchCommand", typeof(ICommand), typeof(SearchBar), null, propertyChanged: OnCommandChanged);

Expand Down
10 changes: 0 additions & 10 deletions src/Core/src/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ public abstract class App : IApp
IServiceProvider? _serviceProvider;
IMauiContext? _context;

protected App()
{
if (Current != null)
throw new InvalidOperationException($"Only one {nameof(App)} instance is allowed");

Current = this;
}

public static App? Current { get; internal set; }

public IServiceProvider? Services => _serviceProvider;

public IMauiContext? Context => _context;
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Button/ButtonHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public static void MapTextColor(ButtonHandler handler, IButton button)

public static void MapFont(ButtonHandler handler, IButton button)
{
var services = App.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(button, fontManager);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Button/ButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public static void MapPadding(ButtonHandler handler, IButton button)

public static void MapFont(ButtonHandler handler, IButton button)
{
var services = App.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(button, fontManager);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Entry/EntryHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ public static void MapPlaceholder(EntryHandler handler, IEntry entry)

public static void MapFont(EntryHandler handler, IEntry entry)
{
var services = App.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(entry, fontManager);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Entry/EntryHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ void OnTextChanged()

public static void MapFont(EntryHandler handler, IEntry entry)
{
var services = App.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(entry, fontManager);
}
Expand Down
2 changes: 0 additions & 2 deletions src/Core/src/Handlers/IViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ namespace Microsoft.Maui
{
public interface IAndroidViewHandler : IViewHandler
{
void SetContext(Context context);

AView? View { get; }
}
}
2 changes: 2 additions & 0 deletions src/Core/src/Handlers/IViewHandler.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using Microsoft.Maui;

namespace Microsoft.Maui
{
public interface IViewHandler
{
void SetMauiContext(IMauiContext mauiContext);
void SetVirtualView(IView view);
void UpdateValue(string property);
void DisconnectHandler();
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Label/LabelHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static void MapTextDecorations(LabelHandler handler, ILabel label)

public static void MapFont(LabelHandler handler, ILabel label)
{
var services = App.Current?.Services
?? throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(label, fontManager);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Core/src/Handlers/Label/LabelHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public static void MapTextDecorations(LabelHandler handler, ILabel label)

public static void MapFont(LabelHandler handler, ILabel label)
{
var services = App.Current?.Services ??
throw new InvalidOperationException($"Unable to find service provider, the App.Current.Services was null.");
var fontManager = services.GetRequiredService<IFontManager>();
_ = handler.Services ?? throw new InvalidOperationException($"{nameof(Services)} should have been set by base class.");

var fontManager = handler.Services.GetRequiredService<IFontManager>();

handler.TypedNativeView?.UpdateFont(label, fontManager);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Handlers/Layout/LayoutHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,24 @@ public override void SetVirtualView(IView view)

_ = TypedNativeView ?? throw new InvalidOperationException($"{nameof(TypedNativeView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiApp.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

TypedNativeView.CrossPlatformMeasure = VirtualView.Measure;
TypedNativeView.CrossPlatformArrange = VirtualView.Arrange;

foreach (var child in VirtualView.Children)
{
TypedNativeView.AddView(child.ToNative(MauiApp.Current.Context));
TypedNativeView.AddView(child.ToNative(MauiContext));
}
}

public void Add(IView child)
{
_ = TypedNativeView ?? throw new InvalidOperationException($"{nameof(TypedNativeView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiApp.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

TypedNativeView.AddView(child.ToNative(MauiApp.Current.Context!), 0);
TypedNativeView.AddView(child.ToNative(MauiContext), 0);
}

public void Remove(IView child)
Expand Down
8 changes: 4 additions & 4 deletions src/Core/src/Handlers/Layout/LayoutHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ public override void SetVirtualView(IView view)

_ = TypedNativeView ?? throw new InvalidOperationException($"{nameof(TypedNativeView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiApp.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

TypedNativeView.CrossPlatformMeasure = VirtualView.Measure;
TypedNativeView.CrossPlatformArrange = VirtualView.Arrange;

foreach (var child in VirtualView.Children)
{
TypedNativeView.AddSubview(child.ToNative(MauiApp.Current.Context));
TypedNativeView.AddSubview(child.ToNative(MauiContext));
}
}

public void Add(IView child)
{
_ = TypedNativeView ?? throw new InvalidOperationException($"{nameof(TypedNativeView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiApp.Current?.Context ?? throw new InvalidOperationException($"The MauiApp.Current.Context can't be null.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

TypedNativeView.AddSubview(child.ToNative(MauiApp.Current.Context));
TypedNativeView.AddSubview(child.ToNative(MauiContext));
TypedNativeView.SetNeedsLayout();
}

Expand Down
4 changes: 1 addition & 3 deletions src/Core/src/Handlers/View/AbstractViewHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ namespace Microsoft.Maui.Handlers
{
public partial class AbstractViewHandler<TVirtualView, TNativeView> : IAndroidViewHandler
{
public void SetContext(Context context) => Context = context;

public Context? Context { get; private set; }
public Context? Context => MauiContext?.Context;

public void SetFrame(Rectangle frame)
{
Expand Down
Loading