2017-12-21 15 views
1

バイト配列でバインドできるイメージを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; } 
     } 
    } 
} 

答えて

1

バインド可能なプロパティの変更を処理する方法が1つあります。

セッターではなくプロパティで変更する必要があります。それはこのようなものでなければなりません

public static readonly BindableProperty ArraySourceProperty = 
    BindableProperty.Create(nameof(ArraySource), typeof(byte[]), typeof(MVVMImage), null, 
    BindingMode.OneWay, null, OnArraySourcePropertyChanged); 

public byte[] ArraySource 
{ 
    get { return (byte[])GetValue(ArraySourceProperty); } 
    set { SetValue(ArraySourceProperty, value); } 
} 

private static void OnArraySourcePropertyChanged(BindableObject bindable, object oldvalue, object newvalue) 
{ 
    var control = (MVVMImage) bindable; 
    if (control != null) 
    { 
     control.Source = ImageSource.FromStream(() => new MemoryStream((byte[])newvalue)); 
    } 
} 
+0

は私のバインディングが悪かった、ありがとうございました。 – Neuxz

+1

うれしい –

関連する問題