简体   繁体   English

在WP8中创建自己的切换按钮?

[英]Creating own toggle button in WP8?

I need to port an Android app which has a widget to WP8. 我需要将具有小部件的Android应用程序移植到WP8。

For this I need to create my own toggle button (user control). 为此,我需要创建自己的切换按钮(用户控件)。 So I just created a subclass of Button called MyToggleButton and use the standard callbacks to change the image and the text of the button. 因此,我刚刚创建了一个名为MyToggleButton的Button子类,并使用标准的回调函数来更改按钮的图像和文本。

To catch the click down I use onMouseEnter and when the click is finished, ie the moud button goes up again I use onMouseLeave 要捕获单击,我使用onMouseEnter ,单击完成后,即再次按下鼠标按钮,则使用onMouseLeave

Wihle this works without problems - the issue is that when clicking the onMouseEnter seems to be called with a little delay and this button changes the ButtonImage with a minimal delay as opposed to a normal button ( I am using Visual Studio Express 2012 with Emulator since I do not yet have a real phone for testing) 这样就可以正常工作-问题是单击onMouseEnter似乎被延迟了一点,并且此按钮以最小的延迟(而不是普通按钮)更改ButtonImage(我将Visual Studio Express 2012与Emulator一起使用,因为我还没有真正的手机进行测试)

WHile I found other ways of creating user controls specifing lots in XAML, however I find the described way of just using the standard methods easier. 虽然我发现了在XAML中创建用于指定批次的用户控件的其他方法,但是我发现上述仅使用标准方法的方法更加容易。 I am just not sure, where this minimal delay comes from. 我只是不确定,这种最小延迟来自何处。

You should probably not make a custom toggle in the first case. 在第一种情况下,您可能不应该进行自定义切换。 Porting an application from Android doesn't mean you also have to port its graphics. 从Android移植应用程序并不意味着您也必须移植其图形。 They simply don't share the same design language. 它们只是不共享相同的设计语言。 You should probably stick to the native WP8 toggle. 您可能应该坚持使用本机WP8切换键。

However, if you really want to create a custom checkbox/toggle, you should check this . 但是,如果您确实要创建自定义复选框/切换,则应选中此项 This was made for WP7, but it also applies to WP8. 这是针对WP7所做的,但也适用于WP8。

Here is a simplified "Toggle" button using images. 这是一个使用图像的简化的“切换”按钮。 based on CheckBox 基于CheckBox

<Style TargetType="{x:Type CheckBox}" >
            <Setter Property="Template" >
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type CheckBox}">
                        <Grid>
                            <ContentPresenter x:Name="Part_Content" />
                            <Image Name="image" Source="/WpfApplication4;component/Images/avatar63.jpg" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="true" >
                                <Setter TargetName="image" Property="Source" Value="/WpfApplication4;component/Images/imagesCA7JZMMY.jpg"/>
                            </Trigger>
                            <Trigger Property="IsChecked" Value="False" >
                                <Setter TargetName="image" Property="Source" Value="/WpfApplication4;component/Images/avatar63.jpg"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

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

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