VS 2017 XAML Дизайнер пуст при переопределении MahApps.Metro Style - PullRequest
0 голосов
/ 11 декабря 2018

Я новичок в программировании и прошу прощения за мой английский.

У меня проблема с переопределением MahApps.Metro Style.
При добавлении стиля в элемент управления Dialog в конструкторе XAML ничего не отображается (см. Снимки экрана)ссылки ниже).
Это похоже на диалог с пустым содержимым, без текста и элементов управления.

Перед добавлением стиля в CustomDialog

Dialog1.xaml

<Dialog:CustomDialog x:Class="DesignerTestWithStyle.Dialog1"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                   xmlns:local="clr-namespace:DesignerTestWithStyle"
                   xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                   xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                   mc:Ignorable="d" 
                   d:DesignHeight="150" d:DesignWidth="250"

                   >
<Grid>
    <StackPanel>
        <Button Content="Test"/>
        <Label Content="Test text"/>
        <TextBox Text="I don't see this text"/>
    </StackPanel>
</Grid>


Works fine

После добавления стиля в CustomDialog

Dialog1.xaml

    <Dialog:CustomDialog x:Class="DesignerTestWithStyle.Dialog1"
                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                   xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                   xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                   xmlns:local="clr-namespace:DesignerTestWithStyle"
                   xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls"
                   xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                   mc:Ignorable="d" 
                   d:DesignHeight="150" d:DesignWidth="250"
                   Style="{StaticResource Dialog1Style}"
                   >
<Grid>
    <StackPanel>
        <Button Content="Test"/>
        <Label Content="Test text"/>
        <TextBox Text="I don't see this text"/>
    </StackPanel>
</Grid>


XAML Designer is blank, without controls

Где Dialog1Style - это файл ResourceDictionary, объявленный в App.xaml, создать из советов из этого запроса:
Как изменить ширину шаблона содержимого диалога MahApps.Metro?
Это похоже напроблема с переопределением шаблона в моем собственном стиле.

Обновление Visual Studio не помогает.
Windows 10 обновлена.
Моя Visual Studio 2017 является выпуском Community Edition 15.9.3, проблема такжепроисходит в Профессионале и ПредприятииРедакция.

Редактировать:
Мой файл app.xaml был пустым после создания нового проекта и добавления пакета mahapps.metro.
На самом деле app.xaml:

<Application x:Class="DesignerTestWithStyle.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:local="clr-namespace:DesignerTestWithStyle"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/Styles/Dialog1Style.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

и Dialog1Style.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:DesignerTestWithStyle.Styles"
                xmlns:Dialog="clr-namespace:MahApps.Metro.Controls.Dialogs;assembly=MahApps.Metro"
                >
<Style TargetType="{x:Type Dialog:BaseMetroDialog}"
    x:Key="Dialog1Style"
    BasedOn="{StaticResource {x:Type Dialog:BaseMetroDialog}}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Dialog:BaseMetroDialog}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="DialogShownStoryboard">
                        <DoubleAnimation AccelerationRatio=".9"
                                        BeginTime="0:0:0"
                                        Duration="0:0:0.2"
                                        Storyboard.TargetProperty="Opacity"
                                        To="1" />
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid Background="{TemplateBinding Background}">
                    <Border FocusVisualStyle="{x:Null}"
                        Focusable="False">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ContentPresenter Grid.Row="0"
                                            Content="{TemplateBinding DialogTop}" />
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10*" />
                                    <ColumnDefinition Width="80*" />
                                    <ColumnDefinition Width="10*" />
                                </Grid.ColumnDefinitions>
                                <!--  Content area  -->
                                <Grid Grid.Column="1"
                                    Margin="0 10 0 0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0"
                                            FontSize="{DynamicResource DialogTitleFontSize}"
                                            Foreground="{TemplateBinding Foreground}"
                                            Text="{TemplateBinding Title}"
                                            TextWrapping="Wrap" />
                                    <ContentPresenter Grid.Row="1"
                                                    Content="{TemplateBinding Content}" />
                                </Grid>
                            </Grid>
                            <ContentPresenter Grid.Row="2"
                                            Content="{TemplateBinding DialogBottom}" />
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard Storyboard="{StaticResource DialogShownStoryboard}" />
                        </EventTrigger.Actions>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 Ответ

0 голосов
/ 13 декабря 2018

Хорошо, проблема в стиле: вы устанавливаете начальную непрозрачность от "0" до Storyoard, это нормально, потому что ваша раскадровка установит непрозрачность на "1" во время выполнения, но если вы все еще хотите показать, скопируйте мой стиль, который установил начальнуюнепрозрачность до "0", но btoryboard начнет его с "0", поэтому никаких различий не будет заметно.

<Style TargetType="{x:Type Dialog:BaseMetroDialog}" x:Key="Dialog1Style" BasedOn="{StaticResource {x:Type Dialog:BaseMetroDialog}}">
    <Setter Property="Opacity" Value="1" /> <!-- Problem is here -->
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Dialog:BaseMetroDialog}">
                <ControlTemplate.Resources>
                    <Storyboard x:Key="DialogShownStoryboard">
                        <DoubleAnimation AccelerationRatio=".9" BeginTime="0:0:0" Duration="0:0:0.5" Storyboard.TargetProperty="Opacity" From="0" To="1" />
                    </Storyboard>
                </ControlTemplate.Resources>
                <Grid Background="{TemplateBinding Background}">
                    <Border FocusVisualStyle="{x:Null}" Focusable="False">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <ContentPresenter Grid.Row="0" Content="{TemplateBinding DialogTop}" />
                            <Grid Grid.Row="1">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="10*" />
                                    <ColumnDefinition Width="80*" />
                                    <ColumnDefinition Width="10*" />
                                </Grid.ColumnDefinitions>
                                <!--  Content area  -->
                                <Grid Grid.Column="1" Margin="0 10 0 0">
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="*" />
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0"
                                        FontSize="{DynamicResource DialogTitleFontSize}" Foreground="{TemplateBinding Foreground}" Text="{TemplateBinding Title}" TextWrapping="Wrap" />
                                    <ContentPresenter Grid.Row="1" Content="{TemplateBinding Content}" />
                                </Grid>
                            </Grid>
                            <ContentPresenter Grid.Row="2" Content="{TemplateBinding DialogBottom}" />
                        </Grid>
                    </Border>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="Loaded">
                        <EventTrigger.Actions>
                            <BeginStoryboard Storyboard="{StaticResource DialogShownStoryboard}" />
                        </EventTrigger.Actions>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Согласно документации MahApps, вы должны добавить эти ресурсы в app.xaml.cs, чтобы вы могли добавить этострока также перед вашим стилем, для загрузки некоторых значений по умолчанию.

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
            <!-- Accent and AppTheme setting  -->
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />

            <!-- Your Style -->
            <ResourceDictionary Source="Dialog1Style.xaml" />
        </ResourceDictionary.MergedDictionaries>

    </ResourceDictionary>
</Application.Resources>
...