【问题标题】:how to define/override Style for ContentView when it is child of ContentPage当 ContentView 是 ContentPage 的子项时,如何为 ContentView 定义/覆盖样式
【发布时间】:2016-12-07 17:26:33
【问题描述】:

我有如下内容视图

        <?xml version="1.0" encoding="utf-8" ?>
        <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
                     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                     xmlns:convertors="clr-namespace:myApp.Convertors;assembly=myApp"   
                     x:Class="myApp.cwPart">

          <ContentView.Content>
            <StackLayout Orientation="Horizontal"
                         BackgroundColor="White">
              <RelativeLayout Style="{StaticResource myStyle}"   
                              HorizontalOptions="Start"
                              VerticalOptions="Center">

我在多个ContentPages 中引用了这个ContentView,作为下面的示例。每个ContentPage 应该设置不同的BackgroundColorHeightWidth。这就是为什么我认为应该在ContentPage 中定义样式,但它会引发错误,即无法识别。我怎样才能做到这一点?

PS,有趣的是我正在使用 Gorilla 播放器来实现这样的更改,而 Gorilla 播放器没有返回任何错误:)

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:convertors="clr-namespace:myApp.Convertors;assembly=myApp"
             xmlns:c="clr-namespace:myApp;assembly=myApp"
             x:Class="myApp.AppLogsPage"
             Title="{Binding AppName}" 
             x:Name="AppLogsPage">

  <ContentPage.Resources>

    <ResourceDictionary>

      <Style x:Key="myStyle" TargetType="RelativeLayout">
        <Setter Property="HeightRequest" Value="148"/>
        <Setter Property="WidthRequest" Value="80"/>
        <Setter Property="BackgroundColor" Value="White"/>
      </Style>
    </ResourceDictionary>

  </ContentPage.Resources>

    <ContentPage.Content>
      <StackLayout x:Name="MainHolder">
        <c:cwPart   />
       ...

如果我如下定义 ContentView 资源,它可以正常工作,但我无法覆盖 ContentView 中定义的样式

            <?xml version="1.0" encoding="utf-8" ?>
            <ContentView xmlns="http://xamarin.com/schemas/2014/forms"
                         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                         xmlns:convertors="clr-namespace:myApp.Convertors;assembly=myApp"   
                         x:Class="myApp.cwPart">

            <ContentView.Resources>

    <ResourceDictionary>

      <Style x:Key="myStyle" TargetType="RelativeLayout">
        <Setter Property="HeightRequest" Value="148"/>
        <Setter Property="WidthRequest" Value="80"/>
        <Setter Property="BackgroundColor" Value="White"/>
      </Style>
    </ResourceDictionary>

  </ContentView.Resources>

              <ContentView.Content>
                <StackLayout Orientation="Horizontal"  BackgroundColor="White">
                  <RelativeLayout  Style="{StaticResource myStyle}"   
       HorizontalOptions="Start" VerticalOptions="Center" >

【问题讨论】:

    标签: xamarin xamarin.android xamarin.forms


    【解决方案1】:

    我会在你的方法中改变很多问题,但让我们保持简单。首先,我会使用带有控件模板的 contentview;

    在 App.xaml 中;

    <ControlTemplate x:Key="MyControlTemplate">
        <StackLayout Orientation="Horizontal"  BackgroundColor="White">
                      <RelativeLayout HorizontalOptions="Start" VerticalOptions="Center" />
    </StackLayout>
      </ControlTemplate>
    

    然后在您的 page.xaml 中

    <ContentView ControlTemplate={StaticResource MyControlTemplate}/>
    

    如果您想为每个页面设置单独的样式,那么您可以为每个页面创建一个控件模板。

    【讨论】:

    • 我认为我可以像 wpf 中的 Usercontrol 一样使用 contentview。这就是为什么我更喜欢这种方法,但如果它有效,我愿意接受任何其他建议。如果我理解正确,您的建议也是静态的,但不是动态的。我在 app.xaml 或视图本身中看不到设置 contentview 高度和重量的任何差异。也许我错过了你的观点?
    • 是的,没错,样式是静态的,因为它们必须在某个地方声明,没有定义它们就没有神奇的方法。但我理解你的方法,因为我也有 WPF 背景。首先确保您的样式是在 App.xaml 中定义的。然后尝试像以前一样设置你的风格。第三,如果你想创建一个类似 usercontrol 的 WPF,然后创建一个从 ContentView 派生的类,并使用一个构造函数来分配你的样式,控制模板来自资源 (app.xaml)。
    • 好的,我大致了解,但是您在哪里定义 RelativeLayout 的样式。在我的问题中的 ContentPage 中?
    • 是的,在 app.xaml 中定义你的全局样式,你也可以在特定页面中定义一个样式。请看一下我关于用户控制的第二个答案。请注意,如果您的解决方案中的页面与您的应用程序 PCL 项目不同,则需要使用 DynamicResource 关键字
    • 如果我在 app.xaml 中定义了 controltemplates,是否不会将额外的负载放入内存,因为它是在应用程序加载时加载的,并且在内存中变得像静态一样。
    【解决方案2】:

    创建类似 WPF 的用户控件;

    在 App.xaml 中;

    <ControlTemplate x:Key="MyControlTemplate">
        <StackLayout Orientation="Horizontal"  BackgroundColor="White">
                      <RelativeLayout HorizontalOptions="Start" VerticalOptions="Center" />
    </StackLayout>
      </ControlTemplate>
    

    在同一个项目中

    public class CustomView: ContentView
    {
       public CustomView()
       {
          ControlTemplate = (ControlTemplate)Application.Current.Resources.FirstOrDefault(x => x.Key == "MyControlTemplate").Value;
       }
    }
    

    然后您可以在任何 xaml 页面中使用 CustomView 控件/视图。不要忘记在消费 xaml 页面中添加 CustomView 的 xaml 命名空间。

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 1970-01-01
      • 2017-03-04
      • 2023-02-14
      • 1970-01-01
      • 2016-10-18
      • 1970-01-01
      • 2023-04-07
      • 2018-05-24
      相关资源
      最近更新 更多