简体   繁体   中英

WP7 - How to bind the Bitmap from ViewModel?

I have bit map in View Model. Now I want to bind to the XAML from View Model.

 public static String _imgQRCode;
        public String imgQRCode
        {
            get { return _imgQRCode; }
            set { this.RaiseAndSetIfChanged(x => x.imgQRCode, value); }
        }

Bit Map:-

imgQRCode = GenerateQRCode(phoneNumber).ToString();

GenerateORCode:-

private static WriteableBitmap GenerateQRCode(string phoneNumber)
        {
            BarcodeWriter _writer = new BarcodeWriter();

            _writer.Renderer = new ZXing.Rendering.WriteableBitmapRenderer()
            {
                Foreground = System.Windows.Media.Color.FromArgb(255, 0, 0, 255),
            };

            _writer.Format = BarcodeFormat.QR_CODE;

            _writer.Options.Height = 400;
            _writer.Options.Width = 400;
            _writer.Options.Margin = 1;

            var barcodeImage = _writer.Write("tel:" + phoneNumber);
            return barcodeImage;
        }

Here i can not bind the image. Please let me any idea to bind the image from viewModel. Thanks in advance.

imgQRCode的数据类型从string更改为WriteableBitmap

You can not bind the image to string. So long as datacontext is correct for your tag, you should define a property for the bitmap, eg

WriteableBitmap QRCode { get; set; } // Implement INotifyPropertyChanged the way you do it

Then have QRCode set in your other property setter, like so:

public String imgQRCode
        {
            get { return _imgQRCode; }
            set 
            { 
              this.RaiseAndSetIfChanged(x => x.imgQRCode, value); 
              this.QRCode = GenerateQRCode(value);
            }
        }

then in XAML you can do <Image Source="{Binding Path=QRCode}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM