【问题标题】:Bind TextChangedEvent to ListView object将 TextChangedEvent 绑定到 ListView 对象
【发布时间】:2018-04-15 06:54:48
【问题描述】:

我正在开发一个 Xamarin.Forms PCL 项目,其中有一个消息列表,并且每条消息都有一个条目来为其输入数据。我想在条目下添加一个标签,该标签将从 50 开始倒数剩余的字符。例如,如果他们输入“test”,它将执行 50 - 文本的长度。

我尝试跟踪使用此代码更改的文本,但它一直导致无效转换错误,所以我没有进一步了解

private void ReplyTextChanged(object sender, TextChangedEventArgs e)
    {
        var message = sender as MessageObject;
    }

XAML 是

<StackLayout>
        <ListView  x:Name="MessageView">
            <ListView .ItemTemplate>
                <DataTemplate>
                    <local:PostViewCell>
                        <StackLayout>
                             <StackLayout x:Name="MessageLayout" BackgroundColor="Transparent" Padding="10, 10, 15, 10">
                                <StackLayout Orientation="Vertical" IsVisible="{Binding ShowReplyField}" Spacing="0">
                                      <Entry Text="{Binding ReplyText}" Placeholder="Reply..." TextChanged="ReplyTextChanged" HorizontalOptions="FillAndExpand" Margin="0, 0, 0, 5"/>
                                      <Label Text="{Binding TextLeft}"/>
                                </StackLayout>
                            </StackLayout>
                        </StackLayout>
                    </local:PostViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

我的消息对象是

public class MessageObject : INotifyPropertyChanged
{
    private string idValue = "";
    private string bodyValue = "";
    private string replyTextLeftValue = "";
    . . .

    public event PropertyChangedEventHandler PropertyChanged;

    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    private MessageObject(string id, string body, . . .)
    {
        idValue = id;
        bodyValue = body;
        replyTextLeftValue = "50 Characters";
        . . . 
    }

    public static MessageObject CreateMessage(string id, string body, . . .)
    {
        return new MessageObject(string id, string body, . . .);
    }

    public string ID
    {
        get
        {
            return this.idValue;
        }
    }

    public string Body
    {
        get
        {
            return this.bodyValue;
        }

        set
        {
            if (value != this.bodyValue)
            {
                this.bodyValue = value;
                NotifyPropertyChanged();
            }
        }
    }

    public string TextLeft
    {
        get
        {
            return this.replyTextLeftValue;
        }

        set
        {
            if(value != this.replyTextLeftValue)
            {
                this.replyTextLeftValue = value;
                NotifyPropertyChanged();
            }
        }
    }

【问题讨论】:

  • 您可以创建转换器来计算remaring符号。标签的绑定上下文也必须是条目的引用。

标签: c# xaml xamarin xamarin.forms


【解决方案1】:

在 TextChanged 事件处理程序中,发送者将是引发事件的条目,而不是视图模型对象,这就是您看到该错误的原因。您可以通过 BindingContext 引用模型。

假设 MessageObject.TextLeft 设置为触发属性更改事件(您没有显示整个 MessageObject 类),那么 TextChanged 事件处理程序将如下所示:

private void ReplyTextChanged(object sender, TextChangedEventArgs e)
    {
        var message = BindingContext as MessageObject;
        message.TextLeft = 50 - e.NewTextValue.Length;
    }

编辑

总结后续的 cmets,一种替代方法是完全避免使用 TextChanged 回调,而是在 MessageObject 上实现 ReplyText 和 TextLeft 属性,如下所示:

public string TextLeft 
{ 
  get 
  { 
    return this.replyTextLeftValue; 
  } 
} 

public string ReplyText 
{ 
  get 
  { 
    return this.replyTextValue; 
  } 

  set 
  { 
    if(value != this.replyTextValue) 
    { 
      this.replyTextValue = value; 
      this.replyTextLeftValue = (50 - value.Length).ToString(); 
      NotifyPropertyChanged(); 
      NotifyPropertyChanged("TextLeft"); 
    } 
  } 
}

【讨论】:

  • 这在一定程度上起作用,但是当我尝试(用于测试)更改正文时,它说 NInterpret.NInterpretException: 在正文的 set 方法上我猜是因为没有CommandParameter字段?你知道解决这个问题的方法吗?
  • 你能添加整个 MessageObject 的实现吗?
  • ItemTemplate 绑定中使用的ReplyText 和TextLeft 属性是什么类型?它看起来不像是 MessageObject...
  • 糟糕,我编辑了它。但无论我在 TextChanged 方法中进行什么更改,它都会产生错误
  • ReplyText 是如何定义的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 2022-06-11
  • 2017-04-07
  • 1970-01-01
  • 2015-02-01
  • 1970-01-01
相关资源
最近更新 更多