【问题标题】:How can I reference my popup?如何引用我的弹出窗口?
【发布时间】:2012-10-23 15:29:24
【问题描述】:

根据我从 Freeman 的书“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 不是一个类,它所在的类是公共的。我该怎么办?

【问题讨论】:

  • 在 SetAndSaveSectionNames 中调用不应该是 hubPageSettingsFlyout.popupSectionName.IsOpen = true 吗?
  • 当我替换这个时:TimeAndSpaceLines.View.TSLsSettings.popupSectionName.IsOpen = true;用这个: hubPageSettingsFlyout.popupSectionName.IsOpen = true; ...我明白了,“'TimeAndSpaceLines.View.TSLsSettings.popupSectionName' 由于其保护级别而无法访问” xaml 中 hubPageSettingsFlyout 的声明是: 所以我还在使困惑。我可能全都错了;我将不得不退后一步重新开始。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    这行得通!

    【讨论】:

      【解决方案3】:

      它做得有点不同

      我没有创建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;
      

      【讨论】:

      • 我相信我正在这样做: private void OnSettingsPaneCommandRequested(SettingsPane sender, SettingsPaneCommandsRequestedEventArgs args) { SettingsCommand cmd = new SettingsCommand("sample", "Sound Options", (x) => { _settingsPopup = new Popup(); . . . var settingsPane = new TSLsSettings(); . . . _settingsPopup.Child = settingsPane; _settingsPopup.IsOpen = true; }); args.Request.ApplicationCommands.Add(cmd); }
      猜你喜欢
      • 1970-01-01
      • 2021-03-07
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-10
      相关资源
      最近更新 更多