Windows phone 7 在用户控件类中,不会触发按钮和列表框事件等WP7 UI控件

Windows phone 7 在用户控件类中,不会触发按钮和列表框事件等WP7 UI控件,windows-phone-7,events,user-controls,uicontrol,Windows Phone 7,Events,User Controls,Uicontrol,我有一个用户控件,在该用户控件中有诸如按钮和列表框之类的ui控件,我从xaml中附加了这些控件的单击和选择更改事件,并尝试以编程方式附加它们,但没有触发这些事件 任何形式的帮助都将不胜感激, 非常感谢 下面是代码.cs和xaml <UserControl x:Class="VisioLink.Views.MagazineListPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentatio

我有一个用户控件,在该用户控件中有诸如按钮和列表框之类的ui控件,我从xaml中附加了这些控件的单击和选择更改事件,并尝试以编程方式附加它们,但没有触发这些事件

任何形式的帮助都将不胜感激, 非常感谢

下面是代码.cs和xaml

            <UserControl 
x:Class="VisioLink.Views.MagazineListPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="clr-namespace:VisioLink.Views"
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"    
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True"
Loaded="PhoneApplicationPage_Loaded"
 xmlns:local="clr-namespace:VisioLink.Views">
<UserControl.Resources>
    <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    <Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="HorizontalContentAlignment" Value="Left"/>
        <Setter Property="VerticalContentAlignment" Value="Top"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="LayoutRoot" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="LayoutRoot">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource TransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <DoubleAnimation Duration="0" To=".5" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ContentContainer"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected"/>
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames
                                Storyboard.TargetName="ContentContainer"
                                Storyboard.TargetProperty="Foreground"
                                Duration="0">
                                            <DiscreteObjectKeyFrame  KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="White"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames
                                Storyboard.TargetName="border"
                                Storyboard.TargetProperty="Background"
                                Duration="0">
                                            <DiscreteObjectKeyFrame  KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="LightBlue"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <StackPanel x:Name="border" Orientation="Horizontal">
                            <!--<CheckBox x:Name="checkBox" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, 
Path=IsSelected, Mode=TwoWay}" DataContext="{TemplateBinding IsSelected}" Visibility="{Binding Converter={StaticResource BooleanToVisibilityConverter}}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"/>-->
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </StackPanel>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="White">
    <toolkit:GestureService.GestureListener>
        <toolkit:GestureListener  Flick="GestureListener_Flick"   />
    </toolkit:GestureService.GestureListener>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition x:Name="firstCol" Width="40"></ColumnDefinition>
        <ColumnDefinition x:Name="secondCol" Width="10"></ColumnDefinition>
        <ColumnDefinition  Width="*"/>
    </Grid.ColumnDefinitions>
    <views:LeftMenuControl Visibility="Collapsed" x:Name="leftMenuControl" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Grid.Row="1" ></views:LeftMenuControl>
    <Button x:Name="flipBtn" Click="flip_click"  Grid.Column="0" Height="70" Grid.Row="1" VerticalAlignment="Center">

        <Button.Background>
            <ImageBrush ImageSource="file:///C:/VisioLink/Resources/Images/next.png" />
        </Button.Background>
    </Button>
    <Rectangle Fill="Black" Width="1" Grid.Column="1" Grid.Row="1" VerticalAlignment="Stretch" />

    <!--TitlePanel contains the name of the application and page title-->
    <StackPanel x:Name="TitlePanel" Grid.Row="0" Grid.Column="2" Margin="0,0,0,0">
        <TextBlock x:Name="ApplicationTitle" Foreground="Black" Text="Nyhet" Style="{StaticResource PhoneTextNormalStyle}"/>

    </StackPanel>

    <!--ContentPanel - place additional content here-->
    <StackPanel x:Name="ContentPanel" Grid.Row="1"  Grid.Column="2" Margin="5,12,12,0">
        <ListBox x:FieldModifier="public" x:Name="databoundListBoxMag"   SelectionChanged="listBox1_SelectionChanged" ItemContainerStyle="{StaticResource ListBoxItemStyle1}" SelectionMode="Single">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border Margin="7"
                        BorderThickness="1"
                        BorderBrush="SteelBlue"
                        CornerRadius="3" x:Name="border1">
                        <StackPanel Width="450" x:Name="listItemPanel" Orientation="Vertical" Height="100" Margin="10,10,10,10">
                            <StackPanel x:Name="firstStack" Orientation="Horizontal">
                            <Image x:Name="magImage" Height="40" Width="40" Visibility="Collapsed" Source="Example4.PNG"></Image>
                            <TextBlock x:Name="itemone" FontWeight="Bold" TextAlignment="Left" Foreground="Black" Margin="0,0,0,0" HorizontalAlignment="Left"  FontSize="22" Text="{Binding name}" Style="{StaticResource PhoneTextNormalStyle}"/>
                        </StackPanel>
                        <TextBlock x:Name="itemtwo" TextAlignment="Left" Foreground="Black" HorizontalAlignment="Left" Margin="0,0,0,0"  FontSize="16" Text="{Binding content}" Style="{StaticResource PhoneTextNormalStyle}"/>
                            <!--<phone:WebBrowser Name="webBrowserContent"   ScriptNotify="wb1_ScriptNotify"  IsScriptEnabled="True"  Source="http://www.bing.com"  >

                            </phone:WebBrowser>-->
                        </StackPanel>

                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <!--<ListBoxItem  Style="{StaticResource ListBoxItemStyle1}">
                <StackPanel x:Name="itemPanel" Grid.Row="0" Margin="0,0,0,28">
                    <TextBlock Height="72" HorizontalAlignment="Left"  Name="nameBlock" Text="{Binding name}" VerticalAlignment="Top" Width="379" />
                    <TextBlock Height="72" HorizontalAlignment="Left"  Name="detailBlock" Text="{Binding detail}" VerticalAlignment="Top" Width="379" />
                </StackPanel>
            </ListBoxItem>-->

        </ListBox>

    </StackPanel>
</Grid>
<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
    </shell:ApplicationBar>
</phone:PhoneApplicationPage.ApplicationBar>-->

这是它的.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    using Microsoft.Phone.Controls;
    using System.Windows.Data;
    using System.Globalization;
    using System.Xml;
    using System.Xml.Linq;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Windows.Resources;
    using System.Text.RegularExpressions;
    using VisioLink.DTO;

   namespace VisioLink.Views
  {
public partial class MagazineListPage : UserControl
{
    public MagazineListPage()
    {
       InitializeComponent();
    }
    List<Article> articles = new List<Article>();

    public int CatalogId { get; set; }
    public string ContentToSee = string.Empty;
    private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
    {
        databoundListBoxMag.SelectionChanged += new SelectionChangedEventHandler(listBox1_SelectionChanged);

        // List<Magazine> listMag = new List<Magazine>();
        // listMag.Clear();
        if(IsolatedStorageSettings.ApplicationSettings.Contains("magazineListPage"))
        {
            IsolatedStorageSettings.ApplicationSettings.TryGetValue("magazineListPage", out ContentToSee);
            DoProcessingOnData();
        }
        else
        {
            WebClient client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            client.DownloadStringAsync(new Uri("http://device01.e-pages.dk/content/default4.php?customer=kaleva&catalog=" + CatalogId, UriKind.RelativeOrAbsolute));

        }

    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
            ContentToSee = e.Result;
            IsolatedStorageSettings.ApplicationSettings.Add("magazineListPage", e.Result);
            DoProcessingOnData();                   

    }


    private void DoProcessingOnData()
    {
        leftMenuControl.parent = this;

        leftMenuControl.contentToSee = ContentToSee != "" ? ContentToSee : "";

        XDocument doc = XDocument.Parse(ContentToSee);
        XElement content = null;
        foreach (XElement element in doc.Descendants("article"))
        {
            XElement refId = element.Element("refid");
            XElement page = element.Element("page");

            XElement title = element.Element("title");
            XElement images = element.Element("images");
            XElement image = null;
            if (images != null)
            {
                foreach (XElement img in images.Descendants("image"))
                {
                    image = img.Element("medium");
                }
            }
            content = element.Element("content");
            String detail = Regex.Replace(content.Value, @"<[^>]*>", String.Empty);
            String cont = "";
            if (detail.Length > 300)
            {
                cont = detail.Substring(0, 300) + ".....";

            }
            else
            {
                cont = detail;
            }
            Article art = new Article(title.Value, cont);
            art.detail = detail;
            art.page = Convert.ToInt32(page.Value);
            art.refId = refId.Value;
            if (image != null)
            {
                art.imageSrc = image.Value;
            }
            articles.Add(art);
        }

        this.databoundListBoxMag.ItemsSource = articles;
    }



    private void wb1_ScriptNotify(object sender, NotifyEventArgs e)
    {
        // The browser is zooming the text so we need to
        // reduce the pixel size by the zoom level... 
        // Which is about 0.50

    }



    private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Article art = databoundListBoxMag.SelectedItem as Article;
        string id = art.refId;
        MagazinePage page = new MagazinePage();
        page._id = id;
        page._content = art.detail;
        page._title = art.name;
        this.Content = page;
    }
    private void GestureListener_Flick(object sender, FlickGestureEventArgs e)
    {
        if (e.Angle > 90 && e.Angle < 270)
        {
            // MessageBox.Show("right");
            GridLength len = new GridLength(50);
            firstCol.Width = len;
            flipBtn.Visibility = Visibility.Visible;
            leftMenuControl.Visibility = Visibility.Collapsed;

        }
        else
        {

            // MessageBox.Show("left");
        }
    }

    private void flip_click(object sender, RoutedEventArgs e)
    {
        GridLength len = new GridLength(200);
        firstCol.Width = len;
        flipBtn.Visibility = Visibility.Collapsed;
        leftMenuControl.Visibility = Visibility.Visible;
    }
    private void SaveHTMLFile2()
    {
        string fileName = "TextFile2.htm";
        IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication();
        if (isolatedStorageFile.FileExists(fileName) == true)
        {
            isolatedStorageFile.DeleteFile(fileName);
        }
        StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
        using (BinaryReader binaryReader = new BinaryReader(streamResourceInfo.Stream))
        {
            byte[] data = binaryReader.ReadBytes((int)streamResourceInfo.Stream.Length);

            using (BinaryWriter bw = new BinaryWriter(isolatedStorageFile.CreateFile(fileName)))
            {
                bw.Write(data);
                bw.Close();
            }
        }
    }

}


public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return System.Convert.ToBoolean(value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value.Equals(Visibility.Visible);
    }



}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
Net系统;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Animation;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
使用Microsoft.Phone.Controls;
使用System.Windows.Data;
利用制度全球化;
使用System.Xml;
使用System.Xml.Linq;
使用System.IO;
使用System.IO.IsolatedStorage;
使用System.Windows.Resources;
使用System.Text.RegularExpressions;
使用visionlink.DTO;
命名空间visionlink.Views
{
公共部分类MagazineListPage:UserControl
{
公共杂志列表页()
{
初始化组件();
}
列表项目=新列表();
public int CatalogId{get;set;}
公共字符串ContentToSee=string.Empty;
已加载专用void PhoneApplicationPage_(对象发送方,路由目标)
{
databoundListBoxMag.SelectionChanged+=新的SelectionChangedEventHandler(listBox1\u SelectionChanged);
//List listMag=新列表();
//listMag.Clear();
if(IsolatedStorageSettings.ApplicationSettings.Contains(“magazineListPage”))
{
隔离存储设置.ApplicationSettings.TryGetValue(“magazineListPage”,OutContentToSee);
DoProcessingOnData();
}
其他的
{
WebClient客户端=新的WebClient();
client.DownloadStringCompleted+=新的DownloadStringCompletedEventHandler(client\u DownloadStringCompleted);
client.DownloadStringAsync(新Uri(“http://device01.e-pages.dk/content/default4.php?customer=kaleva&catalog=“+CatalogId,UriKind.RelativeOrAbsolute”);
}
}
无效客户端\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
ContentToSee=e.结果;
隔离存储设置。应用程序设置。添加(“magazineListPage”,e.Result);
DoProcessingOnData();
}
私有void DoProcessingOnData()
{
leftMenuControl.parent=此;
leftMenuControl.contentToSee=contentToSee!=“”?contentToSee:“”;
XDocument doc=XDocument.Parse(ContentToSee);
XElement内容=空;
foreach(文档子体(“文章”)中的XElement元素)
{
XElement refId=元素。元素(“refId”);
XElement页面=element.element(“页面”);
XElement title=元素。元素(“标题”);
XElement images=element.element(“图像”);
像素图像=空;
如果(图像!=null)
{
foreach(images.substands(“image”)中的XElement img)
{
图像=图像元素(“中等”);
}
}
内容=元素。元素(“内容”);
String detail=Regex.Replace(content.Value,@“]*>”,String.Empty);
字符串cont=“”;
如果(细节长度>300)
{
cont=详细信息。子字符串(0,300)+“…”;
}
其他的
{
cont=详细信息;
}
文章艺术