[บทเรียฺน,B4XCustomView] การอ้างอิงรูปทางเมนู Files Manager Tab แล้วเพิ่มเข้าไป Properties Dialog ให้แก่ CustomView

Theera

Expert
Licensed User
Longtime User
ขอขอบคุณ Sagenut ที่แบ่งปันความรู้ เทคนิคการเขียนโค้ด ณ ที่นี่ด้วย

เราจะเริ่มออกแบบ B4XCustomView ด้วย Class Module

ตัวอย่าง
B4X:
#DesignerProperty: Key: GraphicImage, DisplayName: <คำอธิบายภาษาไทย หรืออังกฤษ> (FileName), FieldType: String, DefaultValue: Img.png, Description: ชื่อไฟล์รูปภาพ(ใส่ใน DirAssets)

Sub Class_Globals
    :::::::
    Private mProps As Map
    ::::::
End Sub

Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
     ::::::
    mProps=Props   'กรณี การออกแบบไม่อยู่ภายในโมดูล DesignerCreateView จำเป็นต้องสร้าง mProps ขึ้นมาและประกาศตัวแปรในโมดูล Class_Global
    :::::::
    DoCustomViewModule  'โมดูลนี้ต้องทำการเรียกใช้งาน mProps
End Sub

Private Sub DoCustomViewModule
     ::::::::
    Dim imgFile As String = mProps.GetDefault("GraphicImage", "Img.png")  'ดึงข้อมูลรูปภาพ จาก ค่าตายตัว(Default Value) จาก Property ของ GraphicImage
 
    'กรณีผู้ใช้งานใช้ไฟล์รูปภาพอื่นให้ดึงข้อมูลรูปภาพใหม่จาก  Property ของ GraphicImage
    If imgFile <> "Img.png" Then
        Dim bmpFile As B4XBitmap = xui.LoadBitmapResize(File.DirAssets, mProps.Get("GraphicImage"), 50dip, 50dip, True)
    End If
    ::::::::
End Sub

' Graphic  Image Property Setter
Public Sub setGraphicImage(ImageFileInDirAssets As String)
    Try
        If File.Exists(File.DirAssets, ImageFileInDirAssets) Then
            bmpSurface = xui.LoadBitmapResize(File.DirAssets, ImageFileInDirAssets, 200, 200, True)
            Draw
        End If
    Catch
        Log("Error loading surface image: " & ImageFileInDirAssets)
    End Try
End Sub

' No Code for getGraphicImage Module

ศึกษาเพิ่มเติม
 
Last edited:
Top