2017-02-03 38 views
0
public class ThemeProperty 
    { 
     public Color FColor { get; set; } = Color.White; 
     public Color BColor { get; set; } = Color.Black; 
    } 

    [TypeConverter(typeof(ExpandableObjectConverter))] 
    public ThemeProperty Theme { get; set; } = new ThemeProperty(); 

    // Use. 
    public void Test() 
    { 
     Theme.BColor = Color.Gray; 
     Theme.FColor = Color.Black; 
     Theme = true; /*I wanted to make the feature active or passive, but 
     I could not figure out how to define a property class for this line.*/ 
    } 

嗨,我创建了一个名为Theme的可扩展属性。虽然我有两个功能,但如果我处于主动或被动状态,我想使用它们,如果我处于活动状态,我想使用它们。我可以创建和控制此功能,但它不灵活。我想像上面那样定义这个特性,但是我不知道如何去做。非常感谢您的帮助。C#展开式属性

enter image description here

我想在红线上添加真假值。激活或停用功能。

回答

0

你可以尝试财产以后这样的:

public class ThemeProperty 
{ 
    public Color FColor { get; set; } 
    public Color BColor { get; set; } 
    public bool ActivePassive { get; set; } 

    public void ThemeProperty(bool state) 
    { 
     ActivePassive = state; 
     FColor = Color.White; 
     BColor = Color.Black; 
    } 
} 

和使用构造提出的是主动被动通过真/假。希望这可以帮助。

+0

我试图用这种方式解释我还没有灵活。谢谢你的回复,我会组织这个问题。 – Emre

0

在“ThemeProperty”类中定义一个属性。

public class ThemeProperty 
{   
    public Color FColor { get; set; } = Color.White; 
    public Color BColor { get; set; } = Color.Black; 
    public bool Active { get; set; } = true; 
} 

// Use. 
[TypeConverter(typeof(ExpandableObjectConverter))] 
public ThemeProperty Theme { get; set; } = new ThemeProperty(); 


public void Test() 
{ 
    Theme.BColor = Color.Gray; 
    Theme.FColor = Color.Black; 
    Theme.Active = true; 
} 

或者,如果需要“活动”,您可能需要将活动布尔值传递给类的构造函数。另外,作为观察,调用类“Property”可能不是一个好习惯。任何可以实例化的类都可以用作属性的类型。以下是您的初始版本的替代品。

public class Theme 
{   
    public Theme(bool active) 
    { 
     Active = active; 
    } 

    public Color FColor { get; set; } = Color.White; 
    public Color BColor { get; set; } = Color.Black; 
    public bool Active { get; set; } 
} 

// Use. 
[TypeConverter(typeof(ExpandableObjectConverter))] 
public Theme theme { get; set; } 


public void Test() 
{ 
    theme = new Theme(true) { BColor = Color.Gray, FColor = Color.Black }; 
} 
+0

我试图用这种方式解释我还没有灵活。谢谢你的回复,我会组织这个问题。 – Emre