Skip to content

Commit

Permalink
Add support for avares and resm #1
Browse files Browse the repository at this point in the history
  • Loading branch information
SKProCH committed Jun 18, 2022
1 parent 64bc399 commit cde9b1b
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 13 deletions.
1 change: 1 addition & 0 deletions AsyncImageLoader.Avalonia.Demo/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public override void OnFrameworkInitializationCompleted() {
desktop.MainWindow = new MainWindow {
DataContext = new MainWindowViewModel(),
};
desktop.MainWindow.AttachDevTools();
}

base.OnFrameworkInitializationCompleted();
Expand Down
Binary file added AsyncImageLoader.Avalonia.Demo/Assets/cat4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AsyncImageLoader.Avalonia.Demo/Assets/cat5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AsyncImageLoader.Avalonia.Demo/Assets/cat6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added AsyncImageLoader.Avalonia.Demo/Assets/cat7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
<AvaloniaResource Include="Assets/avalonia-logo.ico" />
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.10" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.10" />
<PackageReference Include="Avalonia" Version="0.10.15" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.15" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.10" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.10" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.15" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.15" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AsyncImageLoader.Avalonia\AsyncImageLoader.Avalonia.csproj" />
</ItemGroup>
<ItemGroup>
<AvaloniaResource Include="Assets/cat4.jpg" />
<AvaloniaResource Include="Assets/cat5.jpg" />
<EmbeddedResource Include="Assets/cat6.jpg" />
<EmbeddedResource Include="Assets/cat7.jpg" />
</ItemGroup>
</Project>
38 changes: 31 additions & 7 deletions AsyncImageLoader.Avalonia.Demo/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,38 @@
x:Class="AsyncImageLoader.Avalonia.Demo.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="AsyncImageLoader.Avalonia.Demo">

<Window.Styles>
<Style Selector="Image">
<Setter Property="Width" Value="400"></Setter>
</Style>
<Style Selector="StackPanel">
<Setter Property="HorizontalAlignment" Value="Center"></Setter>
</Style>
</Window.Styles>
<Design.DataContext>
<vm:MainWindowViewModel/>
</Design.DataContext>
<UniformGrid>
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat0.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat1.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat2.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat3.jpg" />
</UniformGrid>
<ScrollViewer>
<StackPanel Orientation="Vertical">
<TextBlock TextAlignment="Center">Images from Internet</TextBlock>
<StackPanel Orientation="Horizontal">
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat0.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat1.jpg" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat2.jpg" />
<Image asyncImageLoader:ImageLoader.Source="https://github.com/AvaloniaUtils/AsyncImageLoader.Avalonia/raw/master/AsyncImageLoader.Avalonia.Demo/Assets/cat3.jpg" />
</StackPanel>
<TextBlock TextAlignment="Center">Images from Assets (AvaloniaResource)</TextBlock>
<StackPanel Orientation="Horizontal">
<Image asyncImageLoader:ImageLoader.Source="avares://AsyncImageLoader.Avalonia.Demo/Assets/cat4.jpg" />
<Image asyncImageLoader:ImageLoader.Source="avares://AsyncImageLoader.Avalonia.Demo/Assets/cat5.jpg" />
</StackPanel>
<TextBlock TextAlignment="Center">Images from manifest resources (EmbeddedResource)</TextBlock>
<StackPanel Orientation="Horizontal">
<Image asyncImageLoader:ImageLoader.Source="resm:AsyncImageLoader.Avalonia.Demo.Assets.cat6.jpg" />
<Image asyncImageLoader:ImageLoader.Source="resm:AsyncImageLoader.Avalonia.Demo.Assets.cat7.jpg" />
</StackPanel>
</StackPanel>
</ScrollViewer>
</Window>
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Media.Imaging;
using Avalonia.Platform;

namespace AsyncImageLoader.Loaders {
/// <summary>
Expand Down Expand Up @@ -65,7 +67,15 @@ public BaseWebImageLoader(HttpClient httpClient, bool disposeHttpClient) {
/// <returns>Bitmap</returns>
protected virtual Task<Bitmap?> LoadFromInternalAsync(string url) {
try {
return Task.FromResult(new Bitmap(url))!;
var uri = url.StartsWith("/")
? new Uri(url, UriKind.Relative)
: new Uri(url, UriKind.RelativeOrAbsolute);

if(uri.IsAbsoluteUri && uri.IsFile)
return Task.FromResult(new Bitmap(uri.LocalPath))!;

var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
return Task.FromResult(new Bitmap(assets.Open(uri)))!;
}
catch (Exception) {
return Task.FromResult<Bitmap?>(null);
Expand Down

0 comments on commit cde9b1b

Please sign in to comment.