将简单的WPF代码重新编译为WinRT

时间:2014-12-29 19:17:34

标签: c# wpf xaml windows-8 windows-runtime

我尝试在WinRT中将以下代码重新编码为模拟四天,但我无法做到。 :/ WinRT没有DrawingImage,所以我尝试使用Path或Polyline进行类比,但是我失败了。 我敢肯定,如果有人知道WPF和WinRT这对他来说重塑这段代码并不是问题。 我认为,只有问题在于DrawingImage和触发。 我知道,这是很多代码,但我认为,这个问题是微不足道的,但我不知道如何解决这个问题四天。 ; / 我非常感谢帮助改造这段代码。

<Window x:Class="HexagonGridTest.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:HexagonGridTest"
    Title="Window1" Height="741.836" Width="697.351" Background="#FF232323">
    <Window.Resources>
        <DrawingImage x:Key="HexagonImage">
            <DrawingImage.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Brush="#11AA11"  
                          Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                       <GeometryDrawing.Pen>
                           <Pen Brush="Black" Thickness="10" LineJoin="Round"/>
                        </GeometryDrawing.Pen>
                    </GeometryDrawing>
               </DrawingGroup>
            </DrawingImage.Drawing>
       </DrawingImage>
       <DrawingImage x:Key="HexagonHoverImage">
           <DrawingImage.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Brush="Khaki"  
                           Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                        <GeometryDrawing.Pen>
                           <Pen Brush="Black" Thickness="10" LineJoin="Round"/>
                       </GeometryDrawing.Pen>
                    </GeometryDrawing>
                </DrawingGroup>
           </DrawingImage.Drawing>
        </DrawingImage>
        <DrawingImage x:Key="HexagonPressedImage">
             <DrawingImage.Drawing>
                <DrawingGroup>
                    <GeometryDrawing Brush="Orange"  
                          Geometry="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z">
                       <GeometryDrawing.Pen>
                           <Pen Brush="Black" Thickness="10" LineJoin="Round"/>
                       </GeometryDrawing.Pen>
                    </GeometryDrawing>
               </DrawingGroup>
           </DrawingImage.Drawing>
        </DrawingImage>

        <Style x:Key="HexagonButtonStyle" TargetType="{x:Type Button}">
            <Setter Property="Background" Value="Khaki"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Grid>
                            <Image x:Name="img" Source="{StaticResource HexagonImage}"/>
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                             <Trigger Property="IsMouseOver" Value="True">
                                 <Setter TargetName="img" Property="Source" Value="{StaticResource HexagonHoverImage}"/>
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="img" Property="Source" Value="{StaticResource HexagonPressedImage}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                   </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <local:HexagonGrid Rows="4" Columns="1" HexagonSideLength="40">
            <local:HexagonGrid.Resources>
                <Style TargetType="{x:Type Button}" BasedOn="{StaticResource HexagonButtonStyle}"/>
            </local:HexagonGrid.Resources>
            <Button Grid.Row="0" Grid.Column="2" Content="0,2"/>
            <Button Grid.Row="0" Grid.Column="1" Content="0,1"/>
            <Button Grid.Row="1" Grid.Column="2" Content="1,2"/>
        </local:HexagonGrid>
    </Grid>
</Window>

此类应与WinRT兼容。

using System;
using System.Windows;
using System.Windows.Controls;

namespace HexagonGridTest
{
    public class HexagonGrid : Grid
    {
        public static readonly DependencyProperty HexagonSideLengthProperty =
           DependencyProperty.Register("HexagonSideLength", typeof(double), typeof(HexagonGrid), null);

        public double HexagonSideLength
        {
            get { return (double)GetValue(HexagonSideLengthProperty); }
            set { SetValue(HexagonSideLengthProperty, value); }
        }

        public static readonly DependencyProperty RowsProperty =
            DependencyProperty.Register("Rows", typeof(int), typeof(HexagonGrid), new PropertyMetadata((int)1));


        public int Rows
        {
            get { return (int)GetValue(RowsProperty); }
            set { SetValue(RowsProperty, value); }
        }

        public static readonly DependencyProperty ColumnsProperty =
            DependencyProperty.Register("Columns", typeof(int), typeof(HexagonGrid), new PropertyMetadata((int)1));

        public int Columns
        {
            get { return (int)GetValue(ColumnsProperty); }
            set { SetValue(ColumnsProperty, value); }
        }


        protected override Size MeasureOverride(Size constraint)
        {
            double side = HexagonSideLength;
            double width = 2 * side;
            double height = side * Math.Sqrt(3.0);
            double colWidth = 0.75 * width;
            double rowHeight = height;

            Size availableChildSize = new Size(width, height);

            foreach (FrameworkElement child in this.Children)
            {
                child.Measure(availableChildSize);
            }

            double totalHeight = Rows * rowHeight;
            if (Columns > 1)
                totalHeight += (0.5 * rowHeight);
            double totalWidth = Columns + (0.5 * side);

            Size totalSize = new Size(totalWidth, totalHeight);

            return totalSize;
        }

        protected override Size ArrangeOverride(Size arrangeSize)
        {
            double side = HexagonSideLength;
            double width = 2 * side;
            double height = side * Math.Sqrt(3.0);
            double colWidth = 0.75 * width;
            double rowHeight = height;

            Size childSize = new Size(width, height);

            foreach (FrameworkElement child in this.Children)
            {
                int row = GetRow(child);
                int col = GetColumn(child);

                double left = col * colWidth;
                double top = row * rowHeight;
                bool isUnevenCol = (col % 2 != 0);
                if (isUnevenCol)
                    top += (0.5 * rowHeight);

                child.Arrange(new Rect(new Point(left, top), childSize));
            }

            return arrangeSize;
        }
    }
}

我的页面资源与样式代码,这是不正确的,这是肯定的,但我无法纠正。 不要抛出任何错误,只是不要画任何东西。

<Page.Resources>
    <Path x:Key="HexagoPath"
          Stroke="DarkGoldenRod" 
          StrokeThickness="7"
          Fill="AliceBlue"
          Data="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z" />

    <Path x:Key="HexagoPathHover"
          Stroke="DarkGoldenRod" 
          StrokeThickness="7"
          Fill="Bisque"
          Data="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z" />

    <Path x:Key="HexagoPathPressed"
          Stroke="DarkGoldenRod" 
          StrokeThickness="7"
          Fill="Brown"
          Data="M 250,0 L 750,0 L 1000,433 L 750,866 L 250,866 L 0,433 Z" />

    <Style x:Key="HexagoPathStyle" TargetType="Button">
        <Setter Property="MinWidth" Value="0"/>
        <Setter Property="MinHeight" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <Path x:Name="PH" DataContext="{StaticResource HexagoPath}">

                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="PointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="Border">
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverBackgroundThemeBrush}"/>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverForegroundThemeBrush}"/>
                            </ObjectAnimationUsingKeyFrames>
                            </Storyboard>
                            </VisualState>

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

                            </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Path>                          
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Page.Resources>

0 个答案:

没有答案