【发布时间】:2021-01-12 10:26:44
【问题描述】:
我希望 Xamarin.Forms 应用程序中的 Label 元素占据 15% 的高度。所以,我是suggested,我使用转换器。以下是我使用的代码:
我的 .xaml 文件
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converters="clr-namespace:ConverterClasses"
x:Class="App_for_e_Toilets.MainPage">
<ContentPage.Resources>
<ResourceDictionary>
<converters:DeviceProperties x:Key="DeviceProperties" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<Label
BackgroundColor="Green"
HeightRequest="{Binding DeviceHeight, Converter={StaticResource DeviceProperties}}"
HorizontalOptions="FillAndExpand"
HorizontalTextAlignment="Center"
Text="Welcome"
TextColor="White"
FontAttributes="Bold"
VerticalTextAlignment="Center"/>
</StackLayout>
</ContentPage>
我的 .xaml.cs 文件:
using System;
using System.Globalization;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace App_for_e_Toilets
{
public partial class MainPage : ContentPage
{
public MainPage()
{
BindingContext = this;
InitializeComponent();
}
public double DeviceHeight = DeviceDisplay.MainDisplayInfo.Height;
}
}
namespace ConverterClasses
{
public partial class DeviceProperties : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
double retVal = 0.50 * (double)value;
return retVal;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
但是,当我运行应用程序时,似乎没有调用转换器(我从public object Convert处的断点在执行时没有破坏代码这一事实意识到这一点。我做错了什么?
PS:这是我进入 Xamarin.Forms 的第二天,所以请耐心等待我的代码
【问题讨论】:
-
您希望您的标签高度为总显示高度的 50% 吗?
-
不,15%,但那个数字就在那里,所以我知道正在应用高度,因为 15% 非常接近默认高度
-
Label 默认高度的 15% ?
-
@Cfun No,设备高度:
public double DeviceHeight = DeviceDisplay.MainDisplayInfo.Height;
标签: xamarin.forms binding inotifypropertychanged converters ivalueconverter