Native Forms: Enable app-level resource dictionary (#696)

* iOS/Droid projects now consume an app-level resource dictionary.

* UWP project uses app-level resource dictionary.

* Compile the resource dictionaries.

* Add comments.
This commit is contained in:
David Britch 2021-02-01 12:23:23 +00:00 коммит произвёл GitHub
Родитель cde78f9bd9
Коммит f8990767e5
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
15 изменённых файлов: 158 добавлений и 18 удалений

Просмотреть файл

@ -27,6 +27,11 @@ namespace Notes.Droid
base.OnCreate(bundle);
Forms.Init(this, bundle);
// Create app-level resource dictionary.
Xamarin.Forms.Application.Current = new Xamarin.Forms.Application();
Xamarin.Forms.Application.Current.Resources = new MyDictionary();
Instance = this;
SetContentView(Resource.Layout.Main);
@ -35,10 +40,17 @@ namespace Notes.Droid
SupportActionBar.Title = "Notes";
FolderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData));
AndroidX.Fragment.App.Fragment mainPage = new NotesPage().CreateSupportFragment(this);
NotesPage notesPage = new NotesPage()
{
// Set the parent so that the app-level resource dictionary can be located.
Parent = Xamarin.Forms.Application.Current
};
AndroidX.Fragment.App.Fragment notesPageFragment = notesPage.CreateSupportFragment(this);
SupportFragmentManager
.BeginTransaction()
.Replace(Resource.Id.fragment_frame_layout, mainPage)
.Replace(Resource.Id.fragment_frame_layout, notesPageFragment)
.Commit();
SupportFragmentManager.BackStackChanged += (sender, e) =>
@ -48,6 +60,8 @@ namespace Notes.Droid
SupportActionBar.SetDisplayHomeAsUpEnabled(hasBack);
SupportActionBar.Title = hasBack ? "Note Entry" : "Notes";
};
notesPage.Parent = null;
}
public override bool OnOptionsItemSelected(Android.Views.IMenuItem item)
@ -62,15 +76,21 @@ namespace Notes.Droid
public void NavigateToNoteEntryPage(Note note)
{
AndroidX.Fragment.App.Fragment noteEntryPage = new NoteEntryPage
NoteEntryPage noteEntryPage = new NoteEntryPage
{
BindingContext = note
}.CreateSupportFragment(this);
BindingContext = note,
// Set the parent so that the app-level resource dictionary can be located.
Parent = Xamarin.Forms.Application.Current
};
AndroidX.Fragment.App.Fragment noteEntryFragment = noteEntryPage.CreateSupportFragment(this);
SupportFragmentManager
.BeginTransaction()
.AddToBackStack(null)
.Replace(Resource.Id.fragment_frame_layout, noteEntryPage)
.Replace(Resource.Id.fragment_frame_layout, noteEntryFragment)
.Commit();
noteEntryPage.Parent = null;
}
public void NavigateBack()

Просмотреть файл

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Notes.Droid.MyDictionary">
<Style TargetType="Button">
<Setter Property="BackgroundColor"
Value="#2196F3"/>
</Style>
</ResourceDictionary>

Просмотреть файл

@ -0,0 +1,14 @@
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Notes.Droid
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyDictionary : ResourceDictionary
{
public MyDictionary()
{
InitializeComponent();
}
}
}

7
Native2Forms/Notes.Droid/Notes.Droid.csproj Executable file → Normal file
Просмотреть файл

@ -70,6 +70,9 @@
<Compile Include="Views\NoteEntryPage.xaml.cs">
<DependentUpon>NoteEntryPage.xaml</DependentUpon>
</Compile>
<Compile Include="MyDictionary.xaml.cs">
<DependentUpon>MyDictionary.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
@ -102,6 +105,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="MyDictionary.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

4
Native2Forms/Notes.UWP/App.xaml.cs Executable file → Normal file
Просмотреть файл

@ -43,6 +43,10 @@ namespace Notes.UWP
Xamarin.Forms.Forms.Init(e);
// Create app-level resource dictionary.
Xamarin.Forms.Application.Current = new Xamarin.Forms.Application();
Xamarin.Forms.Application.Current.Resources = new MyDictionary();
if (e.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
//TODO: Load state from previously suspended application

14
Native2Forms/Notes.UWP/MainPage.xaml.cs Executable file → Normal file
Просмотреть файл

@ -20,16 +20,21 @@ namespace Notes.UWP
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Enabled;
Instance = this;
FolderPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData));
notesPage = new Notes.UWP.Views.NotesPage();
notesPage = new Notes.UWP.Views.NotesPage
{
// Set the parent so that the app-level resource dictionary can be located.
Parent = Xamarin.Forms.Application.Current
};
this.Content = notesPage.CreateFrameworkElement();
this.Loaded += OnMainPageLoaded;
SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
notesPage.Parent = null;
}
void OnBackRequested(object sender, BackRequestedEventArgs e)
@ -58,9 +63,12 @@ namespace Notes.UWP
{
noteEntryPage = new Notes.UWP.Views.NoteEntryPage
{
BindingContext = note
BindingContext = note,
// Set the parent so that the app-level resource dictionary can be located.
Parent = Xamarin.Forms.Application.Current
};
this.Frame.Navigate(noteEntryPage);
noteEntryPage.Parent = null;
}
public void NavigateBack()

Просмотреть файл

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Notes.MyDictionary">
<Style TargetType="Button">
<Setter Property="BackgroundColor"
Value="#2196F3"/>
</Style>
</ResourceDictionary>

Просмотреть файл

@ -0,0 +1,14 @@
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Notes
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyDictionary : ResourceDictionary
{
public MyDictionary()
{
InitializeComponent();
}
}
}

Просмотреть файл

@ -123,6 +123,9 @@
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Models\Note.cs" />
<Compile Include="MyDictionary.xaml.cs">
<DependentUpon>MyDictionary.xaml</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Views\NoteEntryPage.xaml.cs">
<DependentUpon>NoteEntryPage.xaml</DependentUpon>
@ -164,6 +167,10 @@
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
<EmbeddedResource Include="MyDictionary.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">

Просмотреть файл

@ -23,6 +23,10 @@ namespace Notes.iOS
{
Forms.Init();
// Create app-level resource dictionary.
Xamarin.Forms.Application.Current = new Xamarin.Forms.Application();
Xamarin.Forms.Application.Current.Resources = new MyDictionary();
Instance = this;
_window = new UIWindow(UIScreen.MainScreen.Bounds);
@ -32,25 +36,39 @@ namespace Notes.iOS
});
FolderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
UIViewController mainPage = new NotesPage().CreateViewController();
mainPage.Title = "Notes";
_navigation = new AppNavigationController(mainPage);
NotesPage notesPage = new NotesPage()
{
// Set the parent so that the app-level resource dictionary can be located.
Parent = Xamarin.Forms.Application.Current
};
UIViewController notesPageController = notesPage.CreateViewController();
notesPageController.Title = "Notes";
_navigation = new AppNavigationController(notesPageController);
_window.RootViewController = _navigation;
_window.MakeKeyAndVisible();
notesPage.Parent = null;
return true;
}
public void NavigateToNoteEntryPage(Note note)
{
var noteEntryPage = new NoteEntryPage
NoteEntryPage noteEntryPage = new NoteEntryPage
{
BindingContext = note
}.CreateViewController();
noteEntryPage.Title = "Note Entry";
_navigation.PushViewController(noteEntryPage, true);
BindingContext = note,
// Set the parent so that the app-level resource dictionary can be located.
Parent = Xamarin.Forms.Application.Current
};
var noteEntryViewController = noteEntryPage.CreateViewController();
noteEntryViewController.Title = "Note Entry";
_navigation.PushViewController(noteEntryViewController, true);
noteEntryPage.Parent = null;
}
public void NavigateBack()

Просмотреть файл

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Notes.MyDictionary">
<Style TargetType="Button">
<Setter Property="BackgroundColor"
Value="LightGray"/>
</Style>
</ResourceDictionary>

Просмотреть файл

@ -0,0 +1,14 @@
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
namespace Notes
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MyDictionary : ResourceDictionary
{
public MyDictionary()
{
InitializeComponent();
}
}
}

Просмотреть файл

@ -136,6 +136,9 @@
<DependentUpon>NoteEntryPage.xaml</DependentUpon>
</Compile>
<Compile Include="Models\Note.cs" />
<Compile Include="MyDictionary.xaml.cs">
<DependentUpon>MyDictionary.xaml</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Views\NotesPage.xaml">
@ -146,6 +149,10 @@
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="MyDictionary.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.CSharp.targets" />
</Project>

2
Native2Forms/Notes.iOS/Views/NoteEntryPage.xaml Executable file → Normal file
Просмотреть файл

@ -11,7 +11,7 @@
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Text="Save"
<Button Text="Save"
Clicked="OnSaveButtonClicked" />
<Button Grid.Column="1"
Text="Delete"

0
Native2Forms/Notes.iOS/Views/NotesPage.xaml Executable file → Normal file
Просмотреть файл