簡體   English   中英

MVVM:來自FileOpenPicker的圖像綁定源

[英]MVVM: Image Bind Source from FileOpenPicker

我將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;之后的情況Picture = bitmapImage; 在此處輸入圖片說明 我還設置了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中看到已選擇/拍攝的照片。 該代碼出了什么問題?

您可以創建一個類似這樣的轉換器

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM