我是Windows Phone开发的新手,我对我正在制作的应用程序有疑问。
我有一个项目,其中包含名为' MainPage.xaml',' MainPage.xaml.cs'和' Character.cs'。 在我的角色中#' class I在实例化时创建一个看起来像一个矩形的对象(如果我正确地创建了类)。这是代码:
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我想实例化这个Character类,并让它在我的' MainPage.xaml'网格。有人可以引导我解决这个问题,因为我是新来的吗?
由于
答案 0 :(得分:1)
在MainPage xaml中添加xmlns:
xmlns:src="clr-namespace:Manipulation"
然后你可以创建它的实例:
<src:Character/>
这是一篇不错的文章:http://www.codeproject.com/Articles/17830/Creating-and-consuming-a-custom-WPF-control
答案 1 :(得分:1)
如果您在其他项目中的自定义控件不忘记在xmlns中添加程序集:
xmlns:src="clr-namespace:Manipulation;assembly=NAMEASSEMBLY"
答案 2 :(得分:0)
确保您在Class
中将默认namespace
名称和XAML
更改为自定义班级。