Domanda

I have an WPF solution and this solution consist of 3 project: 1-A project that has several WPF user control inside 2-Another project that has several WPF user control inside 3-A project which has Resources for 2 WPF projects above.

As you know, if you have common settings for you views like that -Using Same FontFamily. -Using same FontSize -Using same FontWeight -Using same BackroundBrush for all your User Controls etc.. You need to declare this setters in you all usercontrol tags like below:

<UserControl ....
    FontFamily="{DynamicResource MyFontFamily}" 
    FontSize="{DynamicResource MyFontSize}" 
    FontWeight="{DynamicResource MyFontWeight}" 
    Background="{DynamicResource MyAppBgBrush2}"
    Width="250" d:DesignHeight="350">
    <Grid/>......

But I dont want to write same setters in all my UserControls. For thi reason, I decided to move this property setting in to a new c# file and locate it in Resource Project.

using System.Windows.Controls;

namespace Resources
{
    public class PageBase : UserControl 
    {
        public PageBase()
        {
            SetResourceReference(FontFamilyProperty, "MyFontFamily");
            SetResourceReference(FontSizeProperty, "MyFontSize");
            SetResourceReference(FontWeightProperty, "MyFontWeight");
            SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
        }
    }
}

So, In my Resource project, I adited AssemlyInfo.cs file like this:

[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]

This edit gives me ability to declare/create a user control like below:

<internalresources:PageBase
            xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
      <Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>

From now, I do not have to create a usercontrol view which its tags start with <UserControl...., I can start with <internalresources:PageBase......

My Question is that, VisualStudio 2010 can show me Design of all my user control bu Expression blend can not. Interesting part is that both in VS and Blend, my project compiling without any error But when I try to open my views in blend it says:

-The namespace 'PageBase' does not exist in namespace "http://schemas.sat.com/winfx/2010/xaml/internalresources"

P.S: References are added properly to my Project and My project was suitable to open with blend.

È stato utile?

Soluzione 2

I have found the problem. The problem is just about Expression blend. If your project setting does not have any PropertyGroup for Debug|AnyCPU you will get this problem. You should add this propertygroup to your csproj file like below via text editor :

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>x86</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>

Altri suggerimenti

Inheriting your own UserControl makes sense when you are adding new abilities or properties to it. In your case you just want to override the design.

You can achieve this very simply by creating a Resource XAML (BasePageResources.xaml) and define your UI properties there with Setter tags.

<Style TargetType="Button">
    <Setter Property="MinWidth" Value="75"/>
    <Setter Property="MinHeight" Value="23"/>
    <Setter Property="Margin" Value="11,11,0,0"/>
</Style>

You can dump all your setters in a single file, give your setters keys, just like CSS classes. Then, in your App.xaml, you can include these files to make them Application-wide accessable.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="BasePageResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

All your buttons in the application should now apply your styles. It's much better than inheriting, and WPF binding feels more natural than your work-around. Blend should cause you less problems if you design the way it expects you to.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top