如何在 Nativescript 中显示 base64 字符串并将其绑定到图像作为源?
How to show and bind base64 string to Image as a source in Nativescript?
我试图在我的视图中将 base64 图像显示并绑定为 ImageSource,但它根本没有显示。我在文档中找不到任何有用的信息..我做错了什么吗?
imageSource 是 属性 应该保存图像 src 数据..
视图如下:
<Page loaded = "loaded" xmlns = "http://schemas.nativescript.org/tns.xsd" >
<StackLayout>
<TextField hint = "String for encoding!" text = "{{ message }}" />
<Button tap = "{{ onGenerateQrTap }}" text = "Generate QR" class = "button" />
<Image src = "{{ imageSource }} " />
</StackLayout>
</Page>
这是视图背后的代码:
import { Page } from 'ui/page';
import { EventData } from 'data/observable';
import { QrGeneratorViewModel } from '../../ViewModels/QrGeneratorViewModel';
import { Button } from 'ui/button';
import { Image } from 'ui/image';
let viewModel = new QrGeneratorViewModel();
export function loaded(args: EventData) {
let page = <Page>args.object;
page.bindingContext = viewModel;
}
这是 ViewModel:
import { Observable } from 'data/observable';
import { QrGenerator } from '../Common/QrGenerator';
import { ImageSource } from "image-source";
export class QrGeneratorViewModel extends Observable {
private _message: string;
private _qrGenerator: QrGenerator.Generator;
private _imageBase64String: string;
private _imageSource: ImageSource;
constructor() {
super();
this._qrGenerator = new QrGenerator.Generator();
this._imageSource = new ImageSource();
}
get message() {
return this._message;
}
set message(newMessage: string) {
this._message = newMessage;
}
get imageSource(): ImageSource {
return this._imageSource;
}
public onGenerateQrTap(): void {
this._imageBase64String = this._qrGenerator.qr(this._message);
this._imageSource.loadFromBase64(this._imageBase64String);
}
}
快速浏览一下代码,您似乎没有设置 _imageSource
以使其更新。您在 ImageSource
实例上调用该方法,但没有设置 UI 绑定。我会尝试为 _imageSource
使用 setter 或使用 Observable:
设置它
this.set('imageSource', this._imageSource.loadFromBase64(this._imageBase64String);
我试图在我的视图中将 base64 图像显示并绑定为 ImageSource,但它根本没有显示。我在文档中找不到任何有用的信息..我做错了什么吗?
imageSource 是 属性 应该保存图像 src 数据..
视图如下:
<Page loaded = "loaded" xmlns = "http://schemas.nativescript.org/tns.xsd" >
<StackLayout>
<TextField hint = "String for encoding!" text = "{{ message }}" />
<Button tap = "{{ onGenerateQrTap }}" text = "Generate QR" class = "button" />
<Image src = "{{ imageSource }} " />
</StackLayout>
</Page>
这是视图背后的代码:
import { Page } from 'ui/page';
import { EventData } from 'data/observable';
import { QrGeneratorViewModel } from '../../ViewModels/QrGeneratorViewModel';
import { Button } from 'ui/button';
import { Image } from 'ui/image';
let viewModel = new QrGeneratorViewModel();
export function loaded(args: EventData) {
let page = <Page>args.object;
page.bindingContext = viewModel;
}
这是 ViewModel:
import { Observable } from 'data/observable';
import { QrGenerator } from '../Common/QrGenerator';
import { ImageSource } from "image-source";
export class QrGeneratorViewModel extends Observable {
private _message: string;
private _qrGenerator: QrGenerator.Generator;
private _imageBase64String: string;
private _imageSource: ImageSource;
constructor() {
super();
this._qrGenerator = new QrGenerator.Generator();
this._imageSource = new ImageSource();
}
get message() {
return this._message;
}
set message(newMessage: string) {
this._message = newMessage;
}
get imageSource(): ImageSource {
return this._imageSource;
}
public onGenerateQrTap(): void {
this._imageBase64String = this._qrGenerator.qr(this._message);
this._imageSource.loadFromBase64(this._imageBase64String);
}
}
快速浏览一下代码,您似乎没有设置 _imageSource
以使其更新。您在 ImageSource
实例上调用该方法,但没有设置 UI 绑定。我会尝试为 _imageSource
使用 setter 或使用 Observable:
this.set('imageSource', this._imageSource.loadFromBase64(this._imageBase64String);