【问题标题】:How to access child elements of HubView XAML in C#?如何在 C# 中访问 HubView XAML 的子元素?
【发布时间】:2015-03-17 12:34:45
【问题描述】:

我刚刚从 Windows Phone 8.1 Silverlight 迁移到 Windows Phone Store 应用程序。我的应用页面有以下 XAML:

<Page
    x:Class="WebClip.HubPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WebClip"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:data="using:WebClip.Data"
    mc:Ignorable="d">

    <Grid x:Name="LayoutRoot">
        <Hub x:Name="Hub"
             Header="web clip"
             Background="{ThemeResource HubBackgroundImageBrush}">

            <HubSection x:Name="TileSelectorView"
                        Header="TILE SELECTOR">
                <DataTemplate>
                    <ListView x:Name="TileList"/>
                </DataTemplate>
            </HubSection>

            <HubSection x:Name="BrowserView"
                        Header="BROWSER">
                <DataTemplate>
                    <WebView x:Name="BrowserBox"/>
                </DataTemplate>
            </HubSection>

        </Hub>
    </Grid>
</Page>

之前,在 Silverlight 中,我可以直接访问像 TileList 这样的元素来执行以下操作:

TileList.ItemsSource = <SomeItemSourceList>;

但现在我无法在后端 C# 代码中执行此操作。 TileList 本身是不可访问的。但是,可以访问 TileSelectorView 和 BrowserView(参考:上面的代码)。

我在 Jerry 回答过类似问题的地方发现了这两个问题:

  1. How to accessing Elements in XAML DataTemplate Listview without interacting with it
  2. How do I access a control inside a XAML DataTemplate?

但是,我无法复制它们。我的 TileSelectorView 下没有 Items 属性可以迭代。

我做错了什么?我该怎么办?

【问题讨论】:

标签: c# windows-phone windows-store-apps windows-phone-8.1 winrt-xaml


【解决方案1】:

从 Windows Phone Silverlight 迁移到 Windows Phone RT 时,我遇到了同样的问题。您可以按照其他人的建议通过数据绑定来解决此问题,但有时您只想获得页面上的控件。我发现下面的文章很有帮助..

Get the controls inside DataTemplate control

【讨论】:

    【解决方案2】:

    您可以像这样对它们进行数据绑定:

    我刚刚从 Windows Phone 8.1 Silverlight 迁移到 Windows Phone Store 应用程序。我的应用页面有以下 XAML:

    <HubSection x:Name="TileSelectorView"
                Header="TILE SELECTOR" 
                ItemsSource="{Binding SomeItemSourceList}">
      <DataTemplate>
        <ListView x:Name="TileList"/>
      </DataTemplate>
    </HubSection>
    

    这假设您使用的是 MVVM 模式,这是开发 Windows Phone 应用程序时的最佳实践

    https://msdn.microsoft.com/en-us/library/windows/apps/jj883732.aspx

    我个人更喜欢 MVVM Light 作为我的 MVVM 框架: http://blog.galasoft.ch/posts/2014/04/building-a-universal-application-for-windows-phone-8-1-and-windows-8-1-with-mvvm-light/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2018-01-22
      • 2022-11-20
      • 1970-01-01
      • 1970-01-01
      • 2015-08-21
      相关资源
      最近更新 更多