日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Windows Phone 7 自定义控件库

發(fā)布時間:2025/6/15 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Windows Phone 7 自定义控件库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Windows Phone 7自定義一個控件庫跟Silverlight的是基本一樣的,第一步創(chuàng)建一個類庫,然后添加一個Themes文件夾,在文件夾里面添加上generic.xaml文件作為默認的控件樣式文件,記住一定要寫這個名字否則就找不到樣式了,大小寫都可以。新建一個控件類MyContro1.cs,MyContro2.cs在這里面就可以寫控件的處理邏輯了。

下面看一下一個水印控件的處理:

generic.xaml文件

?

  • <ResourceDictionary?
  • ????xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"?
  • ????xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"?
  • ????xmlns:local="clr-namespace:Phone.Controls;assembly=Phone.Controls"?
  • ????xmlns:d="http://schemas.microsoft.com/expression/blend/2008"?
  • ????xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"??? ?
  • ????mc:Ignorable="d"?
  • ????>?
  • ????<Style??TargetType="local:WatermarkedTextBox">?
  • ????????<Setter?Property="FontFamily"?Value="{StaticResource?PhoneFontFamilyNormal}"/>?
  • ????????<Setter?Property="FontSize"?Value="{StaticResource?PhoneFontSizeMediumLarge}"/>?
  • ????????<Setter?Property="Background"?Value="White"/>?
  • ????????<Setter?Property="Foreground"?Value="#FF3880B2"/>?
  • ????????<Setter?Property="BorderBrush"?Value="#FF3880B2"/>?
  • ????????<Setter?Property="SelectionBackground"?Value="#FF3880B2"/>?
  • ????????<Setter?Property="SelectionForeground"?Value="{StaticResource?PhoneTextBoxSelectionForegroundBrush}"/>?
  • ????????<Setter?Property="BorderThickness"?Value="{StaticResource?PhoneBorderThickness}"/>?
  • ????????<Setter?Property="Padding"?Value="2"/>?
  • ????????<Setter?Property="Template">?
  • ????????????<Setter.Value>?
  • ????????????????<ControlTemplate?TargetType="local:WatermarkedTextBox">?
  • ????????????????????<Grid?Background="Transparent">?
  • ????????????????????????<VisualStateManager.VisualStateGroups>?
  • ????????????????????????????<VisualStateGroup?x:Name="CommonStates">?
  • ????????????????????????????????<VisualState?x:Name="Normal"/>?
  • ????????????????????????????????<VisualState?x:Name="MouseOver"/>?
  • ????????????????????????????????<VisualState?x:Name="Disabled">?
  • ????????????????????????????????????<Storyboard>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Visibility"?Storyboard.TargetName="EnabledBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0">?
  • ????????????????????????????????????????????????<DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????????????<Visibility>Collapsed</Visibility>?
  • ????????????????????????????????????????????????</DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????</DiscreteObjectKeyFrame>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Visibility"?Storyboard.TargetName="DisabledOrReadonlyBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0">?
  • ????????????????????????????????????????????????<DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????????????<Visibility>Visible</Visibility>?
  • ????????????????????????????????????????????????</DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????</DiscreteObjectKeyFrame>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????</Storyboard>?
  • ????????????????????????????????</VisualState>?
  • ????????????????????????????????<VisualState?x:Name="ReadOnly">?
  • ????????????????????????????????????<Storyboard>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Visibility"?Storyboard.TargetName="EnabledBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0">?
  • ????????????????????????????????????????????????<DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????????????<Visibility>Collapsed</Visibility>?
  • ????????????????????????????????????????????????</DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????</DiscreteObjectKeyFrame>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Visibility"?Storyboard.TargetName="DisabledOrReadonlyBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0">?
  • ????????????????????????????????????????????????<DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????????????<Visibility>Visible</Visibility>?
  • ????????????????????????????????????????????????</DiscreteObjectKeyFrame.Value>?
  • ????????????????????????????????????????????</DiscreteObjectKeyFrame>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Background"?Storyboard.TargetName="DisabledOrReadonlyBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0"?Value="White"/>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="BorderBrush"?Storyboard.TargetName="DisabledOrReadonlyBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0"?Value="#FF3880B2"/>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Foreground"?Storyboard.TargetName="DisabledOrReadonlyContent">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0"?Value="{StaticResource?PhoneTextBoxReadOnlyBrush}"/>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????</Storyboard>?
  • ????????????????????????????????</VisualState>?
  • ????????????????????????????</VisualStateGroup>?
  • ????????????????????????????<VisualStateGroup?x:Name="FocusStates">?
  • ????????????????????????????????<VisualState?x:Name="Focused">?
  • ????????????????????????????????????<Storyboard>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="Background"?Storyboard.TargetName="EnabledBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0"?Value="White"/>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????????<ObjectAnimationUsingKeyFrames?Storyboard.TargetProperty="BorderBrush"?Storyboard.TargetName="EnabledBorder">?
  • ????????????????????????????????????????????<DiscreteObjectKeyFrame?KeyTime="0"?Value="#FF3880B2"/>?
  • ????????????????????????????????????????</ObjectAnimationUsingKeyFrames>?
  • ????????????????????????????????????</Storyboard>?
  • ????????????????????????????????</VisualState>?
  • ????????????????????????????????<VisualState?x:Name="Unfocused"/>?
  • ?
  • ????????????????????????????</VisualStateGroup>?
  • ????????????????????????</VisualStateManager.VisualStateGroups>?
  • ????????????????????????<Border?x:Name="EnabledBorder"?BorderBrush="#FF3880B2"?BorderThickness="{TemplateBinding?BorderThickness}"?Background="{TemplateBinding?Background}"?Margin="{StaticResource?PhoneTouchTargetOverhang}">?
  • ????????????????????????????<Grid>?
  • ????????????????????????????????<ContentControl?x:Name="watermarkContent"??HorizontalContentAlignment="Left"?Style="{TemplateBinding?WatermarkStyle}"?Content="{TemplateBinding?Watermark}"?Background="Transparent"?Opacity="0.5"/>?
  • ????????????????????????????????<ContentControl?x:Name="ContentElement"?BorderThickness="0"?HorizontalContentAlignment="Stretch"?Margin="{StaticResource?PhoneTextBoxInnerMargin}"?Padding="{TemplateBinding?Padding}"?VerticalContentAlignment="Stretch"/>?
  • ????????????????????????????</Grid>?
  • ????????????????????????</Border>?
  • ????????????????????????<Border?x:Name="DisabledOrReadonlyBorder"?BorderBrush="{StaticResource?PhoneDisabledBrush}"?BorderThickness="{TemplateBinding?BorderThickness}"?Background="White"?Margin="{StaticResource?PhoneTouchTargetOverhang}"?Visibility="Collapsed">?
  • ????????????????????????????<TextBox?x:Name="DisabledOrReadonlyContent"?Background="White"?Foreground="{StaticResource?PhoneDisabledBrush}"?
  • ?????????????????????????????????????FontWeight="{TemplateBinding?FontWeight}"?FontStyle="{TemplateBinding?FontStyle}"?FontSize="{TemplateBinding?FontSize}"?
  • ?????????????????????????????????????FontFamily="{TemplateBinding?FontFamily}"?IsReadOnly="True"?SelectionForeground="{TemplateBinding?SelectionForeground}"? ?
  • ?????????????????????????????????????SelectionBackground="{TemplateBinding?SelectionBackground}"?TextAlignment="{TemplateBinding?TextAlignment}"? ?
  • ?????????????????????????????????????TextWrapping="{TemplateBinding?TextWrapping}"?Text="{TemplateBinding?Text}"?Template="{StaticResource?PhoneDisabledTextBoxTemplate}"?/>?
  • ????????????????????????</Border>?
  • ????????????????????</Grid>?
  • ????????????????</ControlTemplate>?
  • ????????????</Setter.Value>?
  • ????????</Setter>?
  • ????</Style>?
  • ?
  • </ResourceDictionary>?
  • WatermarkTextBox..cs

    ?

  • using?System.Windows; ?
  • using?System.Windows.Controls; ?
  • ?
  • namespace?Phone.Controls ?
  • { ?
  • ????public?class?WatermarkedTextBox?:?TextBox ?
  • ????{ ?
  • ????????ContentControl?WatermarkContent; ?
  • ????????public?static?readonly?DependencyProperty?WatermarkProperty?= ?
  • ??????DependencyProperty.Register("Watermark",?typeof(object),?typeof(WatermarkedTextBox),?new?PropertyMetadata(OnWatermarkPropertyChanged)); ?
  • ?
  • ????????public?static?readonly?DependencyProperty?WatermarkStyleProperty?= ?
  • ??????DependencyProperty.Register("WatermarkStyle",?typeof(Style),?typeof(WatermarkedTextBox),?null); ?
  • ?
  • ????????public?Style?WatermarkStyle ?
  • ????????{ ?
  • ????????????get?{?return?base.GetValue(WatermarkStyleProperty)?as?Style;?} ?
  • ????????????set?{?base.SetValue(WatermarkStyleProperty,?value);?} ?
  • ????????} ?
  • ?
  • ????????public?object?Watermark ?
  • ????????{ ?
  • ????????????get?{?return?base.GetValue(WatermarkProperty)?as?object;?} ?
  • ????????????set?{?base.SetValue(WatermarkProperty,?value);?} ?
  • ????????} ?
  • ?
  • ????????public?WatermarkedTextBox() ?
  • ????????{ ?
  • ????????????DefaultStyleKey?=?typeof(WatermarkedTextBox); ?
  • ????????} ?
  • ?
  • ????????public?override?void?OnApplyTemplate() ?
  • ????????{ ?
  • ????????????base.OnApplyTemplate(); ?
  • ????????????thisthis.WatermarkContent?=?this.GetTemplateChild("watermarkContent")?as?ContentControl; ?
  • ????????????if(WatermarkContent?!=?null) ?
  • ????????????{ ?
  • ??????????????DetermineWatermarkContentVisibility(); ?
  • ????????????} ?
  • ????????} ?
  • ?
  • ????????protected?override?void?OnGotFocus(RoutedEventArgs?e) ?
  • ????????{ ?
  • ????????????if?(WatermarkContent?!=?null?&&?string.IsNullOrEmpty(this.Text)) ?
  • ????????????{ ?
  • ????????????????this.WatermarkContent.Visibility?=?Visibility.Collapsed; ?
  • ????????????} ?
  • ????????????base.OnGotFocus(e); ?
  • ????????} ?
  • ?
  • ????????protected?override?void?OnLostFocus(RoutedEventArgs?e) ?
  • ????????{ ?
  • ????????????if?(WatermarkContent?!=?null?&&?string.IsNullOrEmpty(this.Text)) ?
  • ????????????{ ?
  • ????????????????this.WatermarkContent.Visibility?=?Visibility.Visible; ?
  • ????????????} ?
  • ????????????base.OnLostFocus(e); ?
  • ????????} ?
  • ?
  • ????????private?static?void?OnWatermarkPropertyChanged(DependencyObject?sender,?DependencyPropertyChangedEventArgs?args) ?
  • ????????{ ?
  • ????????????WatermarkedTextBox?watermarkTextBox?=?sender?as?WatermarkedTextBox; ?
  • ????????????if(watermarkTextBox?!=?null?&&?watermarkTextBox.WatermarkContent?!=null) ?
  • ????????????{ ?
  • ??????????????watermarkTextBox.DetermineWatermarkContentVisibility(); ?
  • ????????????} ?
  • ????????} ?
  • ?
  • ????????private?void?DetermineWatermarkContentVisibility() ?
  • ????????{ ?
  • ????????????if?(string.IsNullOrEmpty(this.Text)) ?
  • ????????????{ ?
  • ????????????????this.WatermarkContent.Visibility?=?Visibility.Visible; ?
  • ????????????} ?
  • ????????????else ?
  • ????????????{ ?
  • ????????????????this.WatermarkContent.Visibility?=?Visibility.Collapsed; ?
  • ????????????} ?
  • ????????} ?
  • ????} ?
  • }?
  • ?

    轉(zhuǎn)載于:https://blog.51cto.com/linzheng/1078436

    總結(jié)

    以上是生活随笔為你收集整理的Windows Phone 7 自定义控件库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。