Skip to content

Commit

Permalink
Tutorial 2: update code
Browse files Browse the repository at this point in the history
  • Loading branch information
adegeo committed Mar 21, 2024
1 parent ac4f1eb commit 6f472ef
Show file tree
Hide file tree
Showing 30 changed files with 234 additions and 251 deletions.
Binary file modified 8.0/Tutorials/ConvertToMvvm/app_after.zip
Binary file not shown.
Binary file modified 8.0/Tutorials/ConvertToMvvm/app_before.zip
Binary file not shown.
17 changes: 9 additions & 8 deletions 8.0/Tutorials/ConvertToMvvm/code/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
namespace Notes;

public partial class App : Application
namespace Notes
{
public App()
{
InitializeComponent();
public partial class App : Application
{
public App()
{
InitializeComponent();

MainPage = new AppShell();
}
MainPage = new AppShell();
}
}
}
34 changes: 20 additions & 14 deletions 8.0/Tutorials/ConvertToMvvm/code/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
namespace Notes;
using Microsoft.Extensions.Logging;

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

return builder.Build();
}
#if DEBUG
builder.Logging.AddDebug();
#endif

return builder.Build();
}
}
}
14 changes: 7 additions & 7 deletions 8.0/Tutorials/ConvertToMvvm/code/Models/Note.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ internal class Note
public string Text { get; set; }
public DateTime Date { get; set; }

public void Save() =>
File.WriteAllText(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename), Text);

public void Delete() =>
File.Delete(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename));

public Note()
{
Filename = $"{Path.GetRandomFileName()}.notes.txt";
Date = DateTime.Now;
Text = "";
}

public void Save() =>
File.WriteAllText(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename), Text);

public void Delete() =>
File.Delete(System.IO.Path.Combine(FileSystem.AppDataDirectory, Filename));

public static Note Load(string filename)
{
filename = System.IO.Path.Combine(FileSystem.AppDataDirectory, filename);
Expand Down Expand Up @@ -52,4 +52,4 @@ public static IEnumerable<Note> LoadAll()
// With the final collection of notes, order them by date
.OrderByDescending(note => note.Date);
}
}
}
7 changes: 4 additions & 3 deletions 8.0/Tutorials/ConvertToMvvm/code/Notes.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<UseMaui>true</UseMaui>
<SingleProject>true</SingleProject>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<!-- Display name -->
<ApplicationTitle>Notes</ApplicationTitle>
Expand Down Expand Up @@ -46,7 +47,7 @@

<!-- Images -->
<MauiImage Include="Resources\Images\*" />
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
<MauiImage Update="Resources\Images\dotnet_bot.png" Resize="True" BaseSize="300,185" />

<!-- Custom Fonts -->
<MauiFont Include="Resources\Fonts\*" />
Expand All @@ -56,10 +57,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
<PackageReference Include="Microsoft.Maui.Controls" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Maui.Controls.Compatibility" Version="$(MauiVersion)" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
18 changes: 9 additions & 9 deletions 8.0/Tutorials/ConvertToMvvm/code/Notes.sln
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.4.32916.344
VisualStudioVersion = 17.9.34714.143
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notes", "Notes.csproj", "{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notes", "Notes.csproj", "{810B17FB-74AD-4D14-8893-ECAC2C293EEB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}.Release|Any CPU.Build.0 = Release|Any CPU
{0CE9FCF7-16AA-467F-8F04-FF175FBC288D}.Release|Any CPU.Deploy.0 = Release|Any CPU
{810B17FB-74AD-4D14-8893-ECAC2C293EEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{810B17FB-74AD-4D14-8893-ECAC2C293EEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{810B17FB-74AD-4D14-8893-ECAC2C293EEB}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{810B17FB-74AD-4D14-8893-ECAC2C293EEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{810B17FB-74AD-4D14-8893-ECAC2C293EEB}.Release|Any CPU.Build.0 = Release|Any CPU
{810B17FB-74AD-4D14-8893-ECAC2C293EEB}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C177C5C5-C67F-4457-BD0A-DF85476DC946}
SolutionGuid = {7E9D46F9-8024-47CF-A375-F6D0CA1965BD}
EndGlobalSection
EndGlobal
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
using Android.Content.PM;
using Android.OS;

namespace Notes;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
namespace Notes
{
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using Android.App;
using Android.Runtime;

namespace Notes;

[Application]
public class MainApplication : MauiApplication
namespace Notes
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}
[Application]
public class MainApplication : MauiApplication
{
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,9 +1,10 @@
using Foundation;

namespace Notes;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
namespace Notes
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- See https://aka.ms/maui-publish-app-store#add-entitlements for more information about adding entitlements.-->
<dict>
<!-- App Sandbox must be enabled to distribute a MacCatalyst app through the Mac App Store. -->
<key>com.apple.security.app-sandbox</key>
<true/>
<!-- When App Sandbox is enabled, this value is required to open outgoing network connections. -->
<key>com.apple.security.network.client</key>
<true/>
</dict>
</plist>

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<!-- The Mac App Store requires you specify if the app uses encryption. -->
<!-- Please consult https://developer.apple.com/documentation/bundleresources/information_property_list/itsappusesnonexemptencryption -->
<!-- <key>ITSAppUsesNonExemptEncryption</key> -->
<!-- Please indicate <true/> or <false/> here. -->

<!-- Specify the category for your app here. -->
<!-- Please consult https://developer.apple.com/documentation/bundleresources/information_property_list/lsapplicationcategorytype -->
<!-- <key>LSApplicationCategoryType</key> -->
<!-- <string>public.app-category.YOUR-CATEGORY-HERE</string> -->
<key>UIDeviceFamily</key>
<array>
<integer>1</integer>
<integer>2</integer>
</array>
<key>UIRequiredDeviceCapabilities</key>
Expand Down
21 changes: 11 additions & 10 deletions 8.0/Tutorials/ConvertToMvvm/code/Platforms/MacCatalyst/Program.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using ObjCRuntime;
using UIKit;

namespace Notes;

public class Program
namespace Notes
{
// 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));
}
public class Program
{
// 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));
}
}
}
21 changes: 11 additions & 10 deletions 8.0/Tutorials/ConvertToMvvm/code/Platforms/Tizen/Main.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
using System;

namespace Notes;

class Program : MauiApplication
namespace Notes
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
internal class Program : MauiApplication
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
static void Main(string[] args)
{
var app = new Program();
app.Run(args);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="maui-application-id-placeholder" version="0.0.0" api-version="7" xmlns="http://tizen.org/ns/packages">
<manifest package="maui-application-id-placeholder" version="0.0.0" api-version="8" xmlns="http://tizen.org/ns/packages">
<profile name="common" />
<ui-application appid="maui-application-id-placeholder" exec="Notes.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
<label>maui-application-title-placeholder</label>
Expand Down
33 changes: 17 additions & 16 deletions 8.0/Tutorials/ConvertToMvvm/code/Platforms/Windows/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace Notes.WinUI;

/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
namespace Notes.WinUI
{
/// <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();
}
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : MauiWinUIApplication
{
/// <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();
}

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
<Package
xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap rescap">

<Identity Name="maui-package-name-placeholder" Publisher="CN=User Name" Version="0.0.0.0" />

<mp:PhoneIdentity PhoneProductId="B97B039E-D893-4BE7-AAF0-5D6D1EBFCE2A" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

<Properties>
<DisplayName>$placeholder$</DisplayName>
<PublisherDisplayName>User Name</PublisherDisplayName>
Expand Down
11 changes: 6 additions & 5 deletions 8.0/Tutorials/ConvertToMvvm/code/Platforms/iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Foundation;

namespace Notes;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
namespace Notes
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
Loading

0 comments on commit 6f472ef

Please sign in to comment.