我怎样才能参考我的弹出窗口?

时间:2012-11-02 23:09:17

标签: c# windows-8 settings winrt-xaml flyout

根据Freeman的书“Metro Revealed”(BTW,迄今为止最好的,尽管重量不到100页)我可以收集的内容,我正试图实现设置弹出窗口(弹出)。

我有这个相关的代码:

具有/是弹出窗口的用户控件:

<UserControl
    x:Class="TimeAndSpaceLines.View.TSLsSettings"
    . . .
    <Popup x:Name="popupSectionName" IsLightDismissEnabled="True" >
    . . .
    </Popup>
</UserControl>

“主要”页面的xaml:

<common:LayoutAwarePage
    x:Name="pageRoot"
    . . .
    xmlns:settingsflyout="using:TimeAndSpaceLines.View"
    . . .
        </Grid>
        <settingsflyout:TSLsSettings x:Name="hubPageSettingsFlyout"/>
        <VisualStateManager.VisualStateGroups>
    . . .

“主要”页面的相关“代码隐藏”:

public ItemsPage()
{
    InitializeComponent();
    SettingsPane.GetForCurrentView().CommandsRequested += OnSettingsPaneCommandRequested;
}

private void OnSettingsPaneCommandRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args)
{
    // Add the commands one by one to the settings panel
    args.Request.ApplicationCommands.Add(new SettingsCommand("commandSetSection1Name",
                                                            "Change the name of Section 1", SetAndSaveSectionNames));
. . .        
}

然而,当我尝试以这种方式打开它时:

private void SetAndSaveSectionNames(IUICommand command)
{
    TimeAndSpaceLines.View.TSLsSettings.popupSectionName.IsOpen = true;
    . . .
}

我受到了欢迎:

An object reference is required for the non-static field, method, or property
'TimeAndSpaceLines.View.TSLsSettings.popupSectionName'

'TimeAndSpaceLines.View.TSLsSettings.popupSectionName' is inaccessible due to its protection level

然而popupSectionName不是一个类,它所在的类是公共的。我该怎么办?

3 个答案:

答案 0 :(得分:2)

TimeAndSpaceLines.View.TSLsSettings是类名,而不是实例名。您想要的是实例的名称:hubPageSettingsFlyout。我建议在popupSectionName IsLightDismissEnabled上使用绑定,这样您就不必尝试查找UI元素,而只需检查它绑定的属性的值。

答案 1 :(得分:1)

它做得有点不同

而不是创建UserControl,我刚刚创建了BasicPage,然后从我的主页面创建了该页面的实例(请参阅下面的var mypane = new CreateProvider {Height = Window.Current.Bounds.Height};),其中CreateProvider是一个XAML页面,我将Popup传递给_providerPopup.Child = mypane;。试试这种方式,我认为你的UserControl也能正常工作。只需将您的用户控件分配到Popup _popup.Child = popupSectionName;

        // Create a Popup window which will contain our flyout.
        _providerPopup = new Popup
                             {
                                 Height = Window.Current.Bounds.Height,
                                 ChildTransitions = new TransitionCollection
                                                        {
                                                            new PaneThemeTransition
                                                                {
                                                                    Edge =
                                                                        (SettingsPane.Edge ==
                                                                         SettingsEdgeLocation.Right)
                                                                            ? EdgeTransitionLocation.Right
                                                                            : EdgeTransitionLocation.Left
                                                                }
                                                        }
                             };

        // Add the proper animation for the panel.

        // Create a SettingsFlyout the same dimenssions as the Popup.
        var mypane = new CreateProvider {Height = Window.Current.Bounds.Height};

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

        // Let's define the location of our Popup.
        _providerPopup.SetValue(Canvas.LeftProperty,
                                SettingsPane.Edge == SettingsEdgeLocation.Right
                                    ? (Window.Current.Bounds.Width - SettingsWidth)
                                    : 0);
        _providerPopup.SetValue(Canvas.TopProperty, 0);
        _providerPopup.IsOpen = true;

答案 2 :(得分:1)

从“主要”页面的相关“代码隐藏”中试试这个:

var myPopUp = (PopUp)hubPageSettingsFlyout.FindName("popupSectionName");

这会有效!