Browse Source

Minimize private API usages (#14810)

* Minimize private API usages

- AvaloniaLocator.Current shouldn't be used unless really needed.
- Add comments where not possible otherwise

* fix wrong if statements
pull/14826/head
Tim 2 years ago
committed by GitHub
parent
commit
3472510677
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      samples/ControlCatalog.Desktop/Program.cs
  2. 4
      samples/ControlCatalog/Pages/DialogsPage.xaml.cs
  3. 3
      samples/ControlCatalog/Pages/TabControlPage.xaml.cs
  4. 3
      samples/ControlCatalog/ViewModels/CursorPageViewModel.cs
  5. 31
      samples/ControlCatalog/ViewModels/MainWindowViewModel.cs
  6. 8
      samples/ControlCatalog/ViewModels/PlatformInformationViewModel.cs
  7. 8
      samples/ControlCatalog/ViewModels/TransitioningContentControlPageViewModel.cs

4
samples/ControlCatalog.Desktop/Program.cs

@ -33,9 +33,7 @@ namespace ControlCatalog
private static void ConfigureAssetAssembly(AppBuilder builder)
{
AvaloniaLocator.CurrentMutable
.GetRequiredService<IAssetLoader>()
.SetDefaultAssembly(typeof(App).Assembly);
AssetLoader.SetDefaultAssembly(typeof(App).Assembly);
}
}
}

4
samples/ControlCatalog/Pages/DialogsPage.xaml.cs

@ -468,8 +468,8 @@ CanPickFolder: {storageProvider.CanPickFolder}";
private IStorageProvider GetStorageProvider()
{
var forceManaged = this.Get<CheckBox>("ForceManaged").IsChecked ?? false;
return forceManaged
? new ManagedStorageProvider(GetWindow())
return forceManaged
? new ManagedStorageProvider(GetWindow()) // NOTE: In your production App use 'AppBuilder.UseManagedSystemDialogs()'
: GetTopLevel().StorageProvider;
}

3
samples/ControlCatalog/Pages/TabControlPage.xaml.cs

@ -51,8 +51,7 @@ namespace ControlCatalog.Pages
private static Bitmap LoadBitmap(string uri)
{
var assets = AvaloniaLocator.Current.GetRequiredService<IAssetLoader>();
return new Bitmap(assets.Open(new Uri(uri)));
return new Bitmap(AssetLoader.Open(new Uri(uri)));
}
}
}

3
samples/ControlCatalog/ViewModels/CursorPageViewModel.cs

@ -18,8 +18,7 @@ namespace ControlCatalog.ViewModels
.Select(x => new StandardCursorModel(x))
.ToList();
var loader = AvaloniaLocator.Current.GetRequiredService<IAssetLoader>();
var s = loader.Open(new Uri("avares://ControlCatalog/Assets/avalonia-32.png"));
var s = AssetLoader.Open(new Uri("avares://ControlCatalog/Assets/avalonia-32.png"));
var bitmap = new Bitmap(s);
CustomCursor = new Cursor(bitmap, new PixelPoint(16, 16));
}

31
samples/ControlCatalog/ViewModels/MainWindowViewModel.cs

@ -3,7 +3,6 @@ using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Notifications;
using Avalonia.Dialogs;
using Avalonia.Platform;
using Avalonia.Reactive;
using System;
using System.ComponentModel.DataAnnotations;
using Avalonia;
@ -50,28 +49,28 @@ namespace ControlCatalog.ViewModels
WindowState.FullScreen,
};
this.WhenAnyValue(x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeEnabled)
.Subscribe(x =>
this.PropertyChanged += (s, e) =>
{
var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
if(x.Item1)
{
hints |= ExtendClientAreaChromeHints.SystemChrome;
}
if(x.Item2)
if (e.PropertyName is nameof(SystemTitleBarEnabled) or nameof(PreferSystemChromeEnabled))
{
hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
if (SystemTitleBarEnabled)
{
hints |= ExtendClientAreaChromeHints.SystemChrome;
}
if (PreferSystemChromeEnabled)
{
hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
}
ChromeHints = hints;
}
ChromeHints = hints;
});
};
SystemTitleBarEnabled = true;
TitleBarHeight = -1;
}
public ExtendClientAreaChromeHints ChromeHints
{
get { return _chromeHints; }

8
samples/ControlCatalog/ViewModels/PlatformInformationViewModel.cs

@ -9,6 +9,14 @@ public class PlatformInformationViewModel : ViewModelBase
{
public PlatformInformationViewModel()
{
/* NOTE:
* ------------
* The below API is not meant to be used in production Apps.
* If you need to consume this info, please use:
* - OperatingSystem ( https://learn.microsoft.com/en-us/dotnet/api/system.operatingsystem | if .NET 5 or greater)
* - or RuntimeInformation ( https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.runtimeinformation )
*/
var runtimeInfo = AvaloniaLocator.Current.GetService<IRuntimePlatform>()?.GetRuntimeInfo();
if (runtimeInfo is { } info)

8
samples/ControlCatalog/ViewModels/TransitioningContentControlPageViewModel.cs

@ -19,8 +19,6 @@ namespace ControlCatalog.ViewModels
{
public TransitioningContentControlPageViewModel()
{
var assetLoader = AvaloniaLocator.Current.GetRequiredService<IAssetLoader>();
var images = new string[]
{
"delicate-arch-896885_640.jpg",
@ -31,7 +29,7 @@ namespace ControlCatalog.ViewModels
foreach (var image in images)
{
var path = $"avares://ControlCatalog/Assets/{image}";
Images.Add(new Bitmap(assetLoader.Open(new Uri(path))));
Images.Add(new Bitmap(AssetLoader.Open(new Uri(path))));
}
SetupTransitions();
@ -250,7 +248,7 @@ namespace ControlCatalog.ViewModels
},
Duration = Duration
};
tasks.Add(animation.RunAsync(from, null, cancellationToken));
tasks.Add(animation.RunAsync(from, cancellationToken));
}
if (to != null)
@ -280,7 +278,7 @@ namespace ControlCatalog.ViewModels
},
Duration = Duration
};
tasks.Add(animation.RunAsync(to, null, cancellationToken));
tasks.Add(animation.RunAsync(to, cancellationToken));
}
await Task.WhenAll(tasks);

Loading…
Cancel
Save