【问题标题】:Handle tap event for ListBoxItem template from code in Windows phone从 Windows phone 中的代码处理 ListBoxItem 模板的点击事件
【发布时间】:2014-02-25 00:25:44
【问题描述】:

我在 Windows Phone 中使用全景控制。在全景控件中,我从代码中动态添加全景项目。在 PanoramaItem 中,我设置了 ListBox。我正在从代码中为 ListBox 设置 ItemTemplate。这是我设置 ItemTemplate 的代码。

private void AddContentInPanoramaItem(PanoramaItem panoramaItem)
{           
    ListBox listBox = new ListBox();
    DataTemplate itemTmp = (DataTemplate)XamlReader.Load(
                                                    @"<DataTemplate xmlns=""http://schemas.microsoft.com/client/2007"">
                                                    <Grid Background=""Green"" Width=""400"" Margin=""0 10 0 10"">
                                                        <Grid.RowDefinitions>
                                                            <RowDefinition/>
                                                            <RowDefinition/>
                                                        </Grid.RowDefinitions>
                                                        <Grid.ColumnDefinitions>
                                                            <ColumnDefinition Width=""Auto""/>
                                                            <ColumnDefinition/>
                                                        </Grid.ColumnDefinitions>
                                                        <Border Background=""White"" Margin=""5 0 5 0"" Width=""60"" Height=""100"" Grid.RowSpan=""2"" CornerRadius=""1"">            
                                                            <Image Source=""Assets\phoneImage.png"" />
                                                         </Border>
                                                        <TextBlock Text=""{Binding Name}"" FontSize=""35"" Grid.Column=""1"" />
                                                        <TextBlock Text=""{Binding Number}"" FontSize=""20"" Grid.Column=""1"" Grid.Row=""2"" />
                                                    </Grid>
                                                </DataTemplate>");
    listBox.ItemTemplate = itemTmp;
    var contacts = from contact in m_contactList
                    where contact.Category == panoramaItem.Header.ToString()
                    orderby contact.Name
                    select contact;
    listBox.ItemsSource = contacts;
    panoramaItem.Content = listBox;             
}

现在我想为 .我定义了一个方法来处理点击事件。

private void Border_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{}

但是当我在标签中添加 Tap=""Border_Tap"" 时,我的应用程序崩溃了。异常说“错误 HRESULT E_FAIL 已从对 COM 组件的调用中返回。”

知道如何解决这个问题吗?

【问题讨论】:

    标签: c# xaml listbox windows-phone listboxitem


    【解决方案1】:

    简短的回答你不能这样做:

    XAML for Load 不应尝试为事件处理程序指定 x:Class,或包含任何 XAML 定义的属性。加载逻辑无法在运行时将加载的 XAML 与代码隐藏类集成。如果要添加事件处理程序,则必须在代码中通过引用从加载结果的对象树结构中获得的对象,并使用特定于语言的语法来附加处理程序(例如 +=)。有关使用代码附加事件的更多信息,请参阅 Silverlight 的事件概述。 [Source]

    到目前为止,我还没有看到任何针对此限制的简单解决方法。如果可能,只需在 XAML 中定义 &lt;DataTemplate&gt;,例如将其放在页面的资源中,然后您就可以从代码中使用它:

    <phone:PhoneApplicationPage.Resources>
        <DataTemplate xmlns="http://schemas.microsoft.com/client/2007" x:Key="listBoxItemTemplate">
            <Grid Background="Green" Width="400" Margin="0 10 0 10">
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>
                <Border Tap="Border_Tap" Background="White" Margin="5 0 5 0" Width="60" Height="100" Grid.RowSpan="2" CornerRadius="1">
                    <Image Source="Assets\phoneImage.png" />
                </Border>
                <TextBlock Text="{Binding Name}" FontSize="35" Grid.Column="1" />
                <TextBlock Text="{Binding Number}" FontSize="20" Grid.Column="1" Grid.Row="2" />
            </Grid>
        </DataTemplate>
    </phone:PhoneApplicationPage.Resources>
    //in C# code
    ........
    listBox.ItemTemplate = (DataTemplate)this.Resources["listBoxItemTemplate"];
    ........
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-09-24
      • 1970-01-01
      • 2023-04-07
      相关资源
      最近更新 更多