如何在UWP中使用SharpDX(查看github库 SharpDX-Samples-master总结)
使用 Windows.UI.Xaml.Media.Imaging.SurfaceImageSource 给前台的control显示。
新建一个对象ImageSource2D继承SurfaceImageSource,通过使用sharpdx绘制SurfaceImageSource
显示在前端xaml元素。(可以是UIElement的fill或者image的source)
----------
xaml前端有控件Image1,Ellipse1绘制他们的SurfaceImageSource,代码如下
namespace MyFirstDxGame
{
///
/// win2d 测试
///
public sealed partial class BlankPage1 : Page
{
private ImageSource2D MyDrawing;
public BlankPage1()
{
this.InitializeComponent();
MyDrawing = new ImageSource2D((int)Image1.Width, (int)Image1.Height, true);
// Use Scenario1Drawing as a source for the Image control
Image1.Source = MyDrawing;
// Use Scenario1Drawing as a source for the Ellipse shape's fill
Ellipse1.Fill = new Windows.UI.Xaml.Media.ImageBrush() { ImageSource = MyDrawing };
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Image1_Tapped(null,null);
}
private void Image1_Tapped(object sender, Windows.UI.Xaml.Input.TappedRoutedEventArgs e)
{
// Begin updating the SurfaceImageSource
MyDrawing.BeginDraw();
// Clear background
MyDrawing.Clear(Colors.Bisque);
// Create a new pseudo-random number generator
Random randomGenerator = new Random();
byte[] pixelValues = new byte[3]; // Represents the red, green, and blue channels of a color
// Draw 50 random retangles
for (int i = 0; i < 50; i++)
{
// Generate a new random color
randomGenerator.NextBytes(pixelValues);
Windows.UI.Color color = new Windows.UI.Color() { R = pixelValues[0], G = pixelValues[1], B = pixelValues[2], A = 255 }