【问题标题】:Xamarin MVVM Custom Model Properties?Xamarin MVVM 自定义模型属性?
【发布时间】:2017-08-18 01:13:47
【问题描述】:

假设我有一个只读模型...

[DataContract]
public partial class Person {
    [DataMember]
    public virtual string LastName { get; set; }

    [DataMember]
    public virtual string FirstName { get; set; }
}

一个视图...

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    x:Class="TheApp.APage"
    BackgroundColor="#03264A"
    Padding="0,0,0,0">
    <ContentPage.Content>
        <StackLayout
            BackgroundColor="Transparent">
            <ListView 
                ItemsSource="{Binding PersonSearchCollection}">
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <Label
                                Text="{Binding FirstName}" />
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

还有一个 ViewModel(基本思想)

namespace TheApp.ViewModels {
    public class APagePageViewModel : Helpers.BindableBase {

        private ObservableCollection<Person> _PersonSearchCollection = new RangeObservableCollection<Person>();

        public ObservableCollection<Person> PersonSearchCollection {
            get { return _PersonSearchCollection; }
            set { SetProperty(ref _PersonSearchCollection, value); }
        }
    }
}

我的 ListView 绑定到一个 ObservableCollection 类型:Person。当用户键入要搜索的名称时,它会从 ServiceStack 调用中填充。

目前 DataTemplate 中的 Label 绑定到 FirstName,但我希望它是一个新属性:FullName (Person.FirstName+" "+Person.LastName)。

如何将属性添加到无法编辑的模型中?我是否需要一个单独的 VM 用于模型本身并将 ObservableCollection 更改为该类型?任何例子都会很棒!

对不起,如果这是一个基本问题,我对 Xamarin 还很陌生。

谢谢!

【问题讨论】:

  • 更改您的数据模板

标签: c# xamarin mvvm


【解决方案1】:

更改您的视图单元格

<ViewCell>
 <StackLayout Orientation="Horizontal">
  <Label Text="{Binding FirstName}"
  <Label Text=" "
  <Label Text="{Binding LastName}"
 </StackLayout>
</ViewCell>

其他方式

  1. 定义一个自定义对象

    public class CustomPerson
        {
            public CustomPerson(Person P)
            {
                FirstName = P.FirstName;
                LastName = P.LastName;
            }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public string FullName
            {
                get { return string.Format("{0} {1}", FirstName, LastName);}
            }
    
        }
    

(2) 用Getter定义一个集合

public IEnumerable<CustomPerson> CustomCollection
 {
  get { return _personSearchCollection.Select(p => new CustomPerson(p)); }
 } 

当您更新个人搜索集合时,会引发自定义集合的属性更改。

  1. 最终与 CustomCollection 绑定

【讨论】:

  • 感谢您的回复。我可以看到这是如何工作的,但这似乎不是最好的方法?我觉得只为空格、字符等制作标签是矫枉过正?例如,如果我想制作: John Smith (19) 我需要 7 个标签? 3 代表名字/姓氏/年龄,4 代表 2 个空格和 2 个括号。
  • 我是 xamarin 的新手,但我可以让它在 wpf xaml 单行中工作。不知道 Xamarin 中是否有运行
  • 这里是关键。我不知道你会怎么用它developer.xamarin.com/guides/xamarin-forms/user-interface/text/…
  • 谢谢你的帮助,我看看能不能把这些信息拼凑起来
  • 测试其中的一些。如果我让它工作,我会接受它作为答案。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-13
  • 1970-01-01
  • 1970-01-01
  • 2021-06-04
  • 1970-01-01
相关资源
最近更新 更多