簡體   English   中英

Need help converting custom C# Windows forms controls to custom C# Universal Windows Platform controls

[英]Need help converting custom C# Windows forms controls to custom C# Universal Windows Platform controls

I am converting a C# windows forms app to a C# universal windows platform (UWP) app. 在這個應用程序中,有一些自定義控件使用 Paint 事件來添加處理程序方法。 在此處理程序中,您將獲得一個 PaintEventArgs object,並從那里獲得一個可以進行繪圖的圖形 object。 我需要在 UWP 中做同樣的事情,但我還沒有找到在 UWP 中獲得圖形 object 的方法。

就我所見,您只能通過組合平台提供的其他控件並設置它們的參數來在 UWP 中創建自定義控件。 但這對我的應用程序來說還不夠。 我需要一個圖形 object 來進行精確繪圖。 有沒有辦法在 UWP 中獲取此圖形 object? I know that with the MSIX package tool you can create an installer for the Windows Store from a Windows Forms app, but I would like my app to be directly in UWP.

Also, I know that in Android, even if it is also XAML based, it is possible to extend the View class in java and override the OnDraw method. Window UWP 中是否有等效的東西?

使用 Visual Studio,C# windows 窗體代碼示例為:

partial class MyCustomButton
{
    ...

    private void InitializeComponent()
    {           
        ...
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyCustomButton_Paint);
    }

 }

public partial class MyCustomButton : Button
{
    public MyCustomButton()
    {
        InitializeComponent();
    }

    private void MyCustomButton_Paint(object sender, PaintEventArgs e)
    {

        Graphics g = e.Graphics;

        ...

        /// Drawing on g

    }

}

即使是基於 XAML 的 UWL 也可以做類似的事情嗎? 可以得到顯卡 object 嗎? (我知道在 Android 中我是可能的)。


由於收到的評論,我將添加信息並嘗試更具體。 我們要更新的 C# Windows.Form 應用程序是一個用於繪制和編輯特定工程過程圖表的工具。 它在 UserControl class 的子類中計算點並繪制線,特別是在 Paint 事件的處理程序中。 當我更新到 UWP 時,我想重用所有這些計算和代碼,但在我看來,只有在圖形 object 可用於繪制時才能完成。 That's why I would like to know if this Graphic object can be obtained in UWP, and if it can't I would like some guidance on how to reuse all that drawing code that is already there in the C# Windows.Form version of the app . 感謝所有回復。

System.Drawing 命名空間中的 Graphics class 與 Win2D 的 CanvasDrawingSession class 類似。 通過將 Win2D NuGet package 添加到您的項目,Win2D 可用於 UWP 應用程序。

有一個類似的線程,你可以參考它。 我按照這個官方網站的代碼示例添加圖形,效果很好。 所以你可以嘗試使用 Win2D 庫來實現相同的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM