2017-02-19 75 views
0

在我的ASP.NET應用程序中,我有各種具有註釋的ViewModel。 現在我想在我的XAMARIN應用程序中使用這些ViewModels。 註釋並不打算在XAMARIN應用程序中使用。 我知道有和沒有註釋的ViewModels不是相同的ViewModels ,但這些字段是相同的。但是,如何最大限度地分享代碼呢?如何在ASP.NET MVC和XAMARIN中使用相同的ViewModel

基本上每個視圖都應該有自己的ViewModel。 但是,在這種情況下,在Web,Android和iOS上多次使用ViewModels會很誘人。 (如果在所有平臺上呈現的字段是相同的)。

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; } 
    } 
} 

坦克!

回答

0

你可以嘗試使用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; } 
    } 
} 
相關問題