简体   繁体   English

在Windows Phone 8.1上触摸时如何更改矩形颜色

[英]How to change Rectangle color when touched on Windows Phone 8.1

I 'm working on an app for wp 8.1 on C# and xaml (visual studio 2013). 我正在使用C#xaml上的wp 8.1应用程序(Visual Studio 2013)。

I have a basic page that contains rectangles that function as buttons. 我有一个基本页面,其中包含用作按钮的矩形。 These rectangles lead to other pages. 这些矩形指向其他页面。 My wish is that touching these transparent rectangles will change the color to blue. 我希望触摸这些透明矩形将颜色更改为蓝色。

<Rectangle  Fill="#FFD69F50" HorizontalAlignment="Left" Height="48" Margin="58,94,0,0" Stroke="Black" VerticalAlignment="Top" Width="73" Tap="h1_Tapped"/>

I have researched but cannot find the fix. 我已经研究过,但找不到解决方法。 Is my request possible? 我的要求可以吗?

Here is an example picture. 这是示例图片。

在此处输入图片说明

The easy way is to use a Button rather than a Rectangle. 最简单的方法是使用Button而不是Rectangle。 It sounds like your use is essentially button-ish so you're better off using a Button control which already implements Button semantics. 听起来您的使用本质上是按钮式的,所以最好使用已经实现Button语义的Button控件。 Otherwise you'll need to add a lot of code to implement that yourself. 否则,您将需要添加大量代码来自己实现。

Specifically to your question, buttons handle PointerPressed events by shifting and changing their colour. 专门针对您的问题,按钮通过移动和更改其颜色来处理PointerPressed事件。 By default this is to the user's theme colour, but you can edit the Button's template to pick your own colour and remove the shift. 默认情况下,这是用户的主题颜色,但是您可以编辑Button的模板以选择您自己的颜色并删除偏移。

To do so, right click on the Button in the designer (I like to do this in the Document Outline pane, since it's easy to find the right control there), and choose Edit Template.Edit a Copy... from the context menu. 为此,请在设计器中的“按钮”上单击鼠标右键(我喜欢在“文档大纲”窗格中执行此操作,因为可以在其中轻松找到合适的控件),然后从上下文菜单中选择“编辑模板”。 。 This will create a copy of the current template so you can edit it. 这将创建当前模板的副本,以便您可以对其进行编辑。

Look through the copied template and find the VisualState x:Name="Pressed" section. 浏览复制的模板,找到VisualState x:Name =“ Pressed”部分。 This controls the appearance of the button when it is pressed. 当按下按钮时,它控制按钮的外观。

<VisualState x:Name="Pressed">
    <Storyboard>
        <PointerDownThemeAnimation Storyboard.TargetName="Grid"/>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedForegroundThemeBrush}"/>
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ButtonPressedBackgroundThemeBrush}"/>
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

By default this does three things: 默认情况下,这会做三件事:

  1. Apply the PointerDownThemeAnimation to shift the button 应用PointerDownThemeAnimation来移动按钮
  2. Change the Background colour to ButtonPressedBackgroundThemeBrush 将背景颜色更改为ButtonPressedBackgroundThemeBrush
  3. Change the Foreground colour to ButtonPressedForegroundThemeBrush 将前景颜色更改为ButtonPressedForegroundThemeBrush

You can edit these to do whatever you'd like, including removing the animation and changing the target colours. 您可以编辑这些文件以执行所需的任何操作,包括删除动画和更改目标颜色。 I'd recommend leaving the colours as theme resources even if you define your own and letting the custom colours fall back to defaults in high contrast modes. 我建议即使您定义自己的颜色,也要保留颜色作为主题资源,并在高对比度模式下让自定义颜色恢复为默认值。

To allow the buttons to be small squares as in your picture, rather than the wider rectangles in your Xaml, you'll probably need to set the button's MinWidth property as well as its Width property. 为了使按钮可以像图片中那样是小正方形,而不是Xaml中的较宽矩形,您可能需要设置按钮的MinWidth属性以及Width属性。

If you really want to stick with Rectangles instead of Buttons then you'll need to write your own code to track the PointerPressed and PointerReleased events. 如果您真的想使用矩形而不是按钮,那么您需要编写自己的代码来跟踪PointerPressed和PointerReleased事件。 On PointerPressed capture the pointer and set the new colour, and on PointerReleased reverse both of those. 在PointerPressed上捕获指针并设置新的颜色,而在PointerReleased上将这两种颜色反转。

private void rectControl_PointerPressed( object sender , PointerRoutedEventArgs e )
{
    rectControl.Fill = new SolidColorBrush(Colors.Blue);
}

private void rectControl_PointerReleased( object sender , PointerRoutedEventArgs e )
{
    rectControl.Fill = new SolidColorBrush(Colors.White);
}

or define a property in your viewModel and then bind the Fill property of rectangle with the defined property in viewModel. 或在viewModel中定义一个属性,然后将矩形的Fill属性与viewModel中定义的属性绑定。

private SolidColorBrush rectangleFill;
public SolidColorBrush RectangleFill
{
    get{ return rectangleFill;}
    set{ rectangleFill=value;
         NotifyPropertyChanged("RectangleFill");
       }
}

//Xaml code: // Xaml代码:

<Rectangle Fill={Binding RectangleFill}>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM