Привязать значение к функции с параметром в xamarin - PullRequest
1 голос
/ 15 января 2020

Возможно ли связать значение, которое возвращает функция? Примерно так:

<Label Text="{Binding function(param)}"></label>
public string function(string param){
 return param;
}

Ответы [ 2 ]

1 голос
/ 16 января 2020

Кроме кода Райана, вы сказали, что значение является фиксированным значением, вы можете передать параметр в Converter.

<ContentPage
x:Class="demo3.simplecontrol.Page10"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:converter="clr-namespace:demo3"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<ContentPage.Resources>
    <converter:Converter1 x:Key="converter1" />
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout>
        <Label
            HorizontalOptions="CenterAndExpand"
            Text="{Binding str,Converter={StaticResource converter1},ConverterParameter=255}"
            VerticalOptions="CenterAndExpand" />
    </StackLayout>
</ContentPage.Content>

 public partial class Page10 : ContentPage, INotifyPropertyChanged
{
    private string _str;
    public string str
    {
        get { return _str; }
        set
        {
            _str = value;
            RaisePropertyChanged("str");
        }
    }
    public Page10()
    {
        InitializeComponent();
        str = "this is test";
        this.BindingContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Converter.cs:

  public class Converter1 : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {

        string str = (string)value;
        string p = (string)parameter;

        return str+ parameter;
    }
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
0 голосов
/ 15 января 2020

Использовать преобразователь:

Создать новый класс для преобразователя:

public class LabelConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToString();
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Добавить ссылку на преобразователь в xaml:

<ContentPage xmlns:converters="clr-namespace:JTJ.Converters">
    <ContentPage.Resources >
        <ResourceDictionary>
            <converters:LabelConverter x:Key="labelConverter"/>
        </ResourceDictionary>
    </ContentPage.Resources>

Привязать значение к преобразователю

<Label Text="{Binding LabelText, Converter={StaticResource labelConverter}}"
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...