diff --git a/.github/instructions/codestyle.instructions.md b/.github/instructions/codestyle.instructions.md index 5851398d85..71e34feb1a 100644 --- a/.github/instructions/codestyle.instructions.md +++ b/.github/instructions/codestyle.instructions.md @@ -235,6 +235,28 @@ This guide provides a set of best practices and coding standards for writing C# } ``` +## Argument Validation: + +- Prefer `ArgumentNullException.ThrowIfNull` over manual null checks: + ```csharp + // Good: Concise null validation + public void Write(byte[] buffer) + { + ArgumentNullException.ThrowIfNull(buffer); + // Use buffer safely here + } + + // Avoid: Verbose manual null check + public void Write(byte[] buffer) + { + if (buffer is null) + { + throw new ArgumentNullException(nameof(buffer)); + } + // Use buffer safely here + } + ``` + ## Safe Operations: - Use Try methods for safer operations: @@ -578,4 +600,4 @@ Here Key Components of the Try Pattern: 4. Null Analysis Attribute: [NotNullWhen(true)] (or [MaybeNullWhen(false)]) informs the compiler that if the method returns true, the out parameter is guaranteed to be non-null. This pattern allows for high-performance retrieval or parsing without throwing exceptions for expected failures. It also allows cleaner call sites by eliminating the need for null-checking the output variable within the if block, as seen in Dictionary.TryGetValue. -``` + diff --git a/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs b/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs index 12721c1b46..33ced3a189 100644 --- a/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs +++ b/samples/CommunityToolkit.Maui.Sample/AppShell.xaml.cs @@ -125,6 +125,7 @@ public partial class AppShell : Shell CreateViewModelMapping(), CreateViewModelMapping(), CreateViewModelMapping(), + CreateViewModelMapping(), CreateViewModelMapping(), CreateViewModelMapping(), CreateViewModelMapping(), diff --git a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs index 78f8c2bcf8..6506108af9 100644 --- a/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs +++ b/samples/CommunityToolkit.Maui.Sample/MauiProgram.cs @@ -123,6 +123,9 @@ public static MauiApp CreateMauiApp() builder.Services.AddHttpClient() .AddStandardResilienceHandler(static options => options.Retry = new MobileHttpRetryStrategyOptions()); + builder.Services.AddHttpClient() + .AddStandardResilienceHandler(static options => options.Retry = new MobileHttpRetryStrategyOptions()); + builder.Services.AddSingleton(); RegisterViewsAndViewModels(builder.Services); @@ -252,6 +255,7 @@ static void RegisterViewsAndViewModels(in IServiceCollection services) services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); + services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); services.AddTransientWithShellRoute(); diff --git a/samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementFromStreamPage.xaml b/samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementFromStreamPage.xaml new file mode 100644 index 0000000000..ac60242731 --- /dev/null +++ b/samples/CommunityToolkit.Maui.Sample/Pages/Views/MediaElement/MediaElementFromStreamPage.xaml @@ -0,0 +1,160 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +