Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在CodeBehind上使用XAML样式_C#_Silverlight_Xaml_Windows Phone 8 - Fatal编程技术网

C# 在CodeBehind上使用XAML样式

C# 在CodeBehind上使用XAML样式,c#,silverlight,xaml,windows-phone-8,C#,Silverlight,Xaml,Windows Phone 8,伙计们,我正在尝试将style.xaml中的一种样式应用到我的代码隐藏中 在我的风格上,我有这样的代码 txt.Foreground = new SolidColorBrush(Color.FromArgb(255, 252, 147, 25)); 文件Style.xaml <SolidColorBrush x:Key="FontGrey" Color="#FFC5C0C0"></SolidColorBrush> 如果我想在style.xaml上更改前景色 我该

伙计们,我正在尝试将style.xaml中的一种样式应用到我的代码隐藏中 在我的风格上,我有这样的代码

txt.Foreground =  new SolidColorBrush(Color.FromArgb(255, 252, 147, 25));
文件Style.xaml

<SolidColorBrush x:Key="FontGrey" Color="#FFC5C0C0"></SolidColorBrush>  
如果我想在style.xaml上更改前景色 我该怎么做?我试着使用资源,但不起作用


注意:Style.xaml和Apptest.xaml是分开的

如果假设资源可用,那么此代码应该适用于您:

txt.Foreground = (Brush)FindResource("FontGrey");

您可以将样式放入Window.Apptest.xaml中的资源,如下所示:

    <ResourceDictionary >
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary 
              Source="Style1.xaml">
            </ResourceDictionary>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>

您可以使用以下语法在Silverlight中访问定义的资源:

txt.Foreground = (SolidColorBrush)Application.Current.Resources["FontGrey"];

谢谢你的夸奖,先生。。。。但是在我的代码中找不到FindResources函数。请检查您的使用情况,这是关于该函数的msdn。我的意思是,当我键入FindRes时,没有提供我的FindResources函数。。它还没有提供,我如何解决这个问题?感谢您的关注sirSilverlight.thx中没有此方法请注意,先生,我应该在哪里添加代码?在Apptest.xaml上还是在Style.xaml上?因为当我试图将代码放在Style.xaml上时,它不起作用。在Apptest.xaml.cs中的窗口代码隐藏类中,您可以将其放在调用InitializeComponent()之后,例如,在调用InitializeComponent()之后;然后,因为我认为它是xaml语言,很抱歉,如果我没有领会你的意思,因为我仍然是一个nooob,我想学习:))当我试图将代码放入页面中时。资源它要求一个相关的键(每个字典条目必须有一个相关的键)。那个么它意味着什么呢?你们在页面中有其他的资源吗?除了ResourceDictionary之外的资源吗?
txt.Foreground = (SolidColorBrush)Application.Current.Resources["FontGrey"];