如何在WinRT应用程序中打开任何特定的SettingsFlyout

时间:2013-02-05 09:22:31

标签: windows-8 winrt-xaml

我正在开发一个Windows 8 metro应用程序,并有多个SettingsFlyout项目,这些项目将通过下面提到的代码添加

SettingsCommand cmd1 = new SettingsCommand("sample", "Color Settings", (x) =>
        {
            // create a new instance of the flyout
            SettingsFlyout settings = new SettingsFlyout();
            // set the desired width.  If you leave this out, you will get Narrow (346px)
            // optionally change header and content background colors away from defaults (recommended)
            // if using Callisto's AppManifestHelper you can grab the element from some member var you held it in
            // settings.HeaderBrush = new SolidColorBrush(App.VisualElements.BackgroundColor);
            settings.HeaderBrush = new SolidColorBrush(Colors.Black);
            settings.HeaderText = string.Format("Color Settings", App.VisualElements.DisplayName);
            settings.Background = new SolidColorBrush(_background);
            settings.Margin = new Thickness(0);
            // provide some logo (preferrably the smallogo the app uses)
            BitmapImage bmp = new BitmapImage(App.VisualElements.SmallLogoUri);
            settings.SmallLogoImageSource = bmp;

            // set the content for the flyout
            settings.Content = new ColorSettings();
            settings.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Stretch;

            // open it
            settings.IsOpen = true;

            // this is only for the test app and not needed
            // you would not use this code in your real app
            // ObjectTracker.Track(settings);
        });

目前使用(SettingsPane.Show())我可以显示添加的弹出项目列表,但我想以编程方式打开任何设置Flyout项目,而不是打开弹出列表。

3 个答案:

答案 0 :(得分:0)

创建一个新类

public class SettingsFlyout
{
    private const int _width = 346;
    private Popup _popup;

    /// <summary>
    /// Show the Flyout with the UserControl as content
    /// </summary>
    /// <param name="control"></param>
    public void ShowFlyout(UserControl control)
    {
        _popup = new Popup();
        _popup.Closed += OnPopupClosed;
        Window.Current.Activated += OnWindowActivated;
        _popup.IsLightDismissEnabled = true;
        _popup.Width = _width;
        _popup.Height = Window.Current.Bounds.Height;

        control.Width = _width;
        control.Height = Window.Current.Bounds.Height;

        _popup.Child = control;
        _popup.SetValue(Canvas.LeftProperty, Window.Current.Bounds.Width - _width);
        _popup.SetValue(Canvas.TopProperty, 0);
        _popup.IsOpen = true;
    }

    private void OnWindowActivated(object sender, Windows.UI.Core.WindowActivatedEventArgs e)
    {
        if (e.WindowActivationState == Windows.UI.Core.CoreWindowActivationState.Deactivated)
        {
            _popup.IsOpen = false;
        }
    }

    void OnPopupClosed(object sender, object e)
    {
        Window.Current.Activated -= OnWindowActivated;
    }
}

在XAML页面中,按一个按钮,然后编写按钮点击事件。

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    SettingsFlyout flyout = new SettingsFlyout();
    flyout.ShowFlyout(new FlyoutContentUserControl());
}

请注意,FlyoutContentUserControl是您要显示的用户控件。

积分转到Q42.WinRT

答案 1 :(得分:0)

您发布的代码将SettingsCommands注册到系统设置窗格。从系统设置窗格调用命令后,您需要新建SettingsFlyout实例并在其上设置IsOpen=True

您只需将此代码重构为单独的方法(例如ShowColorSettingsFlyout()),并从Button.Click事件处理程序中调用该方法。您可以在任意位置创建新的Callisto SettingsFlyout并在其上设置IsOpen=True

答案 2 :(得分:0)

在App.xaml.cs中添加以下内容以注册您的SettingsFlyout,例如CustomSetting ...

protected override void OnWindowCreated(WindowCreatedEventArgs args)
{
    SettingsPane.GetForCurrentView().CommandsRequested += OnCommandsRequested;
}

private void OnCommandsRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
    args.Request.ApplicationCommands.Add(new SettingsCommand(
            "Custom Setting", "Custom Setting", (handler) => ShowCustomSettingFlyout()));
}

public void ShowCustomSettingFlyout()
{
    CustomSetting CustomSettingFlyout = new CustomSetting();
    CustomSettingFlyout.Show();
}

然后,在代码中的任何位置,您希望以编程方式打开CustomSetting弹出窗口,调用ShowCustomSettingFlyout,例如在按钮的事件处理程序中单击...

void Button_Click_1(object sender, RoutedEventArgs e)
{
    ShowCustomSettingFlyout()
}

改编自:Quickstart: Add app settings (XAML)