Skip to content

Commit

Permalink
Implement .NET 6 features for templates. (#3018)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
  • Loading branch information
jamesmontemagno and mattleibow authored Nov 2, 2021
1 parent 42af2a7 commit e9eb388
Show file tree
Hide file tree
Showing 30 changed files with 271 additions and 308 deletions.
16 changes: 5 additions & 11 deletions src/Templates/src/templates/maui-blazor/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
using Application = Microsoft.Maui.Controls.Application;
namespace MauiApp._1;

namespace MauiApp._1
public partial class App : Application
{
public partial class App : Application
public App()
{
public App()
{
InitializeComponent();
InitializeComponent();

MainPage = new MainPage();
}
MainPage = new MainPage();
}
}
15 changes: 6 additions & 9 deletions src/Templates/src/templates/maui-blazor/Data/WeatherForecast.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
namespace MauiApp._1.Data;

namespace MauiApp._1.Data
public class WeatherForecast
{
public class WeatherForecast
{
public DateTime Date { get; set; }
public DateTime Date { get; set; }

public int TemperatureC { get; set; }
public int TemperatureC { get; set; }

public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

public string Summary { get; set; }
}
public string Summary { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using System;
using System.Linq;
using System.Threading.Tasks;
namespace MauiApp._1.Data;

namespace MauiApp._1.Data
public class WeatherForecastService
{
public class WeatherForecastService
private static readonly string[] Summaries = new[]
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
Date = startDate.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
}).ToArray());
}
}

14 changes: 14 additions & 0 deletions src/Templates/src/templates/maui-blazor/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Maui;
global using Microsoft.Maui.Controls;
global using Microsoft.Maui.Controls.Hosting;
global using Microsoft.Maui.Controls.Xaml;
global using Microsoft.Maui.Essentials;
global using Microsoft.Maui.Hosting;
global using System;
global using System.Collections.Generic;
global using System.IO;
global using System.Linq;
global using System.Net.Http;
global using System.Threading;
global using System.Threading.Tasks;
12 changes: 4 additions & 8 deletions src/Templates/src/templates/maui-blazor/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;
using Microsoft.Maui.Controls;
namespace MauiApp._1;

namespace MauiApp._1
public partial class MainPage : ContentPage
{
public partial class MainPage : ContentPage
public MainPage()
{
public MainPage()
{
InitializeComponent();
}
InitializeComponent();
}
}
38 changes: 16 additions & 22 deletions src/Templates/src/templates/maui-blazor/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
using Microsoft.AspNetCore.Components.WebView.Maui;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.Maui.Controls.Compatibility;
using Microsoft.Maui.Controls.Hosting;
using MauiApp._1.Data;

namespace MauiApp._1
namespace MauiApp._1;

public static class MauiProgram
{
public static class MauiProgram
public static MauiApp CreateMauiApp()
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.RegisterBlazorMauiWebView()
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});
var builder = MauiApp.CreateBuilder();
builder
.RegisterBlazorMauiWebView()
.UseMauiApp<App>()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
});

builder.Services.AddBlazorWebView();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddBlazorWebView();
builder.Services.AddSingleton<WeatherForecastService>();

return builder.Build();
}
return builder.Build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using Android.App;
using Android.Content.PM;
using Microsoft.Maui;
using Android.OS;

namespace MauiApp._1
namespace MauiApp._1;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : MauiAppCompatActivity
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
public class MainActivity : MauiAppCompatActivity
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Platform.Init(this, savedInstanceState);
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System;
using Android.App;
using Android.App;
using Android.Runtime;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;

namespace MauiApp._1
namespace MauiApp._1;

[Application]
public class MainApplication : MauiApplication
{
[Application]
public class MainApplication : MauiApplication
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Foundation;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;

namespace MauiApp._1
namespace MauiApp._1;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using UIKit;

namespace MauiApp._1
namespace MauiApp._1;

public class Program
{
public class Program
// This is the main entry point of the application.
static void Main(string[] args)
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using Microsoft.UI.Xaml;
using Windows.ApplicationModel;
using Microsoft.UI.Xaml;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace MauiApp._1.WinUI
namespace MauiApp._1.WinUI;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public partial class App : MauiWinUIApplication
public App()
{
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
this.InitializeComponent();
}
this.InitializeComponent();
}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

protected override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
base.OnLaunched(args);

Microsoft.Maui.Essentials.Platform.OnLaunched(args);
}
Platform.OnLaunched(args);
}
}

Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
using Foundation;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;

namespace MauiApp._1
namespace MauiApp._1;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
19 changes: 9 additions & 10 deletions src/Templates/src/templates/maui-blazor/Platforms/iOS/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using UIKit;

namespace MauiApp._1
namespace MauiApp._1;

public class Program
{
public class Program
// This is the main entry point of the application.
static void Main(string[] args)
{
// This is the main entry point of the application.
static void Main(string[] args)
{
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
// if you want to use a different Application Delegate class from "AppDelegate"
// you can specify it here.
UIApplication.Main(args, null, typeof(AppDelegate));
}
}
}
11 changes: 4 additions & 7 deletions src/Templates/src/templates/maui-lib/Class1.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
namespace MauiLib1;

namespace MauiLib1
// All the code in this file is included in all platforms.
public class Class1
{
// All the code in this file is included in all platforms.
public class Class1
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using System;
namespace MauiLib1;

namespace MauiLib1
// All the code in this file is only included on Android.
public class PlatformClass1
{
// All the code in this file is only included on Android.
public class PlatformClass1
{
}
}
}
Loading

0 comments on commit e9eb388

Please sign in to comment.