询问者
x:Name与x:Key

问题
-
MainPage的资源中提添加:
<phone:PhoneApplicationPage.Resources>
<LinearGradientBrush x:Name="color">
</phone:PhoneApplicationPage.Resources>在code-behind的中的Load中写上myTxt.Foreground = color;没显示任何警告或错误,但没有显示myTxt的内容...
改成myTxt.Foreground = (LinearGradientBrush)this.Resources["color"];就行了。。。
不是说用x:Name的话可以直接引用资源的吗?
全部回复
-
public partial class <pre lang="x-xml"><UserControl x:Class="SilverlightApplication6.XXXXXXX" 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" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <UserControl.Resources> <SolidColorBrush x:Name="cxt" x:Key="yyy"/> </UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White"> <Button Content="Button" Height="23" HorizontalAlignment="Left" Click="button1_Click" Margin="202,120,0,0" Name="button1" VerticalAlignment="Top" Width="75" /> </Grid> </UserControl>
using System.Windows; using System.Windows.Controls; using System.Windows.Browser; using System.Windows.Media; namespace SilverlightApplication6 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); var tt = this.cxt; var t2 = this.Resources["yyy"] as SolidColorBrush; } private void button1_Click(object sender, RoutedEventArgs e) { var hello = HtmlPage.Window.Invoke("onSilverlightLoad",new object[]{ this}); } } }
努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!- 已编辑 天使坠Moderator 2011年11月7日 3:26 代码样式修改
-
因为它是自资源.
你以后记住x:name用在非资源的地方。当然你也可以用x:key
x:key用在资源中<xxxxxx.Resources> 然后用Resources得到。
如果你在load事件中使用,你一定要在load完成后才可以调用,否则资源还没创建,因为他也是对象。
努力!完成了js解析器,还差一个svg插件,一个绘图程序,做好自己,呵呵~!你好。
其实我觉得提问者想知道以下内容。
同样在code-behind的中的Load中,为什么1不行,为什么2就可以。
1,myTxt.Foreground = color;没显示任何警告或错误,但没有显示myTxt的内容
2,myTxt.Foreground = (LinearGradientBrush)this.Resources["color"];就行了
如果按你所说的【如果你在load事件中使用,你一定要在load完成后才可以调用,否则资源还没创建,因为他也是对象。】,似乎1和2都应该不可以的吧。
我也是初学者,有同样的疑问。
- 已编辑 xugy0926 2011年11月7日 15:21