Skip to content

Commit 8a5e4e6

Browse files
committed
Added code and tested for Android and Windows.
1 parent 4ebc253 commit 8a5e4e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1203
-31
lines changed

LukeMauiFilePicker.Demo/App.xaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version = "1.0" encoding = "UTF-8" ?>
2+
<Application xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:local="clr-namespace:LukeMauiFilePicker.Demo"
5+
x:Class="LukeMauiFilePicker.Demo.App">
6+
<Application.Resources>
7+
<ResourceDictionary>
8+
<ResourceDictionary.MergedDictionaries>
9+
<ResourceDictionary Source="Resources/Styles/Colors.xaml" />
10+
<ResourceDictionary Source="Resources/Styles/Styles.xaml" />
11+
</ResourceDictionary.MergedDictionaries>
12+
</ResourceDictionary>
13+
</Application.Resources>
14+
</Application>

LukeMauiFilePicker.Demo/App.xaml.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace LukeMauiFilePicker.Demo;
2+
3+
public partial class App : Application
4+
{
5+
public App()
6+
{
7+
InitializeComponent();
8+
9+
MainPage = new AppShell();
10+
}
11+
}

LukeMauiFilePicker.Demo/AppShell.xaml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<Shell
3+
x:Class="LukeMauiFilePicker.Demo.AppShell"
4+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
5+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
6+
xmlns:local="clr-namespace:LukeMauiFilePicker.Demo"
7+
Shell.FlyoutBehavior="Disabled">
8+
9+
<ShellContent
10+
Title="Home"
11+
ContentTemplate="{DataTemplate local:MainPage}"
12+
Route="MainPage" />
13+
14+
</Shell>
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace LukeMauiFilePicker.Demo;
2+
3+
public partial class AppShell : Shell
4+
{
5+
public AppShell()
6+
{
7+
InitializeComponent();
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
5+
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
6+
<OutputType>Exe</OutputType>
7+
<RootNamespace>LukeMauiFilePicker.Demo</RootNamespace>
8+
<UseMaui>true</UseMaui>
9+
<SingleProject>true</SingleProject>
10+
<ImplicitUsings>enable</ImplicitUsings>
11+
<Nullable>enable</Nullable>
12+
13+
<!-- Display name -->
14+
<ApplicationTitle>LukeMauiFilePicker.Demo</ApplicationTitle>
15+
16+
<!-- App Identifier -->
17+
<ApplicationId>com.lukevo.lukemauifilepicker</ApplicationId>
18+
<ApplicationIdGuid>1e8dbbec-f2da-4025-b895-1b019d4ace88</ApplicationIdGuid>
19+
20+
<!-- Versions -->
21+
<ApplicationDisplayVersion>1.0</ApplicationDisplayVersion>
22+
<ApplicationVersion>1</ApplicationVersion>
23+
24+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
25+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">13.1</SupportedOSPlatformVersion>
26+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
27+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</SupportedOSPlatformVersion>
28+
<TargetPlatformMinVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'windows'">10.0.17763.0</TargetPlatformMinVersion>
29+
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
30+
</PropertyGroup>
31+
32+
<ItemGroup>
33+
<!-- App Icon -->
34+
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
35+
36+
<!-- Splash Screen -->
37+
<MauiSplashScreen Include="Resources\Splash\splash.svg" Color="#512BD4" BaseSize="128,128" />
38+
39+
<!-- Images -->
40+
<MauiImage Include="Resources\Images\*" />
41+
<MauiImage Update="Resources\Images\dotnet_bot.svg" BaseSize="168,208" />
42+
43+
<!-- Custom Fonts -->
44+
<MauiFont Include="Resources\Fonts\*" />
45+
46+
<!-- Raw Assets (also remove the "Resources\Raw" prefix) -->
47+
<MauiAsset Include="Resources\Raw\**" LogicalName="%(RecursiveDir)%(Filename)%(Extension)" />
48+
</ItemGroup>
49+
50+
<ItemGroup>
51+
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
52+
</ItemGroup>
53+
54+
<ItemGroup>
55+
<ProjectReference Include="..\LukeMauiFilePicker\LukeMauiFilePicker.csproj" />
56+
</ItemGroup>
57+
58+
</Project>

LukeMauiFilePicker.Demo/MainPage.xaml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="LukeMauiFilePicker.Demo.MainPage">
5+
6+
<VerticalStackLayout>
7+
<Button Text="Pick a Single File" Clicked="PickOne" Margin="10" />
8+
<Button Text="Pick Multiple Files" Clicked="PickMany" Margin="10" />
9+
10+
<Editor x:Name="TextEditor" HeightRequest="200" Margin="10" />
11+
12+
<Button Text="Save" Clicked="Save" Margin="10" />
13+
</VerticalStackLayout>
14+
15+
</ContentPage>
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System.Text;
2+
3+
namespace LukeMauiFilePicker.Demo;
4+
5+
public partial class MainPage : ContentPage
6+
{
7+
static readonly Dictionary<DevicePlatform, IEnumerable<string>> FileType = new()
8+
{
9+
{ DevicePlatform.Android, new[] { "text/*" } } ,
10+
{ DevicePlatform.iOS, new[] { ".txt", ".json" } },
11+
{ DevicePlatform.WinUI, new[] { ".txt", ".json" } }
12+
};
13+
14+
readonly IFilePickerService picker;
15+
public MainPage(IFilePickerService picker)
16+
{
17+
this.picker = picker;
18+
19+
InitializeComponent();
20+
}
21+
22+
async Task OnFilesPickedAsync(IEnumerable<IPickFile> files)
23+
{
24+
var str = new StringBuilder();
25+
26+
foreach (var f in files)
27+
{
28+
using var s = await f.OpenReadAsync();
29+
using var reader = new StreamReader(s);
30+
31+
str.AppendLine(await reader.ReadToEndAsync());
32+
}
33+
34+
await MainThread.InvokeOnMainThreadAsync(() =>
35+
{
36+
TextEditor.Text = str.ToString();
37+
});
38+
}
39+
40+
private async void PickOne(object sender, EventArgs e)
41+
{
42+
var file = await picker.PickFileAsync("Select a file", FileType);
43+
if (file is null) { return; }
44+
45+
await OnFilesPickedAsync(new[] { file });
46+
}
47+
48+
private async void PickMany(object sender, EventArgs e)
49+
{
50+
var files = await picker.PickFilesAsync("Select a file", FileType, true);
51+
if (files is null || !files.Any()) { return; }
52+
53+
await OnFilesPickedAsync(files);
54+
}
55+
56+
private async void Save(object sender, EventArgs e)
57+
{
58+
var bytes = Encoding.UTF8.GetBytes(TextEditor.Text ?? "");
59+
using var memory = new MemoryStream(bytes);
60+
61+
await picker.SaveFileAsync(new("text.txt", memory)
62+
{
63+
AndroidMimeType = "text/plain",
64+
WindowsFileTypes = ("Text files", new() { ".txt", })
65+
});
66+
}
67+
68+
}
69+
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Extensions.Logging;
2+
using Microsoft.Maui.LifecycleEvents;
3+
4+
namespace LukeMauiFilePicker.Demo;
5+
public static class MauiProgram
6+
{
7+
public static MauiApp CreateMauiApp()
8+
{
9+
var builder = MauiApp.CreateBuilder();
10+
builder
11+
.UseMauiApp<App>()
12+
.ConfigureFonts(fonts =>
13+
{
14+
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
15+
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
16+
})
17+
.ConfigureFilePicker(100);
18+
19+
builder.Services
20+
.AddFilePicker()
21+
22+
.AddSingleton<MainPage>();
23+
24+
#if DEBUG
25+
builder.Logging.AddDebug();
26+
#endif
27+
28+
return builder.Build();
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
3+
<application android:allowBackup="true" android:icon="@mipmap/appicon" android:roundIcon="@mipmap/appicon_round" android:supportsRtl="true"></application>
4+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
5+
<uses-permission android:name="android.permission.INTERNET" />
6+
7+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Android.App;
2+
using Android.Content.PM;
3+
using Android.OS;
4+
5+
namespace LukeMauiFilePicker.Demo;
6+
[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
7+
public class MainActivity : MauiAppCompatActivity
8+
{
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using Android.App;
2+
using Android.Runtime;
3+
4+
namespace LukeMauiFilePicker.Demo;
5+
[Application]
6+
public class MainApplication : MauiApplication
7+
{
8+
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
9+
: base(handle, ownership)
10+
{
11+
}
12+
13+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<color name="colorPrimary">#512BD4</color>
4+
<color name="colorPrimaryDark">#2B0B98</color>
5+
<color name="colorAccent">#2B0B98</color>
6+
</resources>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Foundation;
2+
3+
namespace LukeMauiFilePicker.Demo;
4+
[Register("AppDelegate")]
5+
public class AppDelegate : MauiUIApplicationDelegate
6+
{
7+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>UIDeviceFamily</key>
6+
<array>
7+
<integer>1</integer>
8+
<integer>2</integer>
9+
</array>
10+
<key>UIRequiredDeviceCapabilities</key>
11+
<array>
12+
<string>arm64</string>
13+
</array>
14+
<key>UISupportedInterfaceOrientations</key>
15+
<array>
16+
<string>UIInterfaceOrientationPortrait</string>
17+
<string>UIInterfaceOrientationLandscapeLeft</string>
18+
<string>UIInterfaceOrientationLandscapeRight</string>
19+
</array>
20+
<key>UISupportedInterfaceOrientations~ipad</key>
21+
<array>
22+
<string>UIInterfaceOrientationPortrait</string>
23+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
24+
<string>UIInterfaceOrientationLandscapeLeft</string>
25+
<string>UIInterfaceOrientationLandscapeRight</string>
26+
</array>
27+
<key>XSAppIconAssets</key>
28+
<string>Assets.xcassets/appicon.appiconset</string>
29+
</dict>
30+
</plist>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using ObjCRuntime;
2+
using UIKit;
3+
4+
namespace LukeMauiFilePicker.Demo;
5+
public class Program
6+
{
7+
// This is the main entry point of the application.
8+
static void Main(string[] args)
9+
{
10+
// if you want to use a different Application Delegate class from "AppDelegate"
11+
// you can specify it here.
12+
UIApplication.Main(args, null, typeof(AppDelegate));
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Maui;
2+
using Microsoft.Maui.Hosting;
3+
using System;
4+
5+
namespace LukeMauiFilePicker.Demo;
6+
7+
internal class Program : MauiApplication
8+
{
9+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
10+
11+
static void Main(string[] args)
12+
{
13+
var app = new Program();
14+
app.Run(args);
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest package="maui-application-id-placeholder" version="0.0.0" api-version="7" xmlns="http://tizen.org/ns/packages">
3+
<profile name="common" />
4+
<ui-application appid="maui-application-id-placeholder" exec="LukeMauiFilePicker.Demo.dll" multiple="false" nodisplay="false" taskmanage="true" type="dotnet" launch_mode="single">
5+
<label>maui-application-title-placeholder</label>
6+
<icon>maui-appicon-placeholder</icon>
7+
<metadata key="http://tizen.org/metadata/prefer_dotnet_aot" value="true" />
8+
</ui-application>
9+
<shortcut-list />
10+
<privileges>
11+
<privilege>http://tizen.org/privilege/internet</privilege>
12+
</privileges>
13+
<dependencies />
14+
<provides-appdefined-privileges />
15+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<maui:MauiWinUIApplication
2+
x:Class="LukeMauiFilePicker.Demo.WinUI.App"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:maui="using:Microsoft.Maui"
6+
xmlns:local="using:LukeMauiFilePicker.Demo.WinUI">
7+
8+
</maui:MauiWinUIApplication>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Microsoft.UI.Xaml;
2+
3+
// To learn more about WinUI, the WinUI project structure,
4+
// and more about our project templates, see: http://aka.ms/winui-project-info.
5+
6+
namespace LukeMauiFilePicker.Demo.WinUI;
7+
/// <summary>
8+
/// Provides application-specific behavior to supplement the default Application class.
9+
/// </summary>
10+
public partial class App : MauiWinUIApplication
11+
{
12+
/// <summary>
13+
/// Initializes the singleton application object. This is the first line of authored code
14+
/// executed, and as such is the logical equivalent of main() or WinMain().
15+
/// </summary>
16+
public App()
17+
{
18+
this.InitializeComponent();
19+
}
20+
21+
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
22+
}
23+

0 commit comments

Comments
 (0)