【问题标题】:Xamarin.Forms not Calling the ConverterXamarin.Forms 不调用转换器
【发布时间】: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


【解决方案1】:

您必须从实现InotifyPropertyChanged 开始,并在 ViewModel 中使用 backfield getter 和 setter 创建绑定属性:

1- 创建一个辅助类,它将作为您的 ViewModel 的基础,并实现触发InotifyPropertyChanged 事件的方法:

BaseViewModel.cs

public abstract class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = null)
    {
        if (!Equals(field, value))
            OnPropertyChanged(name);
    }

    protected void OnPropertyChanged([CallerMemberName] string name = null) =>
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));

}

2- 为继承自 BaseViewModel 的 ViewModel 创建一个 clss:

MainPageViewModel.cs

public class MainPageViewModel : BaseViewModel
{
private double labelheight;
public double LabelHeight
   {
     get => labelheight;
     set => SetProperty(ref labelheight, value);
   }
}

LabelHeight 将存储标签的高度。

由于您想要设备高度的百分比,您可以将DeviceDisplay.MainDisplayInfo.Height 移动到您的转换器:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    return 0.15 * DeviceDisplay.MainDisplayInfo.Height;
}
HeightRequest="{Binding LabelHeight, Converter={StaticResource DeviceProperties}}"

3- 最后将您的 BindingContext 设置为 ViewModel:

public MainPage()
{
    BindingContext = new MainPageViewModel();
    InitializeComponent();
}

Bindings 与 properties 而非变量一起使用,还可以在 ui 和代码背后(逻辑)之间进行交互,更改属性值需要实现 Inotifypropertychanged 接口。

相关链接:

MVVM Pattern

How to implement INotifyPropertyChanged in Xamarin.Forms

Inotifypropertychanged

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2022-01-24
    • 2012-03-10
    相关资源
    最近更新 更多