2017-02-03 74 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