【问题标题】:Xamarin Forms binding - intercepting a bound value and changing itXamarin Forms 绑定 - 拦截绑定值并更改它
【发布时间】:2019-11-02 07:50:51
【问题描述】:

我有一个简单的组合自定义控件,它显示设置为绑定ControlText 属性的文本。在下面的示例中,您可以看到单击按钮时控件会更新。

如何更改代码,以便控件显示的标签获取发送给它的任何内容并将其转换为全大写?

所以不是显示...

计数=5

它会显示...

COUNT=5

在这个简单的示例中,可以利用 IValueConverter 来完成此操作,但我希望为我需要实现的更复杂的示例看到不同的实现。我正在寻找一种解决方案,该解决方案拦截后面代码中设置的值,对其进行转换,并将其设置为自定义控件的 ControlText 属性。

SimpleControl.xaml.cs

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class SimpleControl : ContentView
{
    public SimpleControl ()
    {
        InitializeComponent ();
    }

    public static readonly BindableProperty ControlTextProperty = BindableProperty.Create(
                                           propertyName: nameof(ControlText),
                                           returnType: typeof(string),
                                           declaringType: typeof(SimpleControl),
                                           defaultBindingMode: BindingMode.TwoWay,
                                           defaultValue: "Hello World");

    public string ControlText
    {
        get { return (string)base.GetValue(ControlTextProperty); }
        set { base.SetValue(ControlTextProperty, value); }
    }
}

另外,我希望在运行时会命中这个断点,但代码永远不会在它上面停止。我正在从 SimplePageModel 设置属性,所以我觉得奇怪的是这从来没有被击中。有人也可以向我解释一下吗?

SimpleControl.xaml

<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="App7.SimpleControl"
             x:Name="this">
    <ContentView.Content>
        <StackLayout Margin="100">
            <Label Text="{Binding Source={x:Reference this}, Path=ControlText}" />
        </StackLayout>
    </ContentView.Content>
</ContentView>

SimplePage.xaml

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:App7"
             x:Class="App7.SimplePage">
    <ContentPage.Content>
        <StackLayout>
            <local:SimpleControl ControlText="{Binding ControlText}" />

            <Button Text="Update Control"
                Command="{Binding UpdateControl}" />
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

SimplePageModel.cs(利用 FreshMVVM)

public class SimplePageModel : FreshBasePageModel
{
    public SimplePageModel() { }

    private int _index;

    public string ControlText { get; set; }

    public Command UpdateControl
    {
        get
        {
            return new Command((t) =>
            {
                ControlText = $"Count = {++_index}";
            });
        }
    }

    public override void Init(object initData)
    {
        ControlText = $"Count = 0";

        base.Init(initData);
    }
}

【问题讨论】:

  • “我想为一个更复杂的例子看一个不同的实现”——我不清楚为什么值转换器不能满足更复杂的例子?是否要修改存储的值而不仅仅是显示的值?
  • 是的,正确的。我的真实世界控件使用 BindableLayout 创建“选项卡”列表。我想将 List 绑定到控件中的属性,但是当它被设置时,我想将它转换为 List。我不想将 List 暴露给外界,只想让控件来处理它。我在想如果我能看到如何用这个简单的例子来做,那么我就会知道如何做一个更复杂的例子。希望对您有所帮助...
  • 为什么不在setter中做呢?
  • 我实际上尝试创建用户控件绑定到的 _ControlText 属性。在 ControlText 设置器中,我将“值”转换为大写并设置 _ControlText。但它没有用。事实上,我上面显示的断点永远不会被击中。我不确定为什么断点也不起作用,但我只能假设从未实际调用过“set”。非常混乱。
  • 我认为您最好专注于解决该问题,而不是尝试提出一些复杂的解决方法。我看到了您之前的帖子,但是有太多事情无法根据您发布的内容轻松调试。如果您想分享代码,我不介意快速浏览一下。

标签: xamarin xamarin.forms freshmvvm


【解决方案1】:

您也可以为此目的使用触发器,因为不太清楚背景中的想法是什么,我只是建议它可能会有所帮助: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/triggers

【讨论】:

    【解决方案2】:

    直接回答问题:从

    更改您的属性的定义
     public static readonly BindableProperty ControlTextProperty = BindableProperty.Create(
                                               propertyName: nameof(ControlText),
                                               returnType: typeof(string),
                                               declaringType: typeof(SimpleControl),
                                               defaultBindingMode: BindingMode.TwoWay,
                                               defaultValue: "Hello World");
    

     public static readonly BindableProperty ControlTextProperty = BindableProperty.Create(
                                               propertyName: nameof(ControlText),
                                               returnType: typeof(string),
                                               declaringType: typeof(SimpleControl),
                                               defaultBindingMode: BindingMode.TwoWay,
                                               defaultValue: "Hello World", 
    coerceValue: (bindable, value) =>
                {
                    if (value!=null)
                        return ((string) value).ToUpper();
                    return value;
                });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多