Skip to content

Commit f9f8b0d

Browse files
Update samples to .NET 7
Besides simply changing `$(TargetFramwork)` to `net7.0-android`... We also can use `RequireViewById` for better C# nullability checks.
1 parent be4df11 commit f9f8b0d

File tree

8 files changed

+20
-31
lines changed

8 files changed

+20
-31
lines changed

AIDLDemo/AIDLDemo/AIDLDemo.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0-android</TargetFramework>
3+
<TargetFramework>net7.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
55
<OutputType>Exe</OutputType>
66
<Nullable>enable</Nullable>

AIDLDemo/AIDLDemo/Activity1.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ protected override void OnStart ()
1717
base.OnStart ();
1818
InitService ();
1919

20-
var button1 = FindViewById<Button> (Resource.Id.buttonCalc);
21-
ArgumentNullException.ThrowIfNull(button1, nameof (button1));
22-
20+
var button1 = RequireViewById<Button> (Resource.Id.buttonCalc);
2321
button1.Click += (sender, e) => {
2422
if (Service != null) {
2523
var text1 = FindViewById<EditText> (Resource.Id.value1);

ActivityLifecycle/ActivityLifecycle.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0-android</TargetFramework>
3+
<TargetFramework>net7.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
55
<OutputType>Exe</OutputType>
66
<Nullable>enable</Nullable>

ActivityLifecycle/MainActivity.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,26 @@ protected override void OnCreate(Bundle? bundle)
1717
base.OnCreate(bundle);
1818
SetContentView(Resource.Layout.activity_main);
1919

20-
var button = FindViewById<Button>(Resource.Id.myButton);
21-
if (button != null)
20+
var button = RequireViewById<Button>(Resource.Id.myButton);
21+
button.Click += (sender, args) =>
2222
{
23-
button.Click += (sender, args) =>
24-
{
25-
var intent = new Intent(this, typeof(SecondActivity));
26-
StartActivity(intent);
27-
};
28-
}
23+
var intent = new Intent(this, typeof(SecondActivity));
24+
StartActivity(intent);
25+
};
2926

3027
if (bundle != null)
3128
{
3229
_counter = bundle.GetInt("click_count", 0);
3330
Log.Debug(GetType().FullName, "Activity A - Recovered instance state");
3431
}
3532

36-
var clickbutton = FindViewById<Button>(Resource.Id.clickButton);
37-
if (clickbutton != null)
33+
var clickbutton = RequireViewById<Button>(Resource.Id.clickButton);
34+
clickbutton.Text = Resources?.GetString(Resource.String.counterbutton_text, _counter);
35+
clickbutton.Click += (sender, args) =>
3836
{
37+
_counter++;
3938
clickbutton.Text = Resources?.GetString(Resource.String.counterbutton_text, _counter);
40-
clickbutton.Click += (sender, args) =>
41-
{
42-
_counter++;
43-
clickbutton.Text = Resources?.GetString(Resource.String.counterbutton_text, _counter);
44-
};
45-
}
39+
};
4640
}
4741

4842
protected override void OnSaveInstanceState(Bundle outState)

Button/Button/Button.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0-android</TargetFramework>
3+
<TargetFramework>net7.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>21</SupportedOSPlatformVersion>
55
<OutputType>Exe</OutputType>
66
<Nullable>enable</Nullable>

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# MonoDroid (Xamarin.Android) samples
22

3-
This branch contains samples ported to .NET 6.
3+
This branch contains samples ported to .NET 7.
44

55
See the [.NET MAUI Installation docs](https://docs.microsoft.com/en-us/dotnet/maui/get-started/installation) for setup instructions.
66

77
This repository contains Mono for Android samples, showing usage of various
88
Android API wrappers from C#. Visit the [Android Sample Gallery](https://docs.microsoft.com/samples/browse/?term=Xamarin.Android)
99
to download individual samples.
1010

11-
## Tips for .NET 6 Migration
11+
## Tips for .NET 7 Migration
1212

13-
The goal here is to fully "modernize" the template for .NET 6 and C# 10.
13+
The goal here is to fully "modernize" the template for .NET 7 and C# 11.
1414

1515
Compare a `dotnet new android` template named the same as the existing project.
1616

TextSwitcher/TextSwitcher/MainActivity.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ protected override void OnCreate(Bundle? savedInstanceState)
1414
SetContentView(Resource.Layout.Main);
1515

1616
// Get the TextSwitcher view from the layout
17-
var mSwitcher = FindViewById<Android.Widget.TextSwitcher>(Resource.Id.switcher);
18-
ArgumentNullException.ThrowIfNull(mSwitcher);
17+
var mSwitcher = RequireViewById<Android.Widget.TextSwitcher>(Resource.Id.switcher);
1918

2019
// BEGIN_INCLUDE(setup)
2120
// Set the factory used to create TextViews to switch between.
@@ -36,9 +35,7 @@ protected override void OnCreate(Bundle? savedInstanceState)
3635
* the new value is displayed in the TextSwitcher. The change of text is
3736
* automatically animated using the in/out animations set above.
3837
*/
39-
var nextButton = FindViewById<Button>(Resource.Id.button);
40-
ArgumentNullException.ThrowIfNull(nextButton);
41-
38+
var nextButton = RequireViewById<Button>(Resource.Id.button);
4239
nextButton.Click += (sender, e) =>
4340
{
4441
mCounter++;

TextSwitcher/TextSwitcher/TextSwitcher.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net6.0-android</TargetFramework>
3+
<TargetFramework>net7.0-android</TargetFramework>
44
<SupportedOSPlatformVersion>23</SupportedOSPlatformVersion>
55
<OutputType>Exe</OutputType>
66
<Nullable>enable</Nullable>

0 commit comments

Comments
 (0)