0

我正在开发一个 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()) 我可以显示添加的弹出项列表,但我想以编程方式打开任何设置弹出项而不是打开弹出列表。

4

3 回答 3

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

于 2013-02-05T09:43:36.630 回答
0

您发布的代码将 a 注册SettingsCommands到系统设置窗格。从系统设置窗格调用命令时,您会新建一个SettingsFlyout实例并IsOpen=True对其进行设置。

您只需将此代码重构为一个单独的方法(例如ShowColorSettingsFlyout()),并从您的Button.Click事件处理程序中调用该方法。您可以创建一个新的 CallistoSettingsFlyoutIsOpen=True在任何地方进行设置。

于 2013-02-05T22:36:15.053 回答
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()
}

改编自:快速入门:添加应用设置 (XAML)

于 2014-10-24T13:52:46.547 回答