WP7的自定义控件(generic.xaml)

时间:2011-07-29 08:46:57

标签: windows-phone-7 custom-controls expression-blend

我目前正在执行http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps步骤?在为WP7做一个自定义控件。我已经在普通的Windows手机肖像页面xaml文件中创建了我的控件(结合了一些控件),我不知道如何将它转换为在generic.xaml文件中工作(作为ResourceDictionary)。到目前为止它没有用。

我尝试使用Expression Blend进行转换,但我不知道该怎么做。

编辑:我发布了我的代码,它是一个显示动态时间的框。我还想添加用于提供日期的属性和用于框的颜色的另一个属性。

这是到目前为止的代码。

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows"
xmlns:local="clr-namespace:CereTime"> 
    <!-- Check xmlns:local im case of error -->
    <Style TargetType="local:CereT1">
        <!-- After specifing the custom properties in .cs file, implement them here -->
        <Setter Property="Date" Value="{TemplateBinding Date}" /> <!-- Under check -->
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="local:CereT1">
                    <Canvas Background="YellowGreen" Width="100" Height="100" Name="DateBox" HorizontalAlignment="Left" VerticalAlignment="Top">
                        <StackPanel Orientation="Vertical" Height="100" Width="100">
                            <TextBlock Name="Month" Text="Month" Foreground="White" TextAlignment="Center" HorizontalAlignment="Center" FontSize="24" FontWeight="Bold" Margin="0,12,0,0" />
                            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,6,0,0">
                                <TextBlock Name="Date" Text="0"  VerticalAlignment="Bottom" Margin="0,0,5,0" FontSize="26.667"/>
                                <TextBlock Name="No_Name" Text="|" FontSize="26.667" />
                                <TextBlock Name="Year" Text="Year" Margin="5,0,0,0" FontSize="26.667" />
                            </StackPanel>
                        </StackPanel>
                    </Canvas>
                </ControlTemplate>              
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

请告诉我。 谢谢,

2 个答案:

答案 0 :(得分:0)

虽然您可以将用户控件的样式移动到ResourceDictionary,但为什么在您的UserControl的相应XAML中具有该模板和模板时会感到烦恼?

简单设置MyUserControl.xaml内部,以及您想要更改的其他属性。

但是关于将自定义控件的样式分离到ResourceDictionary的整个部分与UserControls没有太大关系。也许你应该告诉我们真正错误是什么,而不是询问元问题。

答案 1 :(得分:0)

在尝试创建控件后,我遇到了同样的问题 解决方案是添加

 <ResourceDictionary>       
     <ResourceDictionary.MergedDictionaries>
         <ResourceDictionary Source="themes/generic.xaml"/>
     </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>

进入 App.xaml

中的Application.Resources标记
相关问题