我怎样才能创建一个量表(即速度计)? [英] How can I create a gauge (i.e. speedometer)?

查看:112
本文介绍了我怎样才能创建一个量表(即速度计)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建量表(即速度表)?

具体而言,我正在尝试在此



我成功创建了一个戒指。然而,现在我需要在环上添加刻度。


$ b XAML:

 < Window x:Class =MotoLens.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
xmlns:mc =http://schemas.openxmlformats.org/markup-compatibility/2006
xmlns:local =clr-namespace:MotoLens
mc:Ignorable =d
背景=黑色
标题=MainWindow高度=350宽度=525>
< Window.Resources>
< local:ValueToBrushConverter x:Key =ValueToBrushConverter/>
< /Window.Resources>
<网格>
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition />
< RowDefinition />
< /Grid.RowDefinitions>

< / Grid>
< / Window>


解决方案

过去,当我不得不创建自定义饼图图表和其他各种需要椭圆形状和弧线的东西,我已经使用了 Microsoft.Expression.Drawing 库。只需将该引用添加到您的项目中,即可访问 Arc ,这将在这里创造奇迹。使用 Path ArcSegment 和其他各种组件可以达到完全相同的效果,但我发现它很容易工作使用 Arc 。说到添加引用......这是类似这样的原因之一是在Blend中处理的,如果你还没有这样做,因为它会自动引入这些引用。我在这里没有做出任何假设,所以这就是为什么我给出了添加引用的说明。

因此,有了这个说法,下面是一个10分钟的项目示例,它显示了什么你可以这样做:



 < Window x:Class =WpfApplication.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d =http://schemas.microsoft.com/expression/blend/2008
xmlns:mc = http://schemas.openxmlformats.org/markup-compatibility/2006
xmlns:ed =http://schemas.microsoft.com/expression/2010/drawing
xmlns:local = clr-namespace:WpfApplication1
mc:Ignorable =d
Title =MainWindowHeight =350Width =525>
< Grid Background =#FF2E2F45>
<网格边距=0,0,0,-120>
< ed:Arc StartAngle = - 120EndAngle =120Stretch =None
Height =300Width =300StrokeThickness =20
StrokeDashArray = .25Stroke =#FF484D5F/>
Height =300Width =300StrokeThickness =20
StrokeDashArray =。25Stroke =Turquoise/>
< / Grid>
< StackPanel Horizo​​ntalAlignment =CenterVerticalAlignment =Center
Margin =0,0,0,-50>
< TextBlock Text =km / hForeground =#FF878A9FHorizo​​ntalAlignment =Center
FontSize =14/>
< TextBlock Text =43Foreground =WhiteHorizo​​ntalAlignment =Center
FontSize =110FontWeight =LightMargin =0,-25,0,0/> ;
< / StackPanel>
< / Grid>
< / Window>

不言而喻,有很多静态代码在那里进行,就位置的东西,但这是为示范做的。大部分内容甚至不是必需的,根据您提供的链接中该应用的图像显示,但我对迷惑细节感兴趣,并希望布局在某种程度上与您的屏幕截图相匹配。我假设你会创建一个控件,所以你要清理它并创建适当的绑定。

就圆形渐变主题而言,我没有在这里讨论过这个问题,因为这是一个完全不同于你所问的主题,更多可以是在这里发现一个不同的StackOverflow问题:沿圆形路径创建渐变画笔


How can I create a gauge (i.e. speedometer)?

Specifically, I am trying to build the image on this link.

I am successful at creating a ring. However, now I need to add the ticks to the ring.

XAML:

<Window x:Class="MotoLens.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MotoLens"
        mc:Ignorable="d"
        Background="Black"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:ValueToBrushConverter x:Key="ValueToBrushConverter" />
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Ellipse Grid.Row="0" Width="300" Fill="Transparent"  StrokeThickness="10" Stroke="{Binding ElementName=Slider, Path=Background}"  StrokeDashArray="1" StrokeEndLineCap="Square" />
    </Grid>
</Window>

解决方案

In the past, when I had to create custom pie charts and other various things requiring elliptical shapes and arcs, I've used the Microsoft.Expression.Drawing library. Just add that reference to your project and you'll get access to Arc, which will do wonders here. The exact same thing can be achieved with Path, ArcSegment and various other components, but I just find it easy to work with Arc. Speaking of adding references... that is one of the reasons things like this are handled in Blend, if you're not already doing that, as it automatically brings in those references. I made no assumptions here, so that's why I gave the instructions of adding the reference.

So, with that said, here's an example a 10-minute project that shows what you can do:

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing"
        xmlns:local="clr-namespace:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid Background="#FF2E2F45">
        <Grid Margin="0,0,0,-120">
            <ed:Arc StartAngle="-120" EndAngle="120" Stretch="None" 
                    Height="300" Width="300" StrokeThickness="20"      
                    StrokeDashArray=".25" Stroke="#FF484D5F"/>
            <ed:Arc StartAngle="-120" EndAngle="-35" Stretch="None" 
                    Height="300" Width="300" StrokeThickness="20"
                    StrokeDashArray=".25" Stroke="Turquoise"/>
        </Grid>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"
                    Margin="0,0,0,-50">
            <TextBlock Text="km/h" Foreground="#FF878A9F" HorizontalAlignment="Center"
                        FontSize="14"/>
            <TextBlock Text="43" Foreground="White" HorizontalAlignment="Center"
                        FontSize="110" FontWeight="Light" Margin="0,-25,0,0"/>
        </StackPanel>
    </Grid>
</Window>

It goes without saying that there is a lot of static code going on in there, as far as placement of things, but that was done for the demonstration. Most of it is not even necessary, based on what the image of that app in the link you've provided shows, but I'm obsessive about detail and wanted the layout to somewhat match your screenshot in the question. I assume you would create a control out of this, so you'd clean it all up and create appropriate bindings.

As far as the circular gradient topic is concerned, I did not bother working on that here, as that's a completely different subject from what you were asking about and more can be found at a different StackOverflow question right over here: Creating Gradient Brush along a Circular Path

这篇关于我怎样才能创建一个量表(即速度计)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆