C# 使用Chakra克服Javascript投影类的1个接口限制?

C# 使用Chakra克服Javascript投影类的1个接口限制?,c#,uwp,chakra,C#,Uwp,Chakra,我对仅将第一个接口投影到Javascript的限制有一个问题。我想知道是否有任何解决方法 我想用Javascript访问大约20个类。这20个类继承自一个BaseModel,它包含所有20个类都使用的重要代码。下面是一个简单的例子: public class Category : BaseModel { public string Title {get;set;} public string Description {get;set;} } public class Item

我对仅将第一个接口投影到Javascript的限制有一个问题。我想知道是否有任何解决方法

我想用Javascript访问大约20个类。这20个类继承自一个BaseModel,它包含所有20个类都使用的重要代码。下面是一个简单的例子:

public class Category : BaseModel
{
    public string Title {get;set;}
    public string Description {get;set;}
}

public class Item : BaseModel
{
    public string Title {get;set;}
    public string Price {get;set;}
}

public class BaseModel : INotifyPropertyChanged, IParentableViewModel
{
    //Some Important Methods that are shared between around 20 Models
}
如果我想用Javascript访问它,我必须为所有20个类创建BaseModel。这是因为Javascript只能看到第一个接口的属性/方法。那么我的代码就变成了这样:

public class CategoryBaseModel : ICategoryModel, INotifyPropertyChanged, IParentableViewModel
{
    //Important Methods gets duplicated between CategoryBaseModel and ItemBaseModel making it harder to maintain
}

public class ItemBaseModel : IItemModel, INotifyPropertyChanged, IParentableViewModel
{
    //Important Methods gets duplicated between CategoryBaseModel and ItemBaseModel making it harder to maintain
}
这变得很难维护,特别是因为我有大约20种方法

我正在寻找是否可以覆盖Javascript的setter,但不确定如何做。这个想法是:

//I want to override something like this. 
//whenever I call category.someProperty = someValue; in Javascript, it goes through this.
//When it goes through this, I can then call the SetProperty method for unknown properties in the interface
public bool JavaScriptPropertySet(object, propertyName, Value)
{
    if(object.hasProperty(propertyName))
    {
        //Set PropertyName
    }
    else
    {
        object.setProperty(propertyName, Value);
    }
}
然后我可以将我的类定义得更简单,如下所示:

public interface IBaseViewModel
{
    string Title {get;set;}
    void SetProperty(propertyName, Value);
}

public class BaseModel : IBaseViewModel, INotifyPropertyChanged, IParentableViewModel
{
    //important methods

    public void SetProperty(propertyName, Value)
    {
        SetPropertyInternal(propertyName, Value);
    }

    public virtual void SetPropertyInternal(propertyName, Value)
    {
    }
}

public class Category : BaseModel
{
    public override void SetPropertyInternal(propertyName, Value)
    {
        if(propertyName == 'description')
            this.description = Value;
    }
}
有这样的方法吗


谢谢

您是否正在使用WinJS开发UWP应用程序?或者在UWP应用程序中托管JavaScript?在UWP应用程序中托管JavaScript。具体来说,我们正在使用脉轮桥。您是否正在使用WinJS开发UWP应用程序?或者在UWP应用程序中托管JavaScript?在UWP应用程序中托管JavaScript。具体来说,我们正在使用脉轮桥。