How do I work with images in a portable class library targeting Windows Store Apps and WP7,WP8,WPF?

后端 未结 7 1040
忘掉有多难
忘掉有多难 2020-12-31 00:23

I am working on a first PCL that targets : WSA (Windows Store Application), WPF,WP7,WP8. We can say that it is a rolerdex kind of application, you have contacts , they have

相关标签:
7条回答
  • 2020-12-31 01:16

    For a lot of cases, images are platform-specific. They need to cater for size and DPI of the device itself, and would need to fit in with the look and feel of the application. For these situations, I would have the View itself decide what images to show to the user, probably based on some sort of state/mode provided by the ViewModel.

    However, these are cases where the images need to come from the ViewModel, for example, in the case of the sender thumbnails that get displayed in mail applications. In these cases, I have the ViewModel return some sort of a platform-agnostic concept of an image (such as byte[]), and then have the platform-specific projects convert that into something that their UI stack understands (in XAML, this would be a ImageSource).

    The code would look something like this:

    Portable project:

    using System.IO;
    using System.Reflection;
    
    namespace Portable
    {
        public class ViewModel
        {
            private byte[] _image = LoadFromResource("Image.png");
    
            public byte[] Image
            {
                get { return _image; }
            }
    
            private static byte[] LoadFromResource(string name)
            {
                using (Stream stream = typeof(ViewModel).GetTypeInfo().Assembly.GetManifestResourceStream("Portable." + name))
                {
                    MemoryStream buffer = new MemoryStream();
                    stream.CopyTo(buffer);
    
                    return buffer.ToArray();
                }
            }
        }
    }
    

    Note: You will need to remove or add GetTypeInfo() depending on the platforms you are targeting.

    Here we're reading from an embedded resource (Properties -> Build Action -> Embedded Resource), but you could imagine this coming from the network, or somewhere else.

    Windows Store app project: In the Windows Store app, you would have a value converter to convert from byte[] -> ImageSource:

    using System;
    using System.IO;
    using Windows.Storage.Streams;
    using Windows.UI.Xaml.Data;
    using Windows.UI.Xaml.Media.Imaging;
    
    namespace App
    {
        public class ByteToImageSourceValueConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, string language)
            {
                InMemoryRandomAccessStream s = new InMemoryRandomAccessStream();
    
                byte[] bytes = (byte[])value;
                Stream stream = s.AsStreamForWrite();
                stream.Write(bytes, 0, bytes.Length);
                stream.Flush();
                stream.Seek(0, SeekOrigin.Begin);
    
    
                BitmapImage source = new BitmapImage();
                source.SetSource(s);
    
                return source;           
    
            }
    
            public object ConvertBack(object value, Type targetType, object parameter, string language)
            {
                throw new NotImplementedException();
            }
        }
    }
    

    In the code behind of the View, set the DataContext:

    DataContext = new ViewModel();
    

    Then in the View itself binding to the ViewModel.Image property, and set the converter:

    <Page.Resources>
        <local:ByteToImageSourceValueConverter x:Name="ImageConverter"/>
    </Page.Resources>
    
    <Grid >
        <Image HorizontalAlignment="Left" Height="242" Margin="77,10,0,0" VerticalAlignment="Top" Width="278" Source="{Binding Image, Converter={StaticResource ImageConverter}}"/>
    
    </Grid>
    
    0 讨论(0)
提交回复
热议问题