【问题标题】:Xaml StaticResource binding style crash using FontSize使用 FontSize 的 Xaml StaticResource 绑定样式崩溃
【发布时间】:2017-12-14 23:55:19
【问题描述】:

尝试初始化此内容页面时,我的代码崩溃了。除静态资源外,一切正常,我尝试取消注释其余部分,这只是静态资源的问题。只有注释掉的代码不起作用。

我不使用 OnPlatform 要求

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 x:Class="ASFT.IssueManager.LoginPage" Padding="10">
        <ContentPage.Resources>
            <ResourceDictionary>
          <x:String x:Key="Labelfont">Medium</x:String>
          <x:String x:Key="Titlefont">Large</x:String>
        </ResourceDictionary>
      </ContentPage.Resources>
      <StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="FillAndExpand" Spacing="10" >
        <StackLayout Orientation="Vertical" VerticalOptions="Start" HorizontalOptions="Center" Spacing="10" WidthRequest="350">
          <Label Text="Login" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Titlefont}"/>
          <BoxView HeightRequest="5" Color="Gray"/>
          <!--<Label Text="Host" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
          <Entry Placeholder="Host/URL" Text="{Binding Host}" />
          <Label Text="UserName" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
          <Entry Placeholder="User name / Account" Text="{Binding Username}" />
          <Label Text="Password" VerticalOptions="Center" HorizontalOptions="Start" FontSize="{StaticResource Labelfont}"/>
          <Entry Placeholder="Password" IsPassword="true" Text="{Binding Password}" />
          <BoxView HeightRequest="5" Color="Gray"/>-->
          <Button x:Name="btnLogin" Text="Login" HorizontalOptions="FillAndExpand" Clicked="OnButtonLogin" WidthRequest="100"/>
        </StackLayout>
      </StackLayout>
    </ContentPage>

【问题讨论】:

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

您可以使用Style 通过NamedSize 枚举设置字体大小。

更改 ResourceDictionary 以使用样式:

<ResourceDictionary>
    <Style x:Key="Labelfont" TargetType="Label">
        <Setter Property="FontSize" Value="Small" />
    </Style>
    <Style x:Key="Titlefont" TargetType="Label">
        <Setter Property="FontSize" Value="Large" />
    </Style>
</ResourceDictionary>

更改控件以引用样式:

<StackLayout Orientation="Vertical" VerticalOptions="Center" HorizontalOptions="FillAndExpand" Spacing="10" >
    <StackLayout Orientation="Vertical" VerticalOptions="Start" HorizontalOptions="Center" Spacing="10" WidthRequest="350">
          <Label Text="Login" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Titlefont}"/>
          <BoxView HeightRequest="5" Color="Gray"/>
          <Label Text="Host" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Labelfont}"/>
          <Entry Placeholder="Host/URL"  />
          <Label Text="UserName" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Labelfont}"/>
          <Entry Placeholder="User name / Account"  />
          <Label Text="Password" VerticalOptions="Center" HorizontalOptions="Start" Style="{StaticResource Labelfont}"/>
          <Entry Placeholder="Password" IsPassword="true"  />
          <BoxView HeightRequest="5" Color="Gray"/>
          <Button x:Name="btnLogin" Text="Login" HorizontalOptions="FillAndExpand" WidthRequest="100"/>
    </StackLayout>
</StackLayout>

输出:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-14
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2013-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多