バイト配列でバインドできるイメージをXamarinフォームに基づいて作成しようとしています。デバイスなしでXamarinカスタムイメージを作成するレンダラー
ModelViewはWPF UIにも使用されているため、ModelSourceで直接ImageSourceを使用したくありません。 私はバイト配列(base64文字列から作成された)をUIにバインドできるイメージに変換したいだけです。 WPFの終わりでは、それはbytearray形式でイメージを取ることができるので問題はありません。今私は、バイト配列を受け取り/バインドしてImageSourceにフォーマットすることができるXamarin Forms用のCustom-ImageControllを作成したいと考えています。
また、私は各プラットフォームのレンダラーを作成したくありません。私はXamarin Imageから標準のレンダラーを使用したい。
以下に示すように、私はカスタムイメージ(MVVMImage)を作成し、Xamarinが提供する基本イメージを使用しました。
問題はMVVMImage iが取得ロードされるたびに、次のとおりです。System.Reflection.TargetInvocationException: <Timeout exceeded getting exception details>
カスタムイメージ:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Testape
{
public class MVVMImage : Image
{
public MVVMImage() : base()
{
//Attempt to avoid of create renderer on every device.
}
public static readonly BindableProperty ArraySourceProperty =
BindableProperty.Create(nameof(ArraySource), typeof(byte[]), typeof(MVVMImage), "");
public byte[] ArraySource
{
set
{
this.Source = ImageSource.FromStream(() => new MemoryStream(value));
}
}
}
}
メインページXAML
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:Testape;assembly=Testape"
x:Class="Testape.MainPage">
<local:MVVMImage ArraySource="{Binding Img}"
VerticalOptions="Center"
HorizontalOptions="Center"/>
</ContentPage>
メインページのコード
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Testape
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
byte[] bt = Convert.FromBase64String("iVBORw0KGgoAAA< And so on >AAAAAElFTkSuQmCC");
BindingContext = new ViewModelMainw(bt);
}
}
}
モデルビュー
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace Testape
{
public class ViewModelMainw
{
byte[] bffapt;
public ViewModelMainw(byte[] bff)
{
bffapt = bff;
}
public byte[] Img
{
get { return bffapt; }
}
}
}
は私のバインディングが悪かった、ありがとうございました。 – Neuxz
うれしい –