Skip to content
This repository was archived by the owner on Apr 29, 2024. It is now read-only.

Commit 8c689ba

Browse files
committed
final version
1 parent 5f31eff commit 8c689ba

File tree

10 files changed

+165
-57
lines changed

10 files changed

+165
-57
lines changed

GetStarted/Notes/MultiPage/Notes.Android/MainActivity.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Android.Views;
77
using Android.Widget;
88
using Android.OS;
9+
using Xamarin.Forms.DualScreen;
910

1011
namespace Notes.Droid
1112
{
@@ -19,6 +20,7 @@ protected override void OnCreate(Bundle savedInstanceState)
1920

2021
base.OnCreate(savedInstanceState);
2122
global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
23+
DualScreenService.Init(this);
2224
LoadApplication(new App());
2325
}
2426
}

GetStarted/Notes/MultiPage/Notes.Android/Notes.Android.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<PackageReference Include="Xamarin.Forms">
5252
<Version>4.6.0.726</Version>
5353
</PackageReference>
54+
<PackageReference Include="Xamarin.Forms.DualScreen">
55+
<Version>4.6.0.726</Version>
56+
</PackageReference>
5457
</ItemGroup>
5558
<ItemGroup>
5659
<Compile Include="ItemDragAndDropEffect.cs" />

GetStarted/Notes/MultiPage/Notes/App.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public App()
1212
{
1313
InitializeComponent();
1414
FolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
15-
MainPage = new NavigationPage(new NotesPage());
15+
MainPage = new NavigationPage(new MainPage());
1616
}
1717

1818
protected override void OnStart()
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
mc:Ignorable="d"
7+
xmlns:dualScreen="clr-namespace:Xamarin.Forms.DualScreen;assembly=Xamarin.Forms.DualScreen"
8+
xmlns:templates="clr-namespace:Notes"
9+
x:Class="Notes.MainPage"
10+
Title="Notes">
11+
<ContentPage.ToolbarItems>
12+
<ToolbarItem Text="+"
13+
Clicked="OnNoteAddedClicked" />
14+
</ContentPage.ToolbarItems>
15+
<ContentPage.Content>
16+
<dualScreen:TwoPaneView
17+
18+
x:Name="twoPaneView">
19+
<!--Pane 1-->
20+
<dualScreen:TwoPaneView.Pane1>
21+
<templates:NotesView />
22+
</dualScreen:TwoPaneView.Pane1>
23+
<!--Pane 2-->
24+
<dualScreen:TwoPaneView.Pane2>
25+
<templates:NoteEntryView />
26+
</dualScreen:TwoPaneView.Pane2>
27+
</dualScreen:TwoPaneView>
28+
29+
</ContentPage.Content>
30+
</ContentPage>
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
using Notes.Models;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
9+
using Xamarin.Forms;
10+
using Xamarin.Forms.Xaml;
11+
using Xamarin.Forms.DualScreen;
12+
13+
namespace Notes
14+
{
15+
[XamlCompilation(XamlCompilationOptions.Compile)]
16+
public partial class MainPage : ContentPage
17+
{
18+
public static MainPage Current { get; private set; }
19+
public MainPage()
20+
{
21+
InitializeComponent();
22+
twoPaneView.LayoutChanged += TwoPaneView_LayoutChanged;
23+
Current = this;
24+
}
25+
26+
27+
public bool DeviceIsSpanned => DualScreenInfo.Current.SpanMode != TwoPaneViewMode.SinglePane;
28+
29+
private void TwoPaneView_LayoutChanged(object sender, EventArgs e)
30+
{
31+
if (DeviceIsSpanned)
32+
{
33+
twoPaneView.WideModeConfiguration = TwoPaneViewWideModeConfiguration.LeftRight;
34+
twoPaneView.TallModeConfiguration = TwoPaneViewTallModeConfiguration.TopBottom;
35+
}
36+
else
37+
{ // single screen!
38+
twoPaneView.WideModeConfiguration = TwoPaneViewWideModeConfiguration.SinglePane;
39+
twoPaneView.TallModeConfiguration = TwoPaneViewTallModeConfiguration.SinglePane;
40+
}
41+
42+
}
43+
44+
void OnNoteAddedClicked(object sender, EventArgs e)
45+
{
46+
twoPaneView.Pane2.BindingContext = new Note();
47+
twoPaneView.PanePriority = TwoPaneViewPriority.Pane2;
48+
}
49+
protected override void OnAppearing()
50+
{
51+
base.OnAppearing();
52+
53+
RefreshData();
54+
}
55+
56+
public void RefreshData()
57+
{
58+
var notes = new List<Note>();
59+
60+
var files = Directory.EnumerateFiles(App.FolderPath, "*.notes.txt");
61+
foreach (var filename in files)
62+
{
63+
notes.Add(new Note
64+
{
65+
Filename = filename,
66+
Text = File.ReadAllText(filename),
67+
Date = File.GetCreationTime(filename)
68+
});
69+
}
70+
71+
BindingContext = notes
72+
.OrderBy(d => d.Date)
73+
.ToList();
74+
75+
twoPaneView.PanePriority = TwoPaneViewPriority.Pane1;
76+
}
77+
public void OnListViewItemSelected(Note note)
78+
{
79+
if (note != null)
80+
{
81+
twoPaneView.Pane2.BindingContext = note;
82+
twoPaneView.PanePriority = TwoPaneViewPriority.Pane2;
83+
}
84+
}
85+
protected override bool OnBackButtonPressed()
86+
{
87+
if (!DeviceIsSpanned)
88+
{ // single-screen
89+
if (twoPaneView.PanePriority == TwoPaneViewPriority.Pane2)
90+
{ //showing detail, back goes to master (list)
91+
twoPaneView.PanePriority = TwoPaneViewPriority.Pane1;
92+
return true;
93+
}
94+
}
95+
return base.OnBackButtonPressed();
96+
}
97+
98+
99+
}
100+
}

GetStarted/Notes/MultiPage/Notes/NoteEntryPage.xaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
2+
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
x:Class="Notes.NoteEntryPage"
5-
Title="Note Entry">
4+
x:Class="Notes.NoteEntryView"
5+
>
66
<StackLayout Margin="20">
77
<Editor Placeholder="Enter your note"
88
Text="{Binding Text}"
@@ -19,4 +19,4 @@
1919
Clicked="OnDeleteButtonClicked"/>
2020
</Grid>
2121
</StackLayout>
22-
</ContentPage>
22+
</ContentView>

GetStarted/Notes/MultiPage/Notes/NoteEntryPage.xaml.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
namespace Notes
77
{
8-
public partial class NoteEntryPage : ContentPage
8+
public partial class NoteEntryView : ContentView
99
{
10-
public NoteEntryPage()
10+
public NoteEntryView()
1111
{
1212
InitializeComponent();
1313
}
1414

15-
async void OnSaveButtonClicked(object sender, EventArgs e)
15+
void OnSaveButtonClicked(object sender, EventArgs e)
1616
{
1717
var note = (Note)BindingContext;
1818

@@ -21,26 +21,27 @@ async void OnSaveButtonClicked(object sender, EventArgs e)
2121
// Save
2222
var filename = Path.Combine(App.FolderPath, $"{Path.GetRandomFileName()}.notes.txt");
2323
File.WriteAllText(filename, note.Text);
24+
((Note)BindingContext).Filename = filename;
2425
}
2526
else
2627
{
2728
// Update
2829
File.WriteAllText(note.Filename, note.Text);
2930
}
3031

31-
await Navigation.PopAsync();
32+
MainPage.Current.RefreshData();
3233
}
3334

34-
async void OnDeleteButtonClicked(object sender, EventArgs e)
35+
void OnDeleteButtonClicked(object sender, EventArgs e)
3536
{
3637
var note = (Note)BindingContext;
3738

3839
if (File.Exists(note.Filename))
3940
{
4041
File.Delete(note.Filename);
4142
}
42-
43-
await Navigation.PopAsync();
43+
BindingContext = new Note();
44+
MainPage.Current.RefreshData();
4445
}
4546
}
4647
}

GetStarted/Notes/MultiPage/Notes/Notes.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111

1212
<ItemGroup>
1313
<PackageReference Include="Xamarin.Forms" Version="4.6.0.726" />
14+
<PackageReference Include="Xamarin.Forms.DualScreen" Version="4.6.0.726" />
1415
</ItemGroup>
1516
<ItemGroup>
1617
<Folder Include="Models\" />
1718
</ItemGroup>
19+
<ItemGroup>
20+
<EmbeddedResource Update="MainPage.xaml">
21+
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
22+
</EmbeddedResource>
23+
</ItemGroup>
1824

1925
</Project>
Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
2+
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
33
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
x:Class="Notes.NotesPage"
4+
x:Class="Notes.NotesView"
55
xmlns:effects="clr-namespace:Notes"
6-
Title="Notes">
7-
<ContentPage.ToolbarItems>
8-
<ToolbarItem Text="+"
9-
Clicked="OnNoteAddedClicked" />
10-
</ContentPage.ToolbarItems>
6+
>
7+
118
<ListView x:Name="listView"
129
Margin="20"
10+
ItemsSource="{Binding .}"
1311
ItemSelected="OnListViewItemSelected">
1412
<ListView.Effects>
1513
<effects:ItemDragAndDropEffect />
@@ -22,4 +20,4 @@
2220
</DataTemplate>
2321
</ListView.ItemTemplate>
2422
</ListView>
25-
</ContentPage>
23+
</ContentView>

GetStarted/Notes/MultiPage/Notes/NotesPage.xaml.cs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,20 @@
77

88
namespace Notes
99
{
10-
public partial class NotesPage : ContentPage
10+
public partial class NotesView : ContentView
1111
{
12-
public NotesPage()
12+
public NotesView()
1313
{
1414
InitializeComponent();
1515
}
1616

17-
protected override void OnAppearing()
18-
{
19-
base.OnAppearing();
20-
21-
var notes = new List<Note>();
22-
23-
var files = Directory.EnumerateFiles(App.FolderPath, "*.notes.txt");
24-
foreach (var filename in files)
25-
{
26-
notes.Add(new Note
27-
{
28-
Filename = filename,
29-
Text = File.ReadAllText(filename),
30-
Date = File.GetCreationTime(filename)
31-
});
32-
}
17+
3318

34-
listView.ItemsSource = notes
35-
.OrderBy(d => d.Date)
36-
.ToList();
37-
}
3819

39-
async void OnNoteAddedClicked(object sender, EventArgs e)
40-
{
41-
await Navigation.PushAsync(new NoteEntryPage
42-
{
43-
BindingContext = new Note()
44-
});
45-
}
4620

47-
async void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
21+
void OnListViewItemSelected(object sender, SelectedItemChangedEventArgs e)
4822
{
49-
if (e.SelectedItem != null)
50-
{
51-
await Navigation.PushAsync(new NoteEntryPage
52-
{
53-
BindingContext = e.SelectedItem as Note
54-
});
55-
}
23+
MainPage.Current.OnListViewItemSelected(e.SelectedItem as Note);
5624
}
5725
}
5826
}

0 commit comments

Comments
 (0)