0

すべてのページで設定フライアウトに簡単にアクセスできるようにしようとしています。http://code.msdn.microsoft.com/windowsapps/App-settings-sample-1f762f49/sourcecode?fileId=50851&pathId=2033699455で設定の例を見ました。

ただし、約 12 個の関数があり、各ページで同じコードの不特定の数のコピーをコピー/貼り付け/維持する必要があるのは、ばかげています。そこで、ポップアップのコードを取得し、基本クラスから継承する基本クラスに配置しましたLayoutAwarePage。マイページは、この新しいクラスを継承します。元:

// Base class declaration that includes settings flyout code
public class SettingsFlyoutAwarePage : myApp.Common.LayoutAwarePage
{
    // Settings flyout code here
}

// Page declaration
public sealed partial class GroupedItemsPage : myApp.Common.SettingsFlyoutAwarePage
{
    // Standard working page stuff here
}

これはサンプル コードと同じですが、実際SettingsFlyoutAwarePageには何も画面に表示されません。

    void onSettingsCommand(IUICommand command)
    {
        // Create a Popup window which will contain our flyout.
        settingsPopup = new Popup();
        settingsPopup.Closed += OnPopupClosed;
        Window.Current.Activated += OnWindowActivated;
        settingsPopup.IsLightDismissEnabled = true;
        settingsPopup.Width = settingsWidth;
        settingsPopup.Height = windowBounds.Height;

        // Add the proper animation for the panel.
        settingsPopup.ChildTransitions = new TransitionCollection();
        settingsPopup.ChildTransitions.Add(new PaneThemeTransition()
        {
            Edge = (SettingsPane.Edge == SettingsEdgeLocation.Right) ?
                   EdgeTransitionLocation.Right :
                   EdgeTransitionLocation.Left
        });

        // Create a SettingsFlyout the same dimenssions as the Popup.
        SettingsFlyout mypane = new SettingsFlyout();
        mypane.Width = settingsWidth;
        mypane.Height = windowBounds.Height;

        // Place the SettingsFlyout inside our Popup window.
        settingsPopup.Child = mypane;

        // Let's define the location of our Popup.
        settingsPopup.SetValue(Canvas.LeftProperty, SettingsPane.Edge == SettingsEdgeLocation.Right ? (windowBounds.Width - settingsWidth) : 0);
        settingsPopup.SetValue(Canvas.TopProperty, 0);
        settingsPopup.IsOpen = true;
    }

すべてのコードをステップ実行でき、すべて正常に実行されているように見えますが、フライアウトが実際に表示されることはありません。少なくとも理論的には、継承された基本クラスからこれを行うことは可能ですか?

4

2 に答える 2

0

これが誰かに役立つ場合は、XAML ページを継承できる設定フライアウト対応クラスを次に示します。もちろん、ポップアップ自体を実装する必要があります(デモ コード リンク) 。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.UI.ApplicationSettings;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media.Animation;
using Windows.UI.Xaml.Navigation;

namespace MyApp.Common
{
    [Windows.Foundation.Metadata.WebHostHidden]
    public class MyAppBasePage : MyApp.Common.LayoutAwarePage
    {

        public MyAppBasePage()
        {
        }

        private bool isSettingCharmEventRegistered;

        // Used to determine the correct height to ensure our custom UI fills the screen.
        private Rect windowBounds;

        // Desired width for the settings UI. UI guidelines specify this should be 346 or 646 depending on your needs.
        private double settingsWidth = 646;

        // This is the container that will hold our custom content.
        private Popup settingsPopup;

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            base.OnNavigatedFrom(e);

            // Added to make sure the event handler for CommandsRequested is cleaned up before other scenarios.
            if (this.isSettingCharmEventRegistered)
            {
                SettingsPane.GetForCurrentView().CommandsRequested -= onCommandsRequested;
                this.isSettingCharmEventRegistered = false;
            }

            // Unregister the event that listens for events when the window size is updated.
            Window.Current.SizeChanged -= OnWindowSizeChanged;
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            windowBounds = Window.Current.Bounds;

            // Added to listen for events when the window size is updated.
            Window.Current.SizeChanged += OnWindowSizeChanged;

            // Added to make sure the event handler for CommandsRequested is cleaned up before other scenarios.
            if (!this.isSettingCharmEventRegistered)
            {
                SettingsPane.GetForCurrentView().CommandsRequested += onCommandsRequested;
                this.isSettingCharmEventRegistered = true;
            }
        }

        /// <summary>
        /// Invoked when the window size is updated.
        /// </summary>
        /// <param name="sender">Instance that triggered the event.</param>
        /// <param name="e">Event data describing the conditions that led to the event.</param>
        void OnWindowSizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            windowBounds = Window.Current.Bounds;
        }


        /// <summary>
        /// We use the window's activated event to force closing the Popup since a user maybe interacted with
        /// something that didn't normally trigger an obvious dismiss.
        /// </summary>
        /// <param name="sender">Instance that triggered the event.</param>
        /// <param name="e">Event data describing the conditions that led to the event.</param>
        private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
        {
            if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
            {
                settingsPopup.IsOpen = false;
            }
        }

        /// <summary>
        /// When the Popup closes we no longer need to monitor activation changes.
        /// </summary>
        /// <param name="sender">Instance that triggered the event.</param>
        /// <param name="e">Event data describing the conditions that led to the event.</param>
        void OnPopupClosed(object sender, object e)
        {
            Window.Current.Activated -= OnWindowActivated;
        }

        /// <summary>
        /// This event is generated when the user opens the settings pane. During this event, append your
        /// SettingsCommand objects to the available ApplicationCommands vector to make them available to the
        /// SettingsPange UI.
        /// </summary>
        /// <param name="settingsPane">Instance that triggered the event.</param>
        /// <param name="eventArgs">Event data describing the conditions that led to the event.</param>
        void onCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs eventArgs)
        {
            UICommandInvokedHandler handler = new UICommandInvokedHandler(onSettingsCommand);

            SettingsCommand generalCommand = new SettingsCommand("DefaultsId", "Defaults", handler);
            eventArgs.Request.ApplicationCommands.Add(generalCommand);
        }

        /// <summary>
        /// This the event handler for the "Defaults" button added to the settings charm. This method
        /// is responsible for creating the Popup window will use as the container for our settings Flyout.
        /// The reason we use a Popup is that it gives us the "light dismiss" behavior that when a user clicks away 
        /// from our custom UI it just dismisses.  This is a principle in the Settings experience and you see the
        /// same behavior in other experiences like AppBar. 
        /// </summary>
        /// <param name="command"></param>
        void onSettingsCommand(IUICommand command)
        {
            // Create a Popup window which will contain our flyout.
            settingsPopup = new Popup();
            settingsPopup.Closed += OnPopupClosed;
            Window.Current.Activated += OnWindowActivated;
            settingsPopup.IsLightDismissEnabled = true;
            settingsPopup.Width = settingsWidth;
            settingsPopup.Height = windowBounds.Height;

            // Add the proper animation for the panel.
            settingsPopup.ChildTransitions = new TransitionCollection();
            settingsPopup.ChildTransitions.Add(new PaneThemeTransition()
            {
                Edge = (SettingsPane.Edge == SettingsEdgeLocation.Right) ?
                       EdgeTransitionLocation.Right :
                       EdgeTransitionLocation.Left
            });

            // Create a SettingsFlyout the same dimenssions as the Popup.
            SettingsFlyout mypane = new SettingsFlyout();
            mypane.Width = settingsWidth;
            mypane.Height = windowBounds.Height;

            // Place the SettingsFlyout inside our Popup window.
            settingsPopup.Child = mypane;

            // Let's define the location of our Popup.
            settingsPopup.SetValue(Canvas.LeftProperty, SettingsPane.Edge == SettingsEdgeLocation.Right ? (windowBounds.Width - settingsWidth) : 0);
            settingsPopup.SetValue(Canvas.TopProperty, 0);
            settingsPopup.IsOpen = true;
        }
    }
}
于 2012-10-17T19:37:08.087 に答える
0

onCommandsRequestedデモで説明されているように機能を実装しましたか?

void onCommandsRequested(SettingsPane settingsPane, SettingsPaneCommandsRequestedEventArgs eventArgs)
        {
            UICommandInvokedHandler handler = new UICommandInvokedHandler(onSettingsCommand);

            SettingsCommand generalCommand = new SettingsCommand("DefaultsId", "Defaults", handler);
            eventArgs.Request.ApplicationCommands.Add(generalCommand);
        }
于 2012-10-17T16:40:49.737 に答える