【问题标题】:How to use the same ViewModel in ASP.NET MVC and XAMARIN如何在 ASP.NET MVC 和 XAMARIN 中使用相同的 ViewModel
【发布时间】:2017-02-19 21:12:16
【问题描述】:

在我的 ASP.NET 应用程序中,我有各种带有注释的 ViewModel。 现在我想在我的 XAMARIN 应用程序中使用这些 ViewModel。 这些注释不打算在 XAMARIN 应用程序中使用。 我知道带和不带注释的 ViewModel 不是相同的 ViewModel ,但字段是相同的。但是如何实现代码的最大共享?

基本上每个视图都应该有自己的视图模型。 然而,在这种情况下,在 Web、Android 和 iOS 上多次使用 ViewModel 会很诱人。 (如果显示的字段在所有平台上都是相同的。)

using System.ComponentModel.DataAnnotations;
namespace ... {
    public class Review : Atom
    {
        /// <summary>
        ///     Name
        /// </summary>
        [Required]
        [Display(Name = "Name")]
        [StringLength(40, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
        [DataType(DataType.Text)]
        public string Autor { get; set; }
    }
}

坦克!

【问题讨论】:

    标签: c# asp.net-mvc xamarin.forms


    【解决方案1】:

    您可以尝试使用conditional compilation

    using System.ComponentModel.DataAnnotations;
    namespace ... {
        public class Review : Atom
        {
            /// <summary>
            ///     Name
            /// </summary>
    #if __MOBILE__
            // Xamarin iOS or Android-specific code
            [Required]
            [Display(Name = "Name")]
            [StringLength(40, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 3)]
            [DataType(DataType.Text)]
    #endif
            public string Autor { get; set; }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-17
      • 1970-01-01
      • 2012-08-01
      • 2016-02-18
      • 2011-02-14
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多