积极答复者
关于Windows Phone Class Library项目中使用资源样式的问题,问了Webcast的老师也没有给出答案

问题
-
步骤:新建一个Windows Phone Class Library - MyCompany.Controls,然后新建目录Themes,
新建Generic.cs,改名Generic.xaml,内容 <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:shell="clr-namespace:
Microsoft.Phone.Shell; assembly=Microsoft.Phone" xmlns:controls="clr-namespace:
Microsoft.Phone.Controls; assembly=Microsoft.Phone. Controls" xmlns:primitives="clr-
namespace:Microsoft.Phone. Controls.Primitives;assembly= Microsoft.Phone.Controls" xmlns:local="clr-namespace:
MyCompany.Controls"> <Style x:Key="MyButtonBase" TargetType="ButtonBase">
<Setter Property="Background" Value="Red"/>
</Style>
</ResourceDictionary>
Generic.xaml中是一个带key的样式,测试页面写:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel>
<Button Style="{StaticResource MyButtonBase}">a</Button>
<Button Style="{StaticResource MyButtonBase}">b</Button>
</StackPanel>
</Grid>
结果完全没有用,无论把Generic.xaml的Build Action设置为Content/Resource/
Page/EmbeddedResource ...,而且有提示说 The resource "PhoneButtonBase" could not be resolved。如果把style放到测试页面的 <phone:PhoneApplicationPage.
Resources> <Style x:Key="MyButtonBase" TargetType="ButtonBase">
<Setter Property="Background" Value="Red"/>
</Style>
</phone:PhoneApplicationPage.
Resources> 这可以正常看到按钮的背景是红色。
请问怎么解决Generic.xaml的样式问题,谢谢。
建了一个测试项目:http://115.com/file/e71dzkhy#Sample.zip
- 已编辑 __Untitled 2012年5月30日 14:50
答案
-
我添加的代码是在WindowsPhoneControl1中的:
<UserControl x:Class="MyCompany.Controls.WindowsPhoneControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyCompany.Controls" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="480" d:DesignWidth="480"> <UserControl.Resources> <ResourceDictionary Source="Themes/Generic.xaml" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="{StaticResource AppBackgroundBrush}"> <Button Style="{StaticResource PhoneButtonBase}" /> </Grid> </UserControl>
实际自定义UserControl和自定义Control是不一样的.
自定义UserControl类似于界面,要Style的时候就要加载.(有点类似于ASP.NET页面中需要添加Javascript一样)
- 已标记为答案 __Untitled 2012年5月31日 8:59
全部回复
-
Hi,
首先,不建议使用名字Generic.xaml,这个名字是专门用来自定义控件,当然你直接用也是没问题的,但是如果将来要自定义控件,就会搞混.
其次,因为你没有导入到当前页面上,所以,无法检索到这个Style资源文件.
具体改法如下:
把这三行的注释去掉:
<SolidColorBrush x:Key="AppBackgroundBrush" Color="#1b85cb"/> <SolidColorBrush x:Key="AppForegroundBrush" Color="White"/> <SolidColorBrush x:Key="AppDisabledBrush" Color="#75b5de"/>
然后加上:
<UserControl.Resources> <ResourceDictionary Source="Themes/Generic.xaml" /> </UserControl.Resources>
-
第一,我做这个Windows Phone Class Library就是为了放所有的自定义控件,您可以打开这个文件 http://115.com/file/e71dzkhy#Sample.zip 里面就是两个测试项目,MyCompany.Controls是放所有UserControl的项目,当然可能有少量的公共类库文件。
第二,去掉注释增加<UserControl.Resources>也还是没用的,提示找不到,更改Generic.xaml BuildAction为Page/Content/Resource都不行。
MyCompany.Controls项目的类型是打算和Coding4Fun之类的控件库作用是一样的,所以使用Themes/Generic.xaml是没问题的,问题是怎么不起作用呢?
能否帮忙打开我的测试项目帮忙看看,谢谢。
- 已编辑 __Untitled 2012年5月31日 3:44
-
你好,
我似乎搞糊涂了,项目我已经打开了.
首先你先看下我这个回复:
就看代码,你会发现,我自定义了一个继承自Controls.Button的Control DyaButton.
而你的控件是组合式的,且WindowsPhoneControl1继承自UserControl,是有差别的.
DyaButton : Controls.Button
然后我通过在构造函数里读取DyaButtonStyle.这里是是可以反射到Generic.xaml的
public DyaButton()
{base.DefaultStyleKey = typeof(DyaButton);} -
我添加的代码是在WindowsPhoneControl1中的:
<UserControl x:Class="MyCompany.Controls.WindowsPhoneControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:MyCompany.Controls" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="480" d:DesignWidth="480"> <UserControl.Resources> <ResourceDictionary Source="Themes/Generic.xaml" /> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="{StaticResource AppBackgroundBrush}"> <Button Style="{StaticResource PhoneButtonBase}" /> </Grid> </UserControl>
实际自定义UserControl和自定义Control是不一样的.
自定义UserControl类似于界面,要Style的时候就要加载.(有点类似于ASP.NET页面中需要添加Javascript一样)
- 已标记为答案 __Untitled 2012年5月31日 8:59