我有一個WPF解決方案,此解決方案由3個項目組成: 1個項目,其中有幾個WPF用戶控件,內部爲 2 - 在 的3-A項目中有幾個WPF用戶控件的另一個項目,該項目有2個以上WPF項目的資源。名稱'foo'在名稱空間'http:// foo ....'中不存在
如您所知,如果您有像您這樣的常見設置 -Using Same FontFamily。 - 使用同一字號 - 使用相同的fontWeight設置 - 使用您的所有用戶控件等相同BackroundBrush。你需要在你聲明此制定者像下面所有的用戶控件標籤:
<UserControl ....
FontFamily="{DynamicResource MyFontFamily}"
FontSize="{DynamicResource MyFontSize}"
FontWeight="{DynamicResource MyFontWeight}"
Background="{DynamicResource MyAppBgBrush2}"
Width="250" d:DesignHeight="350">
<Grid/>......
,但我不想寫在我所有的用戶控件中都有相同的setter。出於這個原因,我決定把這個屬性設置移到一個新的c#文件中,並在資源項目中找到它。
using System.Windows.Controls;
namespace Resources
{
public class PageBase : UserControl
{
public PageBase()
{
SetResourceReference(FontFamilyProperty, "MyFontFamily");
SetResourceReference(FontSizeProperty, "MyFontSize");
SetResourceReference(FontWeightProperty, "MyFontWeight");
SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
}
}
}
所以,在我的資源項目,我adited AssemlyInfo.cs文件是這樣的:
[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]
這個編輯給我申報/創建用戶控制象下面這樣的能力:
<internalresources:PageBase
xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
<Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>
從現在開始,我不必創建一個用戶控件視圖,其標籤以 <UserControl....
開頭,我可以從<internalresources:PageBase
開始......
我的問題是,VisualStudio 2010可以顯示我設計我所有的用戶控件bu Expression Blend不能。有趣的是無論是在VS和Blend的是,我的項目編譯沒有任何錯誤但是當我嘗試在混合打開我的觀點,它說:
-The命名空間「PageBase」命名空間中不存在「http://schemas.sat.com/winfx/2010/xaml/internalresources」
PS:引用被正確地添加到我的項目中,我的項目適合用混合打開。
謝謝您的答案。其實我在使用樣式或任何其他自定義的東西,如datatemplates,itemtemplates在我的資源。我的意思是我正在爲我的控件執行相同的資源機制。我的問題是,當你想在你的項目中添加一個WPF用戶控件時,它會創建一個cs和xaml文件。有什麼辦法可以將樣式應用於您的UserControl,如控件和定義,資源 –