Windows Phone 开发:如何使自定义 class 出现在 xaml 网格上?

Windows Phone development: How to make a custom class appear on a xaml grid?

我是 Windows Phone 开发新手,我对我正在制作的应用程序有疑问。

我有一个包含 class 的项目,名为 'MainPage.xaml'、'MainPage.xaml.cs' 和 'Character.cs'。 在我的 'Character.cs' class 中,我创建了一个实例化时看起来像矩形的对象(如果我正确地制作了 class)。这是代码:

using System;
using System.Collections.Generic;
//using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Windows.Input;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.UI.Xaml.Shapes;
using System.Diagnostics;

//Blue color: "#FF32C7FF"
//Green color: "#FF32FFE3"

namespace Manipulation
{
public sealed partial class Character : Control
{
    Path myPath = new Path();
    private TranslateTransform dragTranslation;
    private int gameStage;  // Determines what stage of game
    private int colorChoice;
    private int xCord = 200;
    private int yCord = 200;
    private int rHeight = 100;
    private int rWidth = 100;


    public Character()
    {
        Random rand = new Random();
        SolidColorBrush characterColor = new SolidColorBrush();
        Rect myRectangle = new Rect(xCord,yCord,rWidth,rHeight);
        Path myPath = new Path();
        RectangleGeometry myGeo = new RectangleGeometry();
        GeometryGroup myGeoGroup = new GeometryGroup();
        Canvas myCanvas = new Canvas();
        //Grid MyGrid = new Grid();


        colorChoice = rand.Next(1,3);
        if(colorChoice == 1) // Represents blue color
        {
            characterColor.Color = Windows.UI.Color.FromArgb(100,50,199,225);
            myPath.Stroke = characterColor;
        }
        else // Represents green color
        {
            characterColor.Color = Windows.UI.Color.FromArgb(100,50,225,227);
        }
        myPath.StrokeThickness = 5;

        myGeo.Rect = myRectangle;
        myGeoGroup.Children.Add(myGeo);

        myPath.ManipulationDelta += Drag_ManipulationDelta;
        dragTranslation = new TranslateTransform();
        myPath.RenderTransform = this.dragTranslation;
        myPath.ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
        myPath.Data = myGeoGroup;           
    }

    private void Drag_ManipulationDelta(object sender, ManipulationDeltaRoutedEventArgs e)
    {
        dragTranslation.X += e.Delta.Translation.X;
        dragTranslation.Y += e.Delta.Translation.Y;
    }

    public int GameStage { get { return gameStage; } set { gameStage = value; } }
    public int CharacterColorNum { get { return colorChoice; } set { colorChoice = value; } }
    public int XCoordinate { get { return xCord; } set { xCord = value; } }
    public int YCoordinate { get { return yCord; } set { yCord = value; } }
    public int CharacterHeight { get { return rHeight; } set { rHeight = value; } }
    public int CharacterWidth { get { return rWidth; } set {rWidth = value;} }
}
}

我想做的是在我的 'MainPage.xaml.cs' class 我想实例化这个角色 class 并让它把它制作的矩形放在我的 'MainPage.xaml' 网格。因为我是新来的,所以有人可以引导我完成这个吗?

谢谢

在你的 MainPage 添加一个 xmlns xaml:

xmlns:src="clr-namespace:Manipulation"

然后你可以创建它的一个实例:

<src:Character/>

这是一篇不错的文章:http://www.codeproject.com/Articles/17830/Creating-and-consuming-a-custom-WPF-control

确保将默认 Class 名称和 namespace 更改为您在 XAML 中自定义的 Class。

Custom Base Page for Windows Phone

如果您的自定义控件在其他项目中,请不要忘记在 xmlns 中添加程序集:

xmlns:src="clr-namespace:Manipulation;assembly=NAMEASSEMBLY"