質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

Q&A

1回答

2670閲覧

UWPの戻る処理をXamarinで実装・・・

Kenshiro_Fukuda

総合スコア21

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Xamarin

Xamarin(ザマリン)は、iPhoneなどのiOSやAndroidで動作し、C# 言語を用いてアプリを開発できるクロスプラットフォーム開発環境です。Xamarin Studioと C# 言語を用いて、 iOS と Android の両方の開発を行うことができます。

0グッド

0クリップ

投稿2016/09/19 05:46

XamarinのUWP利用において、戻る操作の実現を行いたいのですが、

using Windows.UI.Core;

を追加して、下記のように記述しました。

// Ensure the current window is active(戻る処理の実装) Window.Current.Activate(); rootFrame.Navigated += RootFrame_Navigated; } private void RootFrame_Navigated(object sender, NavigationEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = rootFrame.CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed; } }

UWP単体ではこの操作でエラーは出なかったと思うのですが、今回のプロジェクトはXamarinツールを利用しています。その中で、このコードを追加したら、5エラーほど出てしまい、OnSuspendingは存在しないだの、OnNavigationFailedも存在しない、名前を直接含むことはできないといったことが出てきました。
結局どこがどうでというのが・・・

namespace _20160917_Xamarin.UWP { /// <summary> /// Provides application-specific behavior to supplement the default Application class. /// </summary> sealed partial class App : Application { /// <summary> /// Initializes the singleton application object. This is the first line of authored code /// executed, and as such is the logical equivalent of main() or WinMain(). /// </summary> public App() { this.InitializeComponent(); this.Suspending += OnSuspending; } /// <summary> /// Invoked when the application is launched normally by the end user. Other entry points /// will be used such as when the application is launched to open a specific file. /// </summary> /// <param name="e">Details about the launch request and process.</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { #if DEBUG if (System.Diagnostics.Debugger.IsAttached) { this.DebugSettings.EnableFrameRateCounter = true; } #endif Frame rootFrame = Window.Current.Content as Frame; // Do not repeat app initialization when the Window already has content, // just ensure that the window is active if (rootFrame == null) { // Create a Frame to act as the navigation context and navigate to the first page rootFrame = new Frame(); rootFrame.NavigationFailed += OnNavigationFailed; Xamarin.Forms.Forms.Init(e); if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Place the frame in the current Window Window.Current.Content = rootFrame; } if (rootFrame.Content == null) { // When the navigation stack isn't restored navigate to the first page, // configuring the new page by passing required information as a navigation // parameter rootFrame.Navigate(typeof(MainPage), e.Arguments); } // Ensure the current window is active(戻る処理の実装) Window.Current.Activate(); rootFrame.Navigated += RootFrame_Navigated; } private void RootFrame_Navigated(object sender, NavigationEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = rootFrame.CanGoBack ? AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed; } } /// <summary> /// Invoked when Navigation to a certain page fails /// </summary> /// <param name="sender">The Frame which failed navigation</param> /// <param name="e">Details about the navigation failure</param> void OnNavigationFailed(object sender, NavigationFailedEventArgs e) { throw new Exception("Failed to load Page " + e.SourcePageType.FullName); } /// <summary> /// Invoked when application execution is being suspended. Application state is saved /// without knowing whether the application will be terminated or resumed with the contents /// of memory still intact. /// </summary> /// <param name="sender">The source of the suspend request.</param> /// <param name="e">Details about the suspend request.</param> private void OnSuspending(object sender, SuspendingEventArgs e) { var deferral = e.SuspendingOperation.GetDeferral(); //TODO: Save application state and stop any background activity deferral.Complete(); } } }

そして、型、名前の終わりが必要とか出てきました…

それか、思い切って共通部分に戻る処理書いちゃったほうがいいのでしょうか??

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

こんんちは。

思い切って共通部分に戻る処理書いちゃったほうがいいのでしょうか??

まず、Xamarin.Formsを使うということは、UWP のコードは一切書かない ことが基本になるということを理解してください。なので、思い切って共有部分に書くということではなく、原則 すべてを Xamarin.Formsの共通プロジェクトにXamarin.FormsのAPIのみを使用して記述していきます。その上で、各固有OSのセンサーやUIを個別に記述必要がある場合のみ、iOS/Android/UWPプロジェクトにiOS/Android/UWPのAPIを使用してコードを記述し、共通プロジェクトから呼び出す。というやり方を取ります。

または、Xamarinネイティブの開発手法でiOS/Android/UWPのUIをそれぞれ個別に記述して、共通化できるコードのみを共通プロジェクトに書いていくやり方でも良いです。

Xamarin.FormsとXamarinネイティブの違いは、先日のハンズオンの最初にお見せした資料各種資料をご覧ください。

Xamarin.FormsではNavigationクラスが用意されているので、ページ遷移はNavigation.PushAsyncで指定したページに、Navigation.PopAsyncで戻るが出来ます。

投稿2016/09/19 07:59

ytabuchi

総合スコア335

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問