TextBlock上的TiltEffect

Mar*_*cze 4 c# windows-phone-7

我正在使用Microsoft提供的TiltEffect工具.我试图将它与TextBlocks一起使用,但是,即使我将TextBlock类型添加到Tiltable项列表中,它也不起作用:

TiltEffect.TiltableItems.Add( typeof( System.Windows.Controls.TextBlock ) );
Run Code Online (Sandbox Code Playgroud)

但是,如果我用边框包围TextBlock ,并将Tap事件移动到Border,它可以正常工作.

这种行为有什么具体原因吗?用Borders包围我所有可点击的TextBlocks并不太优雅.

更新:这同样适用于矩形形状.

UPDATE2:我当前的解决方案是一种解决方法,我定义了一个自定义控件:

<UserControl x:Class="MyApp.Controls.TiltableTextBlock"
    Name="tiltableTextBlock"
    ...>
    <Button Margin="0" Padding="0">
        <Button.Template>
            <ControlTemplate>
                <TextBlock Margin="0" Padding="0"
                    Text="{Binding Text, ElementName=tiltableTextBlock}"
                    Style="{Binding Style, ElementName=tiltableTextBlock}" />
            </ControlTemplate>
        </Button.Template>
    </Button>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

并在代码背后:

public partial class TiltableTextBlock : UserControl
{
    public TiltableTextBlock()
    {
        InitializeComponent();
    }

    public string Text
    {
        get { return (string)GetValue( TextProperty ); }
        set { SetValue( TextProperty, value ); }
    }

    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register( "Text", typeof( string ), typeof( TiltableTextBlock ), new PropertyMetadata( String.Empty ) );

    //NOTE: hiding the Style property of the base class, so the Style does not get applied to the UserControl, rather to the TextBlock.
    public new Style Style
    {
        get { return (Style)GetValue( StyleProperty ); }
        set { SetValue( StyleProperty, value ); }
    }

    public new static readonly DependencyProperty StyleProperty =
        DependencyProperty.Register( "Style", typeof( Style ), typeof( TiltableTextBlock ), new PropertyMetadata( new Style( typeof( TextBlock ) ) ) );
}
Run Code Online (Sandbox Code Playgroud)

我目前不使用任何其他特定于TextBlock的属性,只使用Text,因此如果需要TextWrapping,FontSize等,则必须以相同的方式实现它.

但是我对这个解决方案并不满意,所以仍然在寻找更优雅的解决方法.

UPDATE3:上面的方法并不完美,我发现它不确定地崩溃了"参数不正确"异常(有时它是在我启动应用程序后抛出的,有时候不是).

Col*_*inE 6

我对Silverlight工具包倾斜效果感到不满意,尤其是它"基于类型"应用于元素的方式.所以我写了一个替代方案.您还可以配置要应用的"倾斜度".源代码可以在这里找到:

Metro in Motion第4部分:倾斜效应

然后,您可以单独将tile应用于元素,如下所示:

<TextBlock local:MetroInMotion.Tilt="6"/> 
Run Code Online (Sandbox Code Playgroud)

整数指定要应用的倾斜度.我建议使用相当低的值,原生效果非常微妙,但是人们倾向于在自己的silerlight应用程序中使它太极端,Metro效果应该是微妙的,它们不应该对你大喊大叫!


小智 5

如果有人遇到这个,我有另一个解决方案.

只需用ListBoxItem标签包含TextBlock元素.

例如

<ListBoxItem>
   <TextBlock>
       A Tilting Textblock
   </TextBlock>
</ListBoxItem>
Run Code Online (Sandbox Code Playgroud)