0

根据我从弗里曼的书“Metro Revealed”中收集到的信息(顺便说一句,这是迄今为止最好的书,尽管它的重量不到 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不是一个类,它所在的类是公共的。我该怎么办?

4

3 回答 3

2

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

于 2012-11-03T04:52:13.850 回答
1

它做的有点不同

我没有创建一个UserControl,而是创建了一个BasicPage,然后从我的主页创建了该页面的一个实例(见下文var mypane = new CreateProvider {Height = Window.Current.Bounds.Height};),其中CreateProviderXAML我传递给我的Popup喜欢的页面_providerPopup.Child = mypane;。试试这种方式,我认为你UserControl也可以正常工作。只需将您的用户控件分配给Popuplike _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;
于 2012-11-03T07:22:16.127 回答
1

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

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

这将有效!

于 2012-11-04T15:18:32.293 回答