如何重用在Xamarin.Forms中的父ContentView中定义的样式

时间:2018-02-28 19:08:00

标签: c# xaml xamarin xamarin.forms

拳头,我有一个名为FieldView的组件以这种方式定义:

<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="CustomViews.Components.Fields.FieldView">

    <ContentView.Resources>
        <ResourceDictionary>
            <Style x:Key="LabelStyle" TargetType="Label">
                <Setter Property="FontSize" Value="20"></Setter>
        </ResourceDictionary>
    </ContentView.Resources>

    <StackLayout>
        <Label Text="Hello world" Style="{StaticResource LabelStyle}"></Label>
    </StackLayout>

</ContentView>

FieldView组件的样式名为&#34; LabelStyle&#34;。显然我可以在FieldView组件中使用这种样式。但是这个组件是一个基本组件,将由其他组件继承,如TextFieldView,实现如下:

<?xml version="1.0" encoding="UTF-8"?>
<local:FieldView xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:local="clr-namespace:GeoSapiens.Coletum.CustomViews.Components.Fields"
    x:Class="GeoSapiens.Coletum.CustomViews.Components.Fields.TextFieldView">

    <StackLayout>

        <!-- the style is not accessible here -->
        <Label Text="Hello from TextFieldView" Style="{StaticResource LabelStyle}"></Label>

    </StackLayout>

</local:FieldView>

问题在于我无法使用FieldView组件内TextFieldView中定义的样式。

有没有办法可以引用FieldView内部TextFieldView组件中定义的样式?即:访问父组件中定义的样式。我应该以任何方式使用代码隐藏文件吗?

3 个答案:

答案 0 :(得分:2)

如果您在整个应用中的多个视图中使用相同的Style我可能只是将您的风格移到App.xaml并从那里使用它。

但是,如果TextFieldView基类设置为FieldView,那么您尝试执行的操作应该有效,但从您的代码看起来您​​的资源未在其中正确定义且缺失结束</Style>。例如,下面的代码在我试用并在xaml页面中使用TextFieldView时起作用。

的FieldView:

<ContentView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="TestApp.Forms.FieldView">
    <ContentView.Resources>
        <ResourceDictionary>
            <Style x:Key="LabelStyle" TargetType="Label">
                <Setter Property="FontSize" Value="40" />
            </Style>
        </ResourceDictionary>
    </ContentView.Resources>
    <StackLayout>
        <Label Text="Hello world" Style="{StaticResource LabelStyle}" />
    </StackLayout>
</ContentView>

TextFieldView:

<local:FieldView 
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:NuclearHalfLife.Forms" 
    x:Class="TestApp.Forms.TextFieldView">
    <StackLayout>
        <Label Text="Hello from TextFieldView" Style="{StaticResource LabelStyle}" />
    </StackLayout>
</local:FieldView>

答案 1 :(得分:1)

尼克的答案确实有效,这只是一个语法错误,我无法想象你的应用程序是如何运行的......

但是,如果您创建一个方法来提供File "/home/tom/Desktop/f2py/Plot.py", line 5, in <module> print(Sigma._doc_) AttributeError: 'module' object has no attribute '_doc_' 而不是在XAML上定义它,您将能够扩展样式(以及任何其他资源)。所以您的基本视图将如下所示:

1> RESTORE DATABASE db
2> FROM DISK = '/var/opt/mssql/db.bak' ;
3> GO

然后,在后面的代码中,您应该实现并使用该方法来获取资源字典:

Msg 5133, Level 16, State 1, Server mbü-lubuntu, Line 1
Directory lookup for the file "D:\Program Files\Microsoft SQL 
Server\MSSQL12.MSSQLSERVER2014\MSSQL\DATA\db.mdf" failed with the operating     
system error 2(The system cannot find the file specified.).
Msg 3156, Level 16, State 3, Server mbü-lubuntu, Line 1
File 'db' cannot be restored to 'D:\Program Files\Microsoft SQL 
Server\MSSQL12.MSSQLSERVER2014\MSSQL\DATA\db.mdf'. Use WITH MOVE to     
identify a valid location for the file.
Msg 5133, Level 16, State 1, Server mbü-lubuntu, Line 1
Directory lookup for the file "D:\Program Files\Microsoft SQL 
Server\MSSQL12.MSSQLSERVER2014\MSSQL\DATA\db_log.ldf" failed with the     
operating system error 2(The system cannot find the file specified.).
Msg 3156, Level 16, State 3, Server mbü-lubuntu, Line 1
File 'db_log' cannot be restored to 'D:\Program Files\Microsoft SQL 
Server\MSSQL12.MSSQLSERVER2014\MSSQL\DATA\db_log.ldf'. Use WITH MOVE to     
identify a valid location for the file.
Msg 3119, Level 16, State 1, Server mbü-lubuntu, Line 1
Problems were identified while planning for the RESTORE statement. Previous 
messages provide details.
Msg 3013, Level 16, State 1, Server mbü-lubuntu, Line 1
RESTORE DATABASE is terminating abnormally.

在您的子视图中,您应该像在基础上一样设置[EULA] accepteula = Y [sqlagent] enabled = true [filelocation] defaultbackupdir = /var/opt/mssql/data/ defaultdatadir = /var/opt/mssql/data/ defaultdumpdir = /var/opt/mssql/data/ defaultlogdir = /var/opt/mssql/data/ 属性,并根据需要添加新资源:

ResourceDictionary

我希望它会有用。

答案 2 :(得分:1)

从新的内容页面定义单独的ResourceDictionary并使用Merged Resource Dictionaries

namespace MyNamespace
{
  [XamlCompilation(XamlCompilationOptions.Compile)]
  public partial class MyStyles : ResourceDictionary
  {
    public MyStyles()
    {
        InitializeComponent();
    }
  }
}

重新定义背后的代码:

ContentView

可以在xmlns:resDictStyles="clr-namespace:MyNamespace" ... <ContentView.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <resDictStyles:MyStyles /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </ContentView.Resources> ... <Label Style="{StaticResource LabelResDictStyle}" />

中引用单独资源字典中的样式
BasedOn

来自合并资源字典的样式可以&#34;扩展&#34;使用xmlns:resDictStyles="clr-namespace:MyNamespace" ... <ContentPage.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <resDictStyles:MyStyles /> </ResourceDictionary.MergedDictionaries> <Style x:Key="LabelPageStyle" TargetType="Label" BasedOn="{StaticResource LabelResDictStyle}"> <Setter Property="FontSize" Value="20"></Setter> </Style> </ResourceDictionary> </ContentPage.Resources> ... <Label Style="{StaticResource LabelPageStyle}" />

$ VirtualBox --help
Oracle VM VirtualBox Manager 5.0.16