MVVM:来自FileOpenPicker的图像绑定源

时间:2015-01-22 22:16:34

标签: c# wpf xaml mvvm windows-phone-8.1

我将OnActivated()添加到app.xaml.cs中,它正常工作:

protected async override void OnActivated(IActivatedEventArgs args)
        {
            var continuationEventArgs = args as IContinuationActivatedEventArgs;
            if (continuationEventArgs != null)
            {
                switch (continuationEventArgs.Kind)
                {
                    case ActivationKind.PickFileContinuation:
                        FileOpenPickerContinuationEventArgs arguments = continuationEventArgs as FileOpenPickerContinuationEventArgs;
                        string passedData = (string)arguments.ContinuationData["keyParameter"];
                        StorageFile file = arguments.Files.FirstOrDefault(); // your picked file
                        addNewPlaceViewModel.OnFilesPicked(file);
                        // do what you want
                        break;
                }
            }
        }

我已经将FileOpenPicker正确地挂钩到MVVM项目中。这是我的代码:

private static readonly IEnumerable<string> SupportedImageFileTypes = new List<string> { ".jpeg", ".jpg", ".png" };
    public AddNewPlaceViewModel(INavigationService navigationService)
    {
        this.navigationService = navigationService;
    }
    private async void OnFilesPicked(IStorageFile file)
    {

            if (file != null)
            {
                var bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(await file.OpenReadAsync());
                Picture = bitmapImage;
                //IN debugger in picture I have sht but in xaml i cannot show this.
            }
        }
    }
    private static void TriggerPicker(IEnumerable<string> fileTypeFilers, bool shouldPickMultiple = false)
    {
        var fop = new FileOpenPicker();
        foreach (var fileType in fileTypeFilers)
        {
            fop.FileTypeFilter.Add(fileType);
        }
        if (shouldPickMultiple)
        {
            fop.PickMultipleFilesAndContinue();
        }
        else
        {
            fop.PickSingleFileAndContinue();
        }
    }

这是Picture = bitmapImage; enter image description here之后的情况 我还设置了Binding和ICommand:

public ICommand UpdatePictureCommand
        {
            get { return new RelayCommand(o => TriggerPicker(SupportedImageFileTypes)); }
        }
private ImageSource _Picture;
        public ImageSource Picture
        {
            get
            {
                return _Picture;
            }
            set
            {
                _Picture = value;
                OnPropertyChanged("Picture");
            }
        }

当我想要显示我拍摄的照片时,这是我在枢轴项目(按钮和图像)中的XAML。

<Button Grid.Row ="4" 
                    Content="Dodaj zdjęcie" 
                    HorizontalAlignment="Center"
                    Command="{Binding UpdatePictureCommand}"/>
<Image Grid.Row="6"
                Width="192" 
                Height="192" 
                Source="{Binding Picture, Mode=TwoWay}"
                />

文件打开选择器工作正常(我可以选择或拍照)但之后我无法在我的XAML中看到选择/拍摄的照片。该代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

你可以创建一个像这样的转换器

[ValueConversion(typeof(Image), typeof(System.Windows.Media.ImageSource))]
    public class ImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            var bitmap = (Bitmap)value;

            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
                bitmap.GetHbitmap(),
                IntPtr.Zero,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());
        }


        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }