MVVM:来自 FileOpenPicker 的图像绑定源 [英] MVVM: Image Bind Source from FileOpenPicker
问题描述
我将 OnActivated() 添加到 app.xaml.cs 中,它可以正常工作:
I added the OnActivated() into app.xaml.cs it is work correctly:
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 项目中.这是我的代码:
I hooked already FileOpenPicker into MVVM project correctly. This is my code:
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;
之后的情况我还设置了绑定和 ICommand:
This is situation after Picture = bitmapImage;
I have also set up Binding and 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.
And this is my XAML in pivot item(button and Image) when I want to show photo which I have taken.
<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 中看到选择/拍摄的照片.该代码有什么问题?
A file open picker is working correctly(I can choose or take a photo) but after that I cannot see choosed/taked photo in my XAML. What is going wrong with that code?
推荐答案
你可以创建一个类似这样的转换器
you can create a converter something like this
[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 的图像绑定源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!