dymosoftware / DCD-SDK-Sample

DYMO Connect SDK Samples
Other
60 stars 26 forks source link

"Not strongly-typed" runtime error #55

Open tweissert opened 2 years ago

tweissert commented 2 years ago

I was successfully able to use code from the sample project in aspnet 4.6.1, but when I upgraded the project to 4.8 (and reinstalled the DYMO.Connect.SDK 1.4.3.37 via Github), the above mentioned debug error prevents running the program in wpf. The specific error says: System.IO.FileLoadException: 'Could not load file or assembly 'DymoSDK, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. A strongly-named assembly is required. (Exception from HRESULT: 0x80131044)' Below is the code from the offending file:

using DymoSDK.Implementations; using System.Collections.ObjectModel; using System.Collections.Generic; using System; using System.IO; using System.Linq; using System.Windows.Input; using System.Windows.Media.Imaging; using SkiaSharp; using SkiaSharp.QrCode;

namespace AISTechQR.ViewModels { public class MainViewModel : BaseViewModel {

region Commands

    private ICommand _generateQRCodeCommand;
    public ICommand GenerateQRCodeCommand
    {
        get { return _generateQRCodeCommand = _generateQRCodeCommand ?? new CommandHandler(() => GenerateQRCodeAction(), true); }
    }

    #endregion

    #region Props
    IEnumerable<DymoSDK.Interfaces.IPrinter> _printers;
    public IEnumerable<DymoSDK.Interfaces.IPrinter> Printers
    {
        get
        {
            if (_printers == null)
                _printers = new List<DymoSDK.Interfaces.IPrinter>();
            return _printers;
        }
        set
        {
            _printers = value;
            NotifyPropertyChanged("Printers");
        }
    }

    public int PrintersFound
    {
        get { return Printers.Count(); }
    }

    string _fileName;
    public string FileName
    {
        get
        {
            if (string.IsNullOrEmpty(_fileName))
                return "No file selected";

            return _fileName;
        }
        set
        {
            _fileName = value;
            NotifyPropertyChanged("FileName");
        }
    }

    private BitmapImage _imageSourcePreview;
    public BitmapImage ImageSourcePreview
    {
        get { return _imageSourcePreview; }
        set
        {
            _imageSourcePreview = value;
            NotifyPropertyChanged("ImageSourcePreview");
        }
    }

    List<DymoSDK.Interfaces.ILabelObject> _labelObjects;
    public List<DymoSDK.Interfaces.ILabelObject> LabelObjects
    {
        get
        {
            if (_labelObjects == null)
                _labelObjects = new List<DymoSDK.Interfaces.ILabelObject>();
            return _labelObjects;
        }
        set
        {
            _labelObjects = value;
            NotifyPropertyChanged("LabelObjects");
        }
    }

    private DymoSDK.Interfaces.ILabelObject _selectedLabelObject;
    public DymoSDK.Interfaces.ILabelObject SelectedLabelObject
    {
        get { return _selectedLabelObject; }
        set
        {
            _selectedLabelObject = value;
            NotifyPropertyChanged("SelectedLabelObject");
        }
    }

    private string _objectValue;
    public string ObjectValue
    {
        get { return _objectValue; }
        set
        {
            _objectValue = value;
            NotifyPropertyChanged("ObjectValue");
        }
    }

    DymoSDK.Interfaces.IPrinter _selectedPrinter;
    public DymoSDK.Interfaces.IPrinter SelectedPrinter
    {
        get { return _selectedPrinter; }
        set
        {
            _selectedPrinter = value;
            NotifyPropertyChanged("FileName");
        }
    }

    List<string> _twinTurboRolls;
    public List<string> TwinTurboRolls
    {
        get
        {
            if (_twinTurboRolls == null)
                _twinTurboRolls = new List<string>();
            return _twinTurboRolls;
        }
        set
        {
            _twinTurboRolls = value;
            NotifyPropertyChanged("TwinTurboRolls");
        }
    }

    private string _selectedRoll;
    public string SelectedRoll
    {
        get { return _selectedRoll; }
        set
        {
            _selectedRoll = value;
            NotifyPropertyChanged("SelectedRoll");
        }
    }

    #endregion

    readonly DymoLabel dymoSDKLabel = (DymoLabel)DymoLabel.Instance;

    public List<ViewAssetsModel> assets;
    public List<TypeModel> types;
    readonly DataAccess _da = new DataAccess();
    public FlexColumnModel FlexColumns;
    public string lblProperties;

    private ObservableCollection<ViewAssetsModel> selectedItems;
    public ObservableCollection<ViewAssetsModel> SelectedItems
    {
        get
        {
            if (selectedItems == null)
            {
                selectedItems = new ObservableCollection<ViewAssetsModel>();
            }
            return selectedItems;
        }
    }

    public MainViewModel()
    {
        DymoSDK.App.Init();
        dymoSDKLabel = (DymoLabel)DymoLabel.Instance;
        Printers = DymoPrinter.Instance.GetPrinters();
        types = _da.GetTypes();
    }

    public void UpdateAssets(int typeID)
    {
        assets = _da.GetAssetsByType(typeID);
        FlexColumns = _da.GetFlexColumnNamesbyType(typeID);
    }

    /// <summary>
    /// Load the preview image label in the  image control
    /// </summary>
    /// <param name="array">Preview image content</param>
    /// <returns>Bitmap of the content</returns>
    private BitmapImage LoadImage(byte[] array)
    {
        using (var ms = new System.IO.MemoryStream(array))
        {
            var image = new BitmapImage();
            image.BeginInit();
            image.CacheOption = BitmapCacheOption.OnLoad;
            image.StreamSource = ms;
            image.EndInit();
            return image;
        }
    }
    private void GenerateQRCodeAction()
    {
        foreach (ViewAssetsModel asset in SelectedItems)
        {
            DymoLabel label = GenerateLabel(asset);

            //Get image preview of the label
            label.GetPreviewLabel();
            //Load image preview in the control 
            ImageSourcePreview = LoadImage(label.Preview);

            int copies = 1;
            if (SelectedPrinter != null)
            {
                bool barcodeOrGraphsquality = true;

                //Send to print
                DymoPrinter.Instance.PrintLabel(label, SelectedPrinter.Name, copies, barcodeGraphsQuality: barcodeOrGraphsquality);
            }
        }
    }
    public DymoLabel GenerateLabel(ViewAssetsModel asset)
    {
        DymoLabel label = (DymoLabel)DymoLabel.Instance;

        int lblHeight = 424;  //375
        int lblWidth = (int)Math.Round(1.8 * lblHeight, 0);   //675

        // create the QR code
        var content = "https://tech.agnesirwin.org/history/" + asset.AssetID.ToString() + "/" + asset.TypeID.ToString();
        QRCodeGenerator generator = new QRCodeGenerator();
        var level = ECCLevel.H;
        var qr = generator.CreateQrCode(content, level);

        // create the bitmap canvas
        var info = new SKImageInfo(lblWidth, lblHeight);
        SKSurface surface = SKSurface.Create(info);
        SKCanvas canvas = surface.Canvas;
        canvas.Clear(SKColors.White);
        // add the qr code to the label
        canvas.Render(qr, lblHeight, lblHeight);

        // Add the AIS Logo
        string resourceID = "AIS.png";
        FileStream streamIn = File.OpenRead(resourceID);
        SKBitmap ais = SKBitmap.Decode(streamIn);
        canvas.DrawBitmap(ais, 160, 165); // 440, 20

        int toptextline = 88;
        int linespacing = 90;
        int textleft = 395;
        int len = 18;
        int linemarker = toptextline;

        SKPaint textPaint = new SKPaint { TextSize = 48, Color = SKColors.Black, IsAntialias = true };
        SKPaint textPaintLarge = new SKPaint { TextSize = 64, Color = SKColors.Black, IsAntialias = true };

        // create the "Label" line of text
        SKRect bounds = new SKRect();
        textPaintLarge.MeasureText(asset.Label, ref bounds);
        canvas.DrawText(asset.Label, textleft + (lblWidth / 2 - bounds.Width) / 2, linemarker, textPaintLarge);

        // Create the Serialnumber Line of text
        bounds = new SKRect();
        len = Math.Min(18, asset.SerialNumber.Length);
        textPaint.MeasureText(asset.SerialNumber.Substring(0, len), ref bounds);
        linemarker += linespacing;
        canvas.DrawText(asset.SerialNumber.Substring(0, len), textleft + (lblWidth / 2 - bounds.Width) / 2, linemarker, textPaint);

        // Create the client's name Line of text
        bounds = new SKRect();
        if (asset.LastEvent.Length > 13)
        {
            int fsx = asset.LastEvent.IndexOf(" ");
            string firstn = asset.LastEvent.Substring(0, fsx);
            string lastn = asset.LastEvent.Substring(fsx + 1, asset.LastEvent.Length - fsx - 1);
            textPaint.MeasureText(firstn, ref bounds);
            linemarker += linespacing;
            canvas.DrawText(firstn, textleft + (lblWidth / 2 - bounds.Width) / 2, linemarker, textPaint);
            textPaint.MeasureText(lastn, ref bounds);
            linemarker += linespacing / 2;
            canvas.DrawText(lastn, textleft + (lblWidth / 2 - bounds.Width) / 2, linemarker, textPaint);
        }
        else
        {
            len = Math.Min(16, asset.LastEvent.Length);
            textPaint.MeasureText(asset.LastEvent.Substring(0, len), ref bounds);
            linemarker += linespacing;
            canvas.DrawText(asset.LastEvent.Substring(0, len), textleft + (lblWidth / 2 - bounds.Width) / 2, linemarker, textPaint);
        }

        // Create the fourth line used for flex1 (chromebook slots)
        bounds = new SKRect();
        if (!string.IsNullOrEmpty(asset.FlexColumn1))
        {
            len = Math.Min(18, asset.FlexColumn1.Length);
            textPaint.MeasureText(asset.FlexColumn1.Substring(0, len), ref bounds);
            linemarker += linespacing;
            canvas.DrawText(asset.FlexColumn1.Substring(0, len), textleft + (lblWidth / 2 - bounds.Width) / 2, linemarker, textPaint);
        }
        SKImage image = surface.Snapshot();
        SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
        byte[] ImageData = data.ToArray();
        string labelgraphic = Convert.ToBase64String(ImageData);

        //Load default label from file path
        FileName = "AIS.Label";
        label.LoadLabelFromFilePath(FileName);

        //Get object names list
        LabelObjects = label.GetLabelObjects().ToList();
        // insert new graphic
        label.SetImageFromBase64(LabelObjects[0].Name, labelgraphic);

        return label;
    }
}

}