2017-12-21 5 views
1

Ich versuche, ein Bild basierend auf Xamarin Formen zu erstellen Image, das mit einem Byte-Array gebunden werden kann.Erstellen von Xamarin Custom Image ohne Gerät Renderer

Ich möchte die ImageSource nicht direkt in meinem ModelView verwenden, da die ModelViews auch für ein WPF-UI verwendet werden. Also ich möchte nur das Byte-Array (erstellt aus einer Base64-Zeichenfolge) in ein Bild konvertieren, das von den UI's gebunden werden kann. Auf dem WPF-Ende gibt es kein Problem, da es ein Bild im Bytearray-Format aufnehmen kann. Jetzt möchte ich ein Custom-ImageControl für Xamarin Forms erstellen, das ein Byte-Array aufnehmen/binden und an eine ImageSource formatieren kann.

Auch ich möchte keinen Renderer für jede Plattform erstellen. Ich möchte den Standard-Renderer von Xamarin Image verwenden.

Wie Sie unten sehen können, habe ich ein benutzerdefiniertes Image (MVVMImage) erstellt und das von Xamarin bereitgestellte Basis-Image verwendet.

Das Problem ist, jedes Mal, wenn die MVVMImage i wird geladen erhalten: System.Reflection.TargetInvocationException: <Timeout exceeded getting exception details>

Bild Gewohnheit:

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)); 
       } 
      } 
     } 
    } 

HauptSeite 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> 

Mainpage-Code

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); 
     } 
    } 
} 

Modellansicht

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; } 
     } 
    } 
} 

Antwort

1

Ein Problem ist die Art und Weise, wie Sie Ihre bindbare Eigenschaft ändern.

Es sollte in Eigenschaft geändert werden nicht der Setter behandelt werden.

Es sollte wie folgt sein:

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

Vielen Dank, meine schlechte Bindung war. – Neuxz

+1

Ich bin froh, dass ich helfen konnte –

Verwandte Themen