Sharing WPF Rendering between two applications


This Content is from Stack Overflow. Question asked by Martin Sparkes

I have a Dot-Net Core 6.0 Project which includes 2 wpf applications and a shared backend codebase

WPF Apps


Both use the same map rendering code to draw the the map on a Canvas object with the data displayed being determined by the collections of HexEntity objects supplied to the rendering function.

Ideally this shared code should be included in the BackendAPI library to avoid code duplication, however I am struggling with how to move these functions into the library because they use Windows presentation function calls (calls to Canvas, Rectangle, TextBox etc.).

An example function I want to move to this library is:

    internal void RenderMapGrid(Canvas canvasWorldMap, int cellSize, int worldHexCount)
        for (int xPos = 0; xPos < worldHexCount; xPos++)
            int delta = 0;
            if (xPos % 2 == 1) delta = cellSize / 2;
            for (int yPos = 0; yPos < worldHexCount; yPos++)
                Rectangle cell = new Rectangle();
                cell.Stroke = new SolidColorBrush(Color.FromRgb(90, 90, 90));
                cell.StrokeThickness = 1;
                cell.Fill = new SolidColorBrush(Color.FromRgb(30, 30, 30));
                cell.Width = cellSize;
                cell.Height = cellSize;
                Canvas.SetLeft(cell, xPos * cellSize);
                Canvas.SetTop(cell, (yPos * cellSize) + delta);

Is what I want to do posible, and if so how do I aproach this?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?