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 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
8 changes: 2 additions & 6 deletions eng/scripts/profile-android.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ The $(Configuration) MSBuild property. Defaults to 'Debug'.
Additional command-line arguments to pass to MSBuild. For example,
'-extra /p:AotAssemblies=True' would enable AOT.

.PARAMETER xamarinformsversion
The Xamarin.Forms version to run hello world on

.PARAMETER sleep

The number of seconds to wait between each app launch. Defaults to 3,
Expand Down Expand Up @@ -89,8 +86,7 @@ param
[string] $package,
[string] $configuration = 'Debug',
[string] $extra,
[string] $xamarinformsversion = '4.7.0.1351',
[string] $androidapi = 'android-28',
[string] $androidapi = 'android-30',
[int] $sleep = 3,
[int] $iterations = 10
)
Expand Down Expand Up @@ -172,7 +168,7 @@ Function Build-App{
$isFlutter = -not $project.EndsWith("csproj")
if(-not $isFlutter)
{
& $msbuild $project /v:minimal /nologo /restore /t:Clean,Install /p:Configuration=$configuration /p:XamarinFormsVersion=$xamarinformsversion $extra
& $msbuild $project /v:minimal /nologo /restore /t:Clean,Install /p:Configuration=$configuration $extra
}
else
{
Expand Down
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
14 changes: 6 additions & 8 deletions src/Controls/samples/Controls.Sample/Pages/MainPage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Maui.Controls.Sample.ViewModel;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Controls;

Expand All @@ -9,14 +8,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 +84,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 +115,10 @@ void SetupMauiLayout()

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

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

void SetupCompatibilityLayout()
Expand Down
13 changes: 8 additions & 5 deletions src/Controls/samples/Controls.Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using Maui.Controls.Sample.Pages;
using Maui.Controls.Sample.Services;
using Maui.Controls.Sample.ViewModel;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Maui;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Hosting;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;

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 All @@ -29,7 +31,8 @@ public void Configure(IAppHostBuilder appBuilder)
{"Logging:LogLevel:Default", "Warning"}
});
})
.UseServiceProviderFactory(new DIExtensionsServiceProviderFactory())
.UseMauiServiceProviderFactory(true)
//.UseServiceProviderFactory(new DIExtensionsServiceProviderFactory())
.ConfigureServices((hostingContext, services) =>
{
services.AddSingleton<ITextService, TextService>();
Expand Down
35 changes: 10 additions & 25 deletions src/Controls/samples/Controls.Sample/ViewModel/MainPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,29 @@
using System.Collections.Generic;
using System.Linq;
using System;
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, ITextService textService)
{
Configuration = App.Current.Services.GetService<IConfiguration>();
_configuration = configuration;
_textService = textService;

//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
26 changes: 2 additions & 24 deletions src/Core/src/App.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,14 @@
using System;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.Maui
{
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;
public IServiceProvider? Services { get; private set; }

internal void SetServiceProvider(IServiceProvider provider)
{
_serviceProvider = provider;
SetHandlerContext(provider.GetService<IMauiContext>());
}

internal void SetHandlerContext(IMauiContext? context)
{
_context = context;
Services = provider;
}
}
}
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
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Button/ButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ButtonHandler() : base(ButtonMapper)

}

public ButtonHandler(PropertyMapper mapper) : base(mapper ?? ButtonMapper)
public ButtonHandler(PropertyMapper? mapper = null) : base(mapper ?? ButtonMapper)
{
}
}
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
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Editor/EditorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public EditorHandler() : base(EditorMapper)

}

public EditorHandler(PropertyMapper mapper) : base(mapper ?? EditorMapper)
public EditorHandler(PropertyMapper? mapper = null) : base(mapper ?? EditorMapper)
{

}
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
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Entry/EntryHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public EntryHandler() : base(EntryMapper)

}

public EntryHandler(PropertyMapper mapper) : base(mapper ?? EntryMapper)
public EntryHandler(PropertyMapper? mapper = null) : base(mapper ?? EntryMapper)
{

}
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; }
}
}
Loading