Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/70.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# 在代码隐藏中设置控件的StaticResource样式_C#_Xaml_Windows Phone 8_Windows Phone 8.1 - Fatal编程技术网

C# 在代码隐藏中设置控件的StaticResource样式

C# 在代码隐藏中设置控件的StaticResource样式,c#,xaml,windows-phone-8,windows-phone-8.1,C#,Xaml,Windows Phone 8,Windows Phone 8.1,比方说,我有这样的东西(在MainPage.xaml中): 你可以设置,像这样的 TextBlock myTextBlock= new TextBlock () { FontFamily = new FontFamily("Segoe UI Light"); Style = Resources["TextBlockStyle"] as Style, }; 您可以使用以下选项: Style textBlockStyle; try { t

比方说,我有这样的东西(在MainPage.xaml中):


你可以设置,像这样的

  TextBlock myTextBlock= new TextBlock ()
    {
        FontFamily = new FontFamily("Segoe UI Light");
        Style = Resources["TextBlockStyle"] as Style,
    };
您可以使用以下选项:

Style textBlockStyle;
try
{
    textBlockStyle = FindResource("TextBlockStyle") as Style;
}
catch(Exception ex)
{
    // exception handling
}

if(textBlockStyle != null)
{
    myTextBlock.Style = textBlockStyle;
}
TryFindResource
方法:

myTextBlock.Style = (Style)TryFindResource("TextBlockStyle");

这个问题已经问了4年多了,但我想发布一个答案来分享我的发现

例如,如果在
App.xaml
(Xamarin跨平台应用程序开发)中的应用程序资源中描述了
样式
蓝色按钮
,则可以如下使用

<?xml version="1.0" encoding="utf-8" ?><Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="SharedUi.App">
<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="BlueButton" TargetType="Button">
            <Setter Property="TextColor" Value="White" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="BackgroundColor" Value="Blue"/>
            <Setter Property="HeightRequest" Value="70"/>
            <Setter Property="FontAttributes" Value="Bold"/>
        </Style>            
    </ResourceDictionary>
</Application.Resources></Application>

我知道这很古老,但是有人知道如何使用FrameworkElementFactory(typeof(TextBlock))做同样的事情吗?我只把这些调用看作是Android特定的API。
myTextBlock.Style = (Style)TryFindResource("TextBlockStyle");
<?xml version="1.0" encoding="utf-8" ?><Application xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="SharedUi.App">
<Application.Resources>
    <ResourceDictionary>
        <Style x:Key="BlueButton" TargetType="Button">
            <Setter Property="TextColor" Value="White" />
            <Setter Property="FontSize" Value="20" />
            <Setter Property="BackgroundColor" Value="Blue"/>
            <Setter Property="HeightRequest" Value="70"/>
            <Setter Property="FontAttributes" Value="Bold"/>
        </Style>            
    </ResourceDictionary>
</Application.Resources></Application>
Button newButton1 = new Button
{
    Text = "Hello",
    WidthRequest = (double)15.0,
    Style = (Style)Application.Current.Resources["BlueButton"]
};