【问题标题】:MVVM: Image Bind Source from FileOpenPickerMVVM:来自 FileOpenPicker 的图像绑定源
【发布时间】:2015-03-21 21:19:26
【问题描述】:

我将 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;之后的情况 我还设置了 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 中看到选择/拍摄的照片。该代码出了什么问题?

【问题讨论】:

  • OnFilesPicked 何时以及如何被解雇?还有什么是 Image 它是您的控件的有效名称吗?
  • 你在某种程度上是using OnActivated event,因此你正在使用..AndContinue()
  • 我更新了代码。我认为我在 viewmodel 中有 OnFilesPicked() 是错误的。
  • 我想做你在那个链接中所做的事情。但我想在 ViewModel 的 MVVM 中执行此操作并将图像绑定到视图。
  • 看看at MSDN 和 interfece IContiuable... 的实现 - 也许你可以在你的 Page/ViewModel 中使用它,然后通过传递来触发你的 OnFilesPicked 方法来自 OnActiveted 事件的参数。

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


【解决方案1】:

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

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

【讨论】:

  • 好吧,但是我必须在哪里创建该转换器...我在 mvvm 中构建应用程序,我可以在代码后面添加 fileopenpicker 吗?
  • 你能谈谈新情况吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-23
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多