Skip to content

Commit e9eb388

Browse files
Implement .NET 6 features for templates. (#3018)
Co-authored-by: Matthew Leibowitz <[email protected]>
1 parent 42af2a7 commit e9eb388

30 files changed

+271
-308
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
using Microsoft.Maui;
2-
using Microsoft.Maui.Controls;
3-
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
4-
using Application = Microsoft.Maui.Controls.Application;
1+
namespace MauiApp._1;
52

6-
namespace MauiApp._1
3+
public partial class App : Application
74
{
8-
public partial class App : Application
5+
public App()
96
{
10-
public App()
11-
{
12-
InitializeComponent();
7+
InitializeComponent();
138

14-
MainPage = new MainPage();
15-
}
9+
MainPage = new MainPage();
1610
}
1711
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
using System;
1+
namespace MauiApp._1.Data;
22

3-
namespace MauiApp._1.Data
3+
public class WeatherForecast
44
{
5-
public class WeatherForecast
6-
{
7-
public DateTime Date { get; set; }
5+
public DateTime Date { get; set; }
86

9-
public int TemperatureC { get; set; }
7+
public int TemperatureC { get; set; }
108

11-
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
1210

13-
public string Summary { get; set; }
14-
}
11+
public string Summary { get; set; }
1512
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
1-
using System;
2-
using System.Linq;
3-
using System.Threading.Tasks;
1+
namespace MauiApp._1.Data;
42

5-
namespace MauiApp._1.Data
3+
public class WeatherForecastService
64
{
7-
public class WeatherForecastService
5+
private static readonly string[] Summaries = new[]
86
{
9-
private static readonly string[] Summaries = new[]
10-
{
11-
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
12-
};
7+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
8+
};
139

14-
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
10+
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
11+
{
12+
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
1513
{
16-
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
17-
{
18-
Date = startDate.AddDays(index),
19-
TemperatureC = Random.Shared.Next(-20, 55),
20-
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
21-
}).ToArray());
22-
}
14+
Date = startDate.AddDays(index),
15+
TemperatureC = Random.Shared.Next(-20, 55),
16+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
17+
}).ToArray());
2318
}
2419
}
20+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
global using Microsoft.Extensions.DependencyInjection;
2+
global using Microsoft.Maui;
3+
global using Microsoft.Maui.Controls;
4+
global using Microsoft.Maui.Controls.Hosting;
5+
global using Microsoft.Maui.Controls.Xaml;
6+
global using Microsoft.Maui.Essentials;
7+
global using Microsoft.Maui.Hosting;
8+
global using System;
9+
global using System.Collections.Generic;
10+
global using System.IO;
11+
global using System.Linq;
12+
global using System.Net.Http;
13+
global using System.Threading;
14+
global using System.Threading.Tasks;
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
using System;
2-
using Microsoft.Maui.Controls;
1+
namespace MauiApp._1;
32

4-
namespace MauiApp._1
3+
public partial class MainPage : ContentPage
54
{
6-
public partial class MainPage : ContentPage
5+
public MainPage()
76
{
8-
public MainPage()
9-
{
10-
InitializeComponent();
11-
}
7+
InitializeComponent();
128
}
139
}
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
using Microsoft.AspNetCore.Components.WebView.Maui;
2-
using Microsoft.Extensions.DependencyInjection;
3-
using Microsoft.Maui;
4-
using Microsoft.Maui.Hosting;
5-
using Microsoft.Maui.Controls.Compatibility;
6-
using Microsoft.Maui.Controls.Hosting;
72
using MauiApp._1.Data;
83

9-
namespace MauiApp._1
4+
namespace MauiApp._1;
5+
6+
public static class MauiProgram
107
{
11-
public static class MauiProgram
8+
public static MauiApp CreateMauiApp()
129
{
13-
public static MauiApp CreateMauiApp()
14-
{
15-
var builder = MauiApp.CreateBuilder();
16-
builder
17-
.RegisterBlazorMauiWebView()
18-
.UseMauiApp<App>()
19-
.ConfigureFonts(fonts =>
20-
{
21-
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
22-
});
10+
var builder = MauiApp.CreateBuilder();
11+
builder
12+
.RegisterBlazorMauiWebView()
13+
.UseMauiApp<App>()
14+
.ConfigureFonts(fonts =>
15+
{
16+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
17+
});
2318

24-
builder.Services.AddBlazorWebView();
25-
builder.Services.AddSingleton<WeatherForecastService>();
19+
builder.Services.AddBlazorWebView();
20+
builder.Services.AddSingleton<WeatherForecastService>();
2621

27-
return builder.Build();
28-
}
22+
return builder.Build();
2923
}
30-
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
using Android.App;
22
using Android.Content.PM;
3-
using Microsoft.Maui;
3+
using Android.OS;
44

5-
namespace MauiApp._1
5+
namespace MauiApp._1;
6+
7+
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
8+
public class MainActivity : MauiAppCompatActivity
69
{
7-
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize)]
8-
public class MainActivity : MauiAppCompatActivity
10+
protected override void OnCreate(Bundle savedInstanceState)
911
{
12+
base.OnCreate(savedInstanceState);
13+
Platform.Init(this, savedInstanceState);
14+
}
15+
16+
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
17+
{
18+
Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
19+
20+
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
1021
}
11-
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
using System;
2-
using Android.App;
1+
using Android.App;
32
using Android.Runtime;
4-
using Microsoft.Maui;
5-
using Microsoft.Maui.Hosting;
63

7-
namespace MauiApp._1
4+
namespace MauiApp._1;
5+
6+
[Application]
7+
public class MainApplication : MauiApplication
88
{
9-
[Application]
10-
public class MainApplication : MauiApplication
9+
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
10+
: base(handle, ownership)
1111
{
12-
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
13-
: base(handle, ownership)
14-
{
15-
}
16-
17-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
1812
}
19-
}
13+
14+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using Foundation;
2-
using Microsoft.Maui;
3-
using Microsoft.Maui.Hosting;
42

5-
namespace MauiApp._1
3+
namespace MauiApp._1;
4+
5+
[Register("AppDelegate")]
6+
public class AppDelegate : MauiUIApplicationDelegate
67
{
7-
[Register("AppDelegate")]
8-
public class AppDelegate : MauiUIApplicationDelegate
9-
{
10-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
11-
}
12-
}
8+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using UIKit;
22

3-
namespace MauiApp._1
3+
namespace MauiApp._1;
4+
5+
public class Program
46
{
5-
public class Program
7+
// This is the main entry point of the application.
8+
static void Main(string[] args)
69
{
7-
// This is the main entry point of the application.
8-
static void Main(string[] args)
9-
{
10-
// if you want to use a different Application Delegate class from "AppDelegate"
11-
// you can specify it here.
12-
UIApplication.Main(args, null, typeof(AppDelegate));
13-
}
10+
// if you want to use a different Application Delegate class from "AppDelegate"
11+
// you can specify it here.
12+
UIApplication.Main(args, null, typeof(AppDelegate));
1413
}
1514
}
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,31 @@
1-
using Microsoft.Maui;
2-
using Microsoft.Maui.Hosting;
3-
using Microsoft.UI.Xaml;
4-
using Windows.ApplicationModel;
1+
using Microsoft.UI.Xaml;
52

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

9-
namespace MauiApp._1.WinUI
6+
namespace MauiApp._1.WinUI;
7+
8+
/// <summary>
9+
/// Provides application-specific behavior to supplement the default Application class.
10+
/// </summary>
11+
public partial class App : MauiWinUIApplication
1012
{
1113
/// <summary>
12-
/// Provides application-specific behavior to supplement the default Application class.
14+
/// Initializes the singleton application object. This is the first line of authored code
15+
/// executed, and as such is the logical equivalent of main() or WinMain().
1316
/// </summary>
14-
public partial class App : MauiWinUIApplication
17+
public App()
1518
{
16-
/// <summary>
17-
/// Initializes the singleton application object. This is the first line of authored code
18-
/// executed, and as such is the logical equivalent of main() or WinMain().
19-
/// </summary>
20-
public App()
21-
{
22-
this.InitializeComponent();
23-
}
19+
this.InitializeComponent();
20+
}
2421

25-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
22+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
2623

27-
protected override void OnLaunched(LaunchActivatedEventArgs args)
28-
{
29-
base.OnLaunched(args);
24+
protected override void OnLaunched(LaunchActivatedEventArgs args)
25+
{
26+
base.OnLaunched(args);
3027

31-
Microsoft.Maui.Essentials.Platform.OnLaunched(args);
32-
}
28+
Platform.OnLaunched(args);
3329
}
3430
}
31+
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
using Foundation;
2-
using Microsoft.Maui;
3-
using Microsoft.Maui.Hosting;
42

5-
namespace MauiApp._1
3+
namespace MauiApp._1;
4+
5+
[Register("AppDelegate")]
6+
public class AppDelegate : MauiUIApplicationDelegate
67
{
7-
[Register("AppDelegate")]
8-
public class AppDelegate : MauiUIApplicationDelegate
9-
{
10-
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
11-
}
12-
}
8+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
9+
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
using UIKit;
22

3-
namespace MauiApp._1
3+
namespace MauiApp._1;
4+
5+
public class Program
46
{
5-
public class Program
7+
// This is the main entry point of the application.
8+
static void Main(string[] args)
69
{
7-
// This is the main entry point of the application.
8-
static void Main(string[] args)
9-
{
10-
// if you want to use a different Application Delegate class from "AppDelegate"
11-
// you can specify it here.
12-
UIApplication.Main(args, null, typeof(AppDelegate));
13-
}
10+
// if you want to use a different Application Delegate class from "AppDelegate"
11+
// you can specify it here.
12+
UIApplication.Main(args, null, typeof(AppDelegate));
1413
}
15-
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using System;
1+
namespace MauiLib1;
22

3-
namespace MauiLib1
3+
// All the code in this file is included in all platforms.
4+
public class Class1
45
{
5-
// All the code in this file is included in all platforms.
6-
public class Class1
7-
{
8-
}
9-
}
6+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
using System;
1+
namespace MauiLib1;
22

3-
namespace MauiLib1
3+
// All the code in this file is only included on Android.
4+
public class PlatformClass1
45
{
5-
// All the code in this file is only included on Android.
6-
public class PlatformClass1
7-
{
8-
}
9-
}
6+
}

0 commit comments

Comments
 (0)