我有一个用户控件,并且在该用户控件中有 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);
}
}
}