【问题标题】:VS2015 does not render custom window control in design view (works in VS2013)VS2015 不在设计视图中呈现自定义窗口控件(适用于 VS2013)
【发布时间】:2015-07-22 19:39:56
【问题描述】:

我有一个使用自定义窗口控件的程序。在 VS2013 中,窗口在设计视图中呈现良好,在 VS2015 中打开的解决方案完全相同。在运行时编译并正常工作。

相关代码片段在这里:

<control:CustomWindow x:Class="CategoryView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:control="clr-namespace:ClassAccounts;assembly=ClassAccounts"
    xmlns:l="clr-namespace:WpfAccounts"
    Title="Category" Width="470" Height="485" WindowStartupLocation="CenterScreen" OKVisibility="True">

<Window.Resources>

    <CollectionViewSource Source="{Binding Categories}" x:Key="Categories"/>
    <CollectionViewSource Source="{Binding Headings}" x:Key="Headings"/>

    <DataTemplate x:Key="CategoryTemplate">
        <TextBlock Text="{Binding comment}" Margin="0" Foreground="{Binding forecolor}"/>
    </DataTemplate>
</Window.Resources>

<Grid>
    ...
</Grid>

Application.xaml

<Application x:Class="Application"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfAccounts"
StartupUri="MainWindow.xaml">

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/ClassAccounts;component/Themes/Generic.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <!-- Region SubMenu -->
        <MenuScrollingVisibilityConverter x:Key="MenuScrollingVisibilityConverter"/>

        <!-- Separator -->
        <Style TargetType="{x:Type Separator}" x:Key="SeparatorStyle">
            <Setter Property="Height" Value="1"/>
            <Setter Property="Background" Value="{StaticResource CustomDarkBlue}"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Separator}">
                        <Rectangle Height="{TemplateBinding Height}" Fill="{StaticResource CustomWhite}"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    ...

        <!-- End Region -->
    </ResourceDictionary>
</Application.Resources>

AssemblyInfo.vb

Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices
Imports System.Globalization
Imports System.Resources
Imports System.Windows

' General Information about an assembly is controlled through the following 
' set of attributes. Change these attribute values to modify the information
' associated with an assembly.

' Review the values of the assembly attributes

<Assembly: AssemblyTitle("Accounts")> 
<Assembly: AssemblyDescription("Account Program")> 
<Assembly: AssemblyCompany("")> 
<Assembly: AssemblyProduct("Accounts")> 
<Assembly: AssemblyCopyRight("Copyright ©  2013-4 Andrew Powell")> 
<Assembly: AssemblyTrademark("")> 
<Assembly: ComVisible(False)>

'In order to begin building localizable applications, set 
'<UICulture>CultureYouAreCodingWith</UICulture> in your .vbproj file
'inside a <PropertyGroup>.  For example, if you are using US english 
'in your source files, set the <UICulture> to "en-US".  Then uncomment the
'NeutralResourceLanguage attribute below.  Update the "en-US" in the line
'below to match the UICulture setting in the project file.

'<Assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)> 


'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
'1st parameter: where theme specific resource dictionaries are located
'(used if a resource is not found in the page, 
' or application resource dictionaries)

'2nd parameter: where the generic resource dictionary is located
'(used if a resource is not found in the page, 
'app, and any theme specific resource dictionaries)
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)> 



'The following GUID is for the ID of the typelib if this project is exposed to COM
<Assembly: Guid("c7920515-ec57-40d4-bccb-8f5b16be77d0")> 

' Version information for an assembly consists of the following four values:
'
'      Major Version
'      Minor Version 
'      Build Number
'      Revision
'
' You can specify all the values or you can default the Build and Revision Numbers 
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")> 

<Assembly: AssemblyVersion("1.0.0.0")> 
<Assembly: AssemblyFileVersion("1.0.0.0")> 

Generic.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ClassAccounts">

<!-- Region Colours -->

<!-- Control colors -->
<SolidColorBrush x:Key="CustomWhite" Color="White"/>
<SolidColorBrush x:Key="CustomDarkBlue" Color="#8DA3C1"/>
<SolidColorBrush x:Key="CustomGray" Color="LightGray"/>
<SolidColorBrush x:Key="CustomDarkHighlight" Color="#5082A4"/>
<!-- Submenu item highlight -->
<SolidColorBrush x:Key="CustomLightHighlight" Color="#3399FF"/>
<!-- TreeviewItem highlight -->
<Color x:Key="CustomLightHighlightC" A="#FF" R="#33" G="#99" B="#FF"/>
<SolidColorBrush x:Key="CustomBackground" Color="#B5CBEF"/>
<Color x:Key="CustomBackgroundC" A="#FF" R="#B5" G="#CB" B="#EF"/>
<SolidColorBrush x:Key="CustomHighlightedControlGlyph" Color="#3C7FB1"/>
<SolidColorBrush x:Key="CustomHighlightedControlBorder" Color="#222"/>
<SolidColorBrush x:Key="CustomPressedControlGlyph" Color="#003366"/>
<SolidColorBrush x:Key="CustomPressedControlBorder" Color="#526C7B"/>
<SolidColorBrush x:Key="CustomPressedControlBackground" Color="#595959"/>
<SolidColorBrush x:Key="CustomGlyph" Color="#444"/>

<!-- Window Colors -->
<SolidColorBrush x:Key="CustomWindowBorder" Color="#395984"/>
<SolidColorBrush x:Key="CustomHeaderLight" Color="#E7EBF7"/>
<Color x:Key="CustomHeaderLightC" A="#FF" R="#E7" G="#EB" B="#F7"/>
<SolidColorBrush x:Key="CustomHeaderDark" Color="#CEE3FF"/>
<Color x:Key="CustomHeaderDarkC" A="#FF" R="#CE" G="#E3" B="#FF"/>
<SolidColorBrush x:Key="CustomControl" Color="Gray"/>

<!-- End Region -->

<!-- Region Window -->

<!-- Window Button style -->
<Style TargetType="{x:Type Button}" x:Key="WindowButtonStyle">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="FontFamily" Value="Webdings"/>
    <Setter Property="FontSize" Value="16"/>
    <Setter Property="Foreground" Value="{StaticResource CustomControl}"/>
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="HorizontalAlignment" Value="Center"/>
    <Setter Property="VerticalAlignment" Value="Top"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ButtonBase}">
                <Border x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Margin="0" Padding="0" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Foreground" Value="LightGray"/>
        </Trigger>
    </Style.Triggers>
</Style>

<!-- Window style -->
<Style TargetType="{x:Type local:CustomWindow}" x:Key="{x:Type local:CustomWindow}">
    <Setter Property="WindowStyle" Value="None"/>
    <Setter Property="ResizeMode" Value="NoResize"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="AllowsTransparency" Value="True"/>
    <Setter Property="ShowInTaskbar" Value="True"/>
    <Setter Property="Icon" Value="\coins.ico"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:CustomWindow}">
                <Border Width="Auto" Height="Auto" Name="windowFrame" BorderBrush="{StaticResource CustomWindowBorder}" BorderThickness="1" CornerRadius="0,20,20,20">
                    <Border.Background>
                        <LinearGradientBrush>
                            <GradientBrush.GradientStops>
                                <GradientStopCollection>
                                    <GradientStop Color="{StaticResource CustomHeaderLightC}" Offset="0.0"></GradientStop>
                                    <GradientStop Color="{StaticResource CustomHeaderDarkC}" Offset="0.25"></GradientStop>
                                </GradientStopCollection>
                            </GradientBrush.GradientStops>
                        </LinearGradientBrush>
                    </Border.Background>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>
                        <Grid Grid.Row="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="100"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" x:Name="tbTitle" Text="{TemplateBinding Title}" FontSize="14" Margin="0" Padding="4"/>
                            <StackPanel Grid.Column="1" Orientation="Horizontal" Margin="0,5,15,0" HorizontalAlignment="Right" VerticalAlignment="Top" Background="Transparent">
                                <Button x:Name="RefreshButton" Content="q" Style="{StaticResource WindowButtonStyle}"/>
                                <Button x:Name="MinimizeButton" Content="0" Style="{StaticResource WindowButtonStyle}"/>
                                <Button x:Name="OKButton" Content="a" Style="{StaticResource WindowButtonStyle}" IsDefault="True"/>
                                <Button x:Name="CloseButton" Content="r" Style="{StaticResource WindowButtonStyle}" IsCancel="True"/>
                            </StackPanel>
                        </Grid>
                        <Grid x:Name="grdMain" Grid.Row="1" Background="{StaticResource CustomBackground}">
                            <AdornerDecorator>
                                <ContentPresenter/>
                            </AdornerDecorator>
                        </Grid>
                        <Grid Grid.Row="2">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Grid.Column="0" Orientation="Horizontal" Margin="15,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
                                <TextBlock x:Name="tbStatusLeft" FontSize="14" Margin="0,0,0,0" Width="150" Padding="2"/>
                                <TextBlock x:Name="tbStatusLeft2" FontSize="14" Margin="0,0,0,0" Width="150" Padding="2"/>
                                <TextBlock x:Name="tbStatusLeft3" FontSize="14" Margin="0,0,0,0" Width="150" Padding="2"/>
                            </StackPanel>
                            <Canvas x:Name="canStatusMiddle" Grid.Column="1" ClipToBounds="True">
                                <TextBlock x:Name="tbStatusMiddle" FontSize="14" Margin="0,0,0,0" Padding="2" TextAlignment="Center"/>
                            </Canvas>
                            <StackPanel Grid.Column="2" Orientation="Horizontal" Margin="0,0,15,0" HorizontalAlignment="Right" VerticalAlignment="Top">
                                <TextBlock x:Name="tbStatusRight3" FontSize="14" Margin="0,0,0,0" Width="150" Padding="2" TextAlignment="Right"/>
                                <TextBlock x:Name="tbStatusRight2" FontSize="14" Margin="0,0,0,0" Width="150" Padding="2" TextAlignment="Right"/>
                                <TextBlock x:Name="tbStatusRight" FontSize="14" Margin="0,0,0,0" Width="150" Padding="2" TextAlignment="Right"/>
                            </StackPanel>
                        </Grid>
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="OKVisibility" Value="False">
                        <Setter TargetName="OKButton" Property="Visibility" Value="Collapsed"/>
                    </Trigger>
                    <Trigger Property="RefreshVisibility" Value="False">
                        <Setter TargetName="RefreshButton" Property="Visibility" Value="Collapsed"/>
                    </Trigger>

                    <Trigger Property="ExtraStatus" Value="False">
                        <Setter TargetName="tbStatusLeft2" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="tbStatusLeft3" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="tbStatusRight2" Property="Visibility" Value="Collapsed"/>
                        <Setter TargetName="tbStatusRight3" Property="Visibility" Value="Collapsed"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<!-- End Region-->

示例项目 - https://www.sendspace.com/file/z4a4tg

感谢任何帮助。 安迪

【问题讨论】:

  • 您的任何 xaml 文件中是否有任何蓝色曲线?一切都在运行时工作?
  • 在运行时工作正常。谢谢(编辑主帖)
  • 如果您在 2015 年打开所有 XAML 文件,没有蓝色曲线? 2015 显示了以前版本没有的错误。我会假设它本身并没有拾取窗口样式,但它确实拾取了所有其他样式,因为您的窗口样式中有一些错误,2013 年不关心,但 2015 年会。
  • 没有蓝色曲线。它必须与 ResourceDictionary 有关 - 因为您可以看到的控件在 Application.xaml 中定义,但自定义窗口本身在 Generic.xaml 中。但它编译和运行完全正常。

标签: wpf vb.net visual-studio-2013 visual-studio-2015


【解决方案1】:

我发现了几件事。我在我发布的示例项目中注意到,实际上它在 VS2013 中无法正确呈现。 Generic.xaml 的关键行是这样的:

<Style TargetType="{x:Type local:CustomWindow}" x:Key="{x:Type local:CustomWindow}">

如果我将其更改为:

<Style TargetType="{x:Type local:CustomWindow}">

它在 VS2013 中渲染得很好(但在 VS2015 中仍然没有)。理论上(来自 MSDN)这不应该有所作为,因为 x:Key 如果未指定是隐式的。

如上面的第一个示例所示,将 TargetType 属性设置为 TextBlock 而不使用 x:Key 分配样式允许您的样式应用于所有 TextBlock 元素。实际发生的是,这样做会隐式地将 x:Key 设置为 {x:Type TextBlock}。这也意味着,如果您给 Style 一个 x:Key 值,而不是 {x:Type TextBlock},则 Style 不会自动应用于所有 TextBlock 元素。相反,您需要将样式显式应用于 TextBlock 元素。

(来自https://msdn.microsoft.com/fr-fr/library/ms745683(v=VS.85).aspx#styling_targettype

在 VS2015 中似乎可以修复它(尽管它有点软糖)正在将其更改为:

<Style TargetType="{x:Type local:CustomWindow}" x:Key="CustomWindow">

添加

<Style="{DynamicResource CustomWindow}">

到主项目中的每个窗口 xaml。我认为我不应该这样做,而且我认为 VS2013 和 VS2015 中可能存在错误,因为前两个中的任何一个都应该有效。

感谢您迄今为止的任何想法并感谢您的意见。 安迪

【讨论】:

  • 您,先生,是我的新英雄。我为此苦苦挣扎了 3 天,将包含 5 个以上基本库的大量代码减少到只有 GenericAppMainWindow xaml 文件(带有一点代码隐藏)。在仍然没有任何运气之后,我偶然发现了您的建议,,它有效!
【解决方案2】:

我正在做与您类似的事情,我的在 VS2015 中工作正常,我在设计器中看到了我的样式窗口。不过我的设置略有不同。

App.Xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="/xxx.yyy;component/Themes/Generic.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

注意不同的路径语法。我之所以提到这一点,是因为我在控件上遇到了相反的问题。出于某种原因,设计者不会在一个窗口中选择使用 /xxx.yyy;component 语法的资源。当我将其切换为打包语法时,设计人员能够看到资源。

就像我说的,我也有一个自定义窗口(框架、客户端、所有内容都被覆盖)。设计师正在展示样式化的版本。

另外,我的 generic.xaml 也是类似的。它只包含其他 xaml 文件:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="/xxx.yyy;component/Themes/xxxWindow.xaml" />

【讨论】:

  • 谢谢。我试过 都没有成功(即编译/运行正常但不显示在设计器中)
  • 嗯...您是否使用样式键?还是全球风格?你在后面的代码中做任何初始化吗?任何 ifdefs 或 InDesignMode 类型检查?据我所知,实际上没有任何方法可以调试设计时问题……您可能需要联系 Microsoft。
  • 嗨。全局风格(我在上面的问题中添加了 Generic.xaml)。 OnApplyTemplate 中有一些代码可以将处理程序添加到窗口上的某些控件,仅此而已。没有 ifdefs 等。我已经向 Microsoft 发布了错误报告。谢谢。
  • 复制到初始帖子的示例项目
  • 我试过你的项目,我看到了同样的问题。我尝试注释掉一堆东西,只是在 app.xaml 中设置窗口背景颜色,但设计师没有选择它。如果我把它放在 window.resources 中,它就会被拾取。所以我会说它是 Vb.net 唯一的问题,因为它在 C# 中工作:)。我的代码是 C#,设计师从任何地方挑选样式。
【解决方案3】:

我在使用 VS 2015 Update 2 时遇到了这个问题。我安装了 Update 3 并解决了这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-01
    • 2014-02-28
    • 2012-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多