Windows runtime Windows应用商店应用程序不支持WPF';s<;InkCanvas>;
我正在开发一个应用程序,可以使用鼠标或手写笔通过触摸屏进行输入。。发现Windows runtime Windows应用商店应用程序不支持WPF';s<;InkCanvas>;,windows-runtime,windows-store-apps,winrt-xaml,inkcanvas,Windows Runtime,Windows Store Apps,Winrt Xaml,Inkcanvas,我正在开发一个应用程序,可以使用鼠标或手写笔通过触摸屏进行输入。。发现对我来说非常有用,但Windows应用商店应用程序项目不识别/支持标记 有没有遇到过类似的需求和场景。 任何建议 当然,其中一个是,但它不像WinRT中使用类而不是WPF的InkCanvas那样灵活。我认为您可能需要将自己的画布控件与一些形状(?)一起使用,并自己处理指针~事件,但我自己没有这样做,因此我不能确定。在xaml中使用此控件,如: Use this control in xaml like: <ctrl:In
对我来说非常有用,但Windows应用商店应用程序项目不识别/支持
标记
有没有遇到过类似的需求和场景。
任何建议
当然,其中一个是,但它不像WinRT中使用类而不是WPF的InkCanvas那样灵活。我认为您可能需要将自己的画布控件与一些形状(?)一起使用,并自己处理指针~事件,但我自己没有这样做,因此我不能确定。在xaml中使用此控件,如:
Use this control in xaml like: <ctrl:InkCanvas x:Name="inkCanvas" />
别忘了引用“ctrl”,比如:xmlns:ctrl=“using:MyAppNamespace”
使用系统;
使用System.Collections.Generic;
使用Windows.Devices.Input;
使用Windows基金会;
使用Windows.UI;
使用Windows.UI.Input;
使用Windows.UI.Input.Inking;
使用Windows.UI.Xaml.Controls;
使用Windows.UI.Xaml.Input;
使用Windows.UI.Xaml.Media;
使用Windows.UI.Xaml.Shapes;
使用System.Linq;
使用System.Threading.Tasks;
使用Windows.Storage;
使用Windows.Storage.Streams;
使用Windows.UI.Xaml.Media.Imaging;
使用System.Runtime.InteropServices.WindowsRuntime;
名称空间MyAppNamespace
{
公共类InkCanvas:Canvas
{
InkManager m_InkManager=new Windows.UI.Input.Inking.InkManager();
私人单位m_PenId;
私人uint\u touchID;
私人点(上一次联络);;
专用点电流接触器;
专用双x1;
私人双y1;
私人双x2;
私人双y2;
私有颜色m_CurrentDrawingColor=颜色。黑色;
专用双m_CurrentDrawingSize=4;
公共列表笔划{get{return m_InkManager.GetStrokes().ToList();}
公共画布()
{
m_InkManager.Mode=Windows.UI.Input.Inking.InkOperationMode.Inking;
设置默认值(m_CurrentDrawingSize、m_CurrentDrawingColor);
this.PointerPressed+=新的PointerEventHandler(OnCanvasPointerPressed);
this.PointerMoved+=新的PointerEventHandler(OnCanvasPointerMoved);
this.PointerReleased+=新的PointerEventHandler(OnCanvasPointerReleased);
this.PointerExited+=新的PointerEventHandler(oncanvaspointerreleed);
}
公共空间清除()
{
这个.Children.Clear();
var strokes=m_InkManager.GetStrokes();
对于(int i=0;i0)
{
var tempFile=wait ApplicationData.Current.LocalFolder.CreateFileAsync(Guid.NewGuid().ToString()+“.jpg”);
var writeStream=await tempFile.OpenAsync(FileAccessMode.ReadWrite);
等待m_InkManager.SaveAsync(writeStream);
等待writeStream.FlushAsync();
var reader=newdatareader(writeStream.GetInputStreamAt(0));
字节=新字节[writeStream.Size];
wait reader.LoadAsync((uint)writeStream.Size);
reader.ReadBytes(字节);
reader.DetachStream();
等待tempFile.DeleteAsync();
}
返回字节;
}
公共异步任务GetBitmapImage()
{
var bitmapImage=新的bitmapImage();
var bytes=await GetBytes();
如果(bytes.Length>0)
{
使用(var stream=new InMemoryRandomAccessStream())
{
//对于AsBuffer,手动添加[使用System.Runtime.InteropServices.WindowsRuntime];
wait stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
bitmapImage.SetSource(流);
}
}
返回位图图像;
}
#区域指针事件处理程序
public void OnCanvasPointerReleased(对象发送方,PointerRoutedEventArgs e)
{
if(e.Pointer.PointerId==m_PenId)
{
Windows.UI.Input.PointerPoint pt=e.GetCurrentPoint(此);
//将指针信息传递给InkManager。
m_InkManager.ProcessPointerUp(pt);
}
else if(e.Pointer.PointerId==\u touchID)
{
//处理触摸输入
}
_touchID=0;
m_PenId=0;
e、 已处理=正确;
}
私有void OnCanvasPointerMoved(对象发送方,PointerRoutedEventArgs e)
{
if(e.Pointer.PointerId==m_PenId)
{
PointerPoint pt=e.GetCurrentPoint(此);
//指针移动时在画布上渲染红线。
//Distance()是一个应用程序定义的函数,用于测试
//指针是否移动得足够远以证明
//画一条新的线。
电流触点pt=pt位置;
x1=_先前的触点PT.X;
y1=_先前的接触点Y;
x2=电流触点PT.X;
y2=电流触点PT.Y;
var color=m_CurrentDrawingColor;
var大小=m_CurrentDrawingSize;
如果(距离(x1,y1,x2,y2)>2.0)
{
行=新行()
{
X1=X1,
Y1=Y1,
X2=X2,
Y2=Y2,
冲程厚度=尺寸,
笔划=新的SolidColorBrush(颜色)
};
_previousContactPt=当前ContactPt;
//在画布上绘制线条,方法是将线条对象添加为
//画布对象的子对象。
this.Children.Add(行);
}
//将指针信息传递给InkManager。
m_InkManager.ProcessPointerUpdate(pt);
}
else if(e.Pointer.PointerId==\u touchID)
{
//过程t
using System;
using System.Collections.Generic;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI;
using Windows.UI.Input;
using Windows.UI.Input.Inking;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Shapes;
using System.Linq;
using System.Threading.Tasks;
using Windows.Storage;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Media.Imaging;
using System.Runtime.InteropServices.WindowsRuntime;
namespace MyAppNamespace
{
public class InkCanvas : Canvas
{
InkManager m_InkManager = new Windows.UI.Input.Inking.InkManager();
private uint m_PenId;
private uint _touchID;
private Point _previousContactPt;
private Point currentContactPt;
private double x1;
private double y1;
private double x2;
private double y2;
private Color m_CurrentDrawingColor = Colors.Black;
private double m_CurrentDrawingSize = 4;
public List<InkStroke> Strokes { get { return m_InkManager.GetStrokes().ToList(); } }
public InkCanvas()
{
m_InkManager.Mode = Windows.UI.Input.Inking.InkManipulationMode.Inking;
SetDefaults(m_CurrentDrawingSize, m_CurrentDrawingColor);
this.PointerPressed += new PointerEventHandler(OnCanvasPointerPressed);
this.PointerMoved += new PointerEventHandler(OnCanvasPointerMoved);
this.PointerReleased += new PointerEventHandler(OnCanvasPointerReleased);
this.PointerExited += new PointerEventHandler(OnCanvasPointerReleased);
}
public void Clear()
{
this.Children.Clear();
var strokes = m_InkManager.GetStrokes();
for (int i = 0; i < strokes.Count; i++)
strokes[i].Selected = true;
m_InkManager.DeleteSelected();
}
public async Task<byte[]> GetBytes()
{
var bytes = new byte[0];
if (Strokes.Count > 0)
{
var tempFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".jpg");
var writeStream = await tempFile.OpenAsync(FileAccessMode.ReadWrite);
await m_InkManager.SaveAsync(writeStream);
await writeStream.FlushAsync();
var reader = new DataReader(writeStream.GetInputStreamAt(0));
bytes = new byte[writeStream.Size];
await reader.LoadAsync((uint)writeStream.Size);
reader.ReadBytes(bytes);
reader.DetachStream();
await tempFile.DeleteAsync();
}
return bytes;
}
public async Task<BitmapImage> GetBitmapImage()
{
var bitmapImage = new BitmapImage();
var bytes = await GetBytes();
if (bytes.Length > 0)
{
using (var stream = new InMemoryRandomAccessStream())
{
// For AsBuffer manually add [ using System.Runtime.InteropServices.WindowsRuntime ];
await stream.WriteAsync(bytes.AsBuffer());
stream.Seek(0);
bitmapImage.SetSource(stream);
}
}
return bitmapImage;
}
#region Pointer Event Handlers
public void OnCanvasPointerReleased(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerId == m_PenId)
{
Windows.UI.Input.PointerPoint pt = e.GetCurrentPoint(this);
// Pass the pointer information to the InkManager.
m_InkManager.ProcessPointerUp(pt);
}
else if (e.Pointer.PointerId == _touchID)
{
// Process touch input
}
_touchID = 0;
m_PenId = 0;
e.Handled = true;
}
private void OnCanvasPointerMoved(object sender, PointerRoutedEventArgs e)
{
if (e.Pointer.PointerId == m_PenId)
{
PointerPoint pt = e.GetCurrentPoint(this);
// Render a red line on the canvas as the pointer moves.
// Distance() is an application-defined function that tests
// whether the pointer has moved far enough to justify
// drawing a new line.
currentContactPt = pt.Position;
x1 = _previousContactPt.X;
y1 = _previousContactPt.Y;
x2 = currentContactPt.X;
y2 = currentContactPt.Y;
var color = m_CurrentDrawingColor;
var size = m_CurrentDrawingSize;
if (Distance(x1, y1, x2, y2) > 2.0)
{
Line line = new Line()
{
X1 = x1,
Y1 = y1,
X2 = x2,
Y2 = y2,
StrokeThickness = size,
Stroke = new SolidColorBrush(color)
};
_previousContactPt = currentContactPt;
// Draw the line on the canvas by adding the Line object as
// a child of the Canvas object.
this.Children.Add(line);
}
// Pass the pointer information to the InkManager.
m_InkManager.ProcessPointerUpdate(pt);
}
else if (e.Pointer.PointerId == _touchID)
{
// Process touch input
}
}
private double Distance(double x1, double y1, double x2, double y2)
{
double d = 0;
d = Math.Sqrt(Math.Pow((x2 - x1), 2) + Math.Pow((y2 - y1), 2));
return d;
}
public void OnCanvasPointerPressed(object sender, PointerRoutedEventArgs e)
{
// Get information about the pointer location.
PointerPoint pt = e.GetCurrentPoint(this);
_previousContactPt = pt.Position;
// Accept input only from a pen or mouse with the left button pressed.
PointerDeviceType pointerDevType = e.Pointer.PointerDeviceType;
if (pointerDevType == PointerDeviceType.Pen ||
pointerDevType == PointerDeviceType.Mouse &&
pt.Properties.IsLeftButtonPressed)
{
// Pass the pointer information to the InkManager.
m_InkManager.ProcessPointerDown(pt);
m_PenId = pt.PointerId;
e.Handled = true;
}
else if (pointerDevType == PointerDeviceType.Touch)
{
// Process touch input
}
}
#endregion
#region Mode Functions
// Change the color and width in the default (used for new strokes) to the values
// currently set in the current context.
private void SetDefaults(double strokeSize, Color color)
{
var newDrawingAttributes = new InkDrawingAttributes();
newDrawingAttributes.Size = new Size(strokeSize, strokeSize);
newDrawingAttributes.Color = color;
newDrawingAttributes.FitToCurve = true;
m_InkManager.SetDefaultDrawingAttributes(newDrawingAttributes);
this.Background = new SolidColorBrush(Colors.White);
}
#endregion
}
}