我使用Xam.Plugins.Forms.Svg来支持svg图像。我需要更改图像,所以我尝试了:
xaml中绑定:
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
fileUri = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private void previewSavedImage() {
try {
// hide video preview
videoPreview.setVisibility(View.GONE);
imgPreview.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 2;
String path = fileUri.getPath();
//File img_file = new File(getFilesDir(), path);
final Bitmap bitmap = BitmapFactory.decodeFile(path, options);
imgPreview.setImageBitmap(bitmap);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
直接从C#更改它:
<abstractions:SvgImage
SvgPath="{Binding CurrentWeather.IconSource, StringFormat='Umbrella.Images.{0:F0}.svg'}"
....
/>
SvgPath属性确实发生了变化,但图像没有重新渲染。
有没有解决方案?
答案 0 :(得分:2)
渲染渲染器的方式是问题,您需要创建新 SvgImage
对象并替换页面中的当前内容渲染器的工作方式,以便从新的SVG文件更新静态Image
。
您可以通过覆盖OnElementPropertyChanged
事件来修补渲染器并回复SvgPath
PropertyName更改:
iOS示例:
protected override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == "SvgPath")
{
var svgStream = _formsControl.SvgAssembly.GetManifestResourceStream(_formsControl.SvgPath);
var reader = new SvgReader(new StreamReader(svgStream), new StylesParser(new ValuesParser()), new ValuesParser());
var graphics = reader.Graphic;
var canvas = new ApplePlatform().CreateImageCanvas(graphics.Size);
graphics.Draw(canvas);
var image = canvas.GetImage();
var uiImage = image.GetUIImage();
Control.Image = uiImage;
}
}