【问题标题】:ArgumentException when styling ContextMenu template设置 ContextMenu 模板样式时出现 ArgumentException
【发布时间】:2014-08-02 13:47:15
【问题描述】:

我正在尝试覆盖 Windows Phone 控件工具包ContextMenu 控件的默认模板。当我从 Generix.xaml 复制并粘贴样式(未修改)到 Toolkit source code 并运行时,长按我得到

ArgumentException: 值不在预期范围内。

如果我删除它,那么它工作正常。覆盖 MenuItemSeparator 控件的模板也没有问题。

这是 Toolkit 中定义的默认样式:

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls"
xmlns:primitives="clr-namespace:Microsoft.Phone.Controls.Primitives">

    <Style TargetType="controls:ContextMenu">
        <Setter Property="Padding" Value="0 18 0 28"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="controls:ContextMenu">
                    <StackPanel x:Name="OuterPanel">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="VisibilityStates">
                                <VisualState x:Name="Open">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleX" Storyboard.TargetProperty="ScaleX">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleY" Storyboard.TargetProperty="ScaleY">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:.3" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:.42" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>

                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border1" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border2" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border1Reversed" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border2Reversed" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="OpenReversed">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleX" Storyboard.TargetProperty="ScaleX">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleY" Storyboard.TargetProperty="ScaleY">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.42" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>

                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border1Reversed" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border2Reversed" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border1" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border2" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="OpenLandscape">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleX" Storyboard.TargetProperty="ScaleY">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleY" Storyboard.TargetProperty="ScaleX">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.42" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>

                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border1" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border2" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border1Reversed" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border2Reversed" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="OpenLandscapeReversed">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleX" Storyboard.TargetProperty="ScaleY">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="ScaleY" Storyboard.TargetProperty="ScaleX">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.3" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.42" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>

                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border1Reversed" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border2Reversed" Storyboard.TargetProperty="Opacity" To="1"/>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border1" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Duration="0:0:0.42" Storyboard.TargetName="Border2" Storyboard.TargetProperty="Opacity">
                                            <LinearDoubleKeyFrame KeyTime="0:0:0" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.4099" Value="0"/>
                                            <LinearDoubleKeyFrame KeyTime="0:0:0.41" Value="1"/>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Closed">
                                    <Storyboard>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="ScaleX" Storyboard.TargetProperty="ScaleX" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="ScaleX" Storyboard.TargetProperty="ScaleY" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="ScaleY" Storyboard.TargetProperty="ScaleY" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="ScaleY" Storyboard.TargetProperty="ScaleX" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border1" Storyboard.TargetProperty="Opacity" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border2" Storyboard.TargetProperty="Opacity" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border1Reversed" Storyboard.TargetProperty="Opacity" To="0"/>
                                        <DoubleAnimation Duration="0" Storyboard.TargetName="Border2Reversed" Storyboard.TargetProperty="Opacity" To="0"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <StackPanel.RenderTransform>
                            <ScaleTransform x:Name="ScaleX"/>
                        </StackPanel.RenderTransform>

                        <Rectangle MinWidth="2" MinHeight="2" Fill="{TemplateBinding BorderBrush}" x:Name="Border1"/>
                        <Rectangle MinWidth="2" MinHeight="2" Fill="{TemplateBinding Background}" x:Name="Border2"/>
                        <Grid x:Name="InnerGrid" Background="{TemplateBinding Background}">
                            <Grid.RenderTransform>
                                <ScaleTransform x:Name="ScaleY"/>
                            </Grid.RenderTransform>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                                <RowDefinition Height="*"/>
                            </Grid.RowDefinitions>
                            <ItemsPresenter Margin="{TemplateBinding Padding}" Grid.Row="1"/>
                        </Grid>
                        <Rectangle MinWidth="2" MinHeight="2" Fill="{TemplateBinding Background}" x:Name="Border1Reversed"/>
                        <Rectangle MinWidth="2" MinHeight="2" Fill="{TemplateBinding BorderBrush}" x:Name="Border2Reversed"/>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我也尝试过删除各个部分,例如 VisualStateGroups,但没有成功。

编辑: 例外是Application_UnhandledException 事件中的ApplicationUnhandledExceptionEventArgs 对象。该对象不提供其他信息。

【问题讨论】:

    标签: windows-phone-8 contextmenu windows-phone-toolkit


    【解决方案1】:

    您必须完全按照源代码的方式使用它以及缺少的 xaml 部分。

    用你的代码试试这个[缺少 XAML]

    <TextBlock Text="Press and Hold to Show the ContextMenu">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu>
                <toolkit:ContextMenu.Template>
                    <ControlTemplate TargetType="toolkit:ContextMenu">
                        <StackPanel Width="300" x:Name="OuterPanel">
                            <Grid x:Name="InnerGrid"/>
                            <Path Name="UpperLeftPath" Data="M0,1 L1,1 L1,0" Fill="Green" Stretch="Fill" Margin="12,0" Height="12" Width="18" HorizontalAlignment="Center"/>
                            <Border BorderBrush="Green" BorderThickness="2" Background="#ff20AD2A">
                            <ItemsPresenter/>
                        </Border>
                        </StackPanel>
                    </ControlTemplate>
                </toolkit:ContextMenu.Template>
                <toolkit:MenuItem Header="Item1"/>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
    </TextBlock>
    

    【讨论】:

      猜你喜欢
      • 2016-12-01
      • 2011-03-17
      • 1970-01-01
      • 2011-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-21
      • 2011-03-12
      相关资源
      最近更新 更多