2010-08-18 96 views
0

我在我的Themes目錄中有一個在generic.xaml中定義的默認樣式的無形控件。我在構造函數中也有以下內容。WPF在VS設計器中不顯示WPF Lookless控件

static MyControl() 
{ 
    DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl))); 
} 

public MyControl() 
{ 
    //DoSomeWork 
} 

是否還有別的我想設置在WPF的土地?在Silverlight中,我所要做的是:

DefaultStyleKey = typeof(MyControl); 

注意:儘管它在Expression Blend中進行了渲染。

回答

1

該構造函數是靜態的嗎?如果不是,應該是。調用應該存在於靜態構造函數中以正常工作。更改或添加您的構造函數,如下所示:

static MyControl() 
{ 
    DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl))); 
} 
+0

這仍然沒有在VS中工作。仍然適用於Expression Blend。 – cjibo 2010-08-18 15:21:29

+0

另請注意,您不能在靜態構造函數上使用訪問修飾符。 – cjibo 2010-08-18 15:29:17

+0

我應該注意到,有時VS中的設計師很愚蠢。 2k10設計師比2k8更好。 – 2010-08-18 16:03:40

0

是你的VS設計器崩潰還是它只顯示一個簡單的邊框外觀。

我會建議。你有一個獨立的控制風格,重量相對較輕。通過檢查IsInDesignTime來從控件的構造函數中應用該樣式。

如果您的設計器崩潰或顯示一些錯誤。然後,你應該嘗試設計時間調試​​。

此外,在少數情況下。如果應用程序類型是「.NetFramework 4 Client Profile」[默認情況下,VS2010中的wpf應用程序類型],則可能會出現類似這樣的有線事件。

HTH

0

你這個屬性添加到您的裝配:

[assembly: ThemeInfo(
    ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located 
    //(used if a resource is not found in the page, 
    // or application resource dictionaries) 
    ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located 
    //(used if a resource is not found in the page, 
    // app, or any theme specific resource dictionaries))] 

這通常放進AssemblyInfo.cs文件。它告訴WPF在哪裏尋找你的generic.xaml文件。