【发布时间】: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