C# xamarin表单图像未显示
我有一个简单的应用程序,它有一个PicListPage(图片列表)。xaml和代码隐藏允许从Android仿真器库中拾取图片或拍照 所选图片(或新图片)不会显示在图像控件中。我无法通过网络搜索找到解决方案。此代码是从教程中复制的。在NewImage.Source行放置断点表明currentimage有一个非空值 任何帮助都将不胜感激。Xamarin女士论坛不允许我注册或登录,因此我不能在那里提问 吉姆·德宾 durbinjw@gmail.com 这是XAMLC# xamarin表单图像未显示,c#,image,xamarin,xamarin.forms,display,C#,Image,Xamarin,Xamarin.forms,Display,我有一个简单的应用程序,它有一个PicListPage(图片列表)。xaml和代码隐藏允许从Android仿真器库中拾取图片或拍照 所选图片(或新图片)不会显示在图像控件中。我无法通过网络搜索找到解决方案。此代码是从教程中复制的。在NewImage.Source行放置断点表明currentimage有一个非空值 任何帮助都将不胜感激。Xamarin女士论坛不允许我注册或登录,因此我不能在那里提问 吉姆·德宾 durbinjw@gmail.com 这是XAML <?xml versio
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:d="http://xamarin.com/schemas/2014/forms/design"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="MyStuff.PicListPage">
<ContentPage.ToolbarItems>
<ToolbarItem x:Name="selectImageButton"
Text="Select Image"
Command="{Binding SelectImageCommand}"/>
<ToolbarItem x:Name="takePicButton"
Text="Take Picture"
Command="{Binding TakePictureCommand}"
CommandParameter="{Binding NewImage}"/>
<ToolbarItem x:Name="deleteButton"
Text="Delete"
Command="{Binding DeletePictureCommand}"
CommandParameter="{Binding SelectedImage}"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
<StackLayout>
<Label Text="Picture List page"
HorizontalOptions="Center"
FontSize="Large"
FontAttributes="Bold"/>
<Frame BorderColor="Black"
WidthRequest="350"
HeightRequest="350"
Margin="10,0,10,0">
<FlexLayout Direction="Row">
**<Image x:Name="NewImage"**
IsVisible="true"
Aspect="AspectFit"
Source="{Binding NewImmage, Mode=TwoWay}"
Margin="10,0,10,0"
WidthRequest="350"
HeightRequest="350"
HorizontalOptions="CenterAndExpand"
VerticalOptions="CenterAndExpand">
</Image>
</FlexLayout>
</Frame>
</StackLayout>
</ContentPage.Content>
</ContentPage>
下面是ViewModel代码片段,它从模拟库中复制图像,其中有5张图片
using MyStuff.Model;
using MyStuff.ViewModel.Commands;
using Plugin.Media;
using Plugin.Media.Abstractions;
using System.Collections.ObjectModel;
using System.ComponentModel;
using Xamarin.Forms;
namespace MyStuff.ViewModel
{
public class PicListPageVM : INotifyPropertyChanged
{
public ObservableCollection<PicList> PicLists { get; set; }
public TakePictureCommand TakePictureCommand { get; set; }
public SelectImageCommand SelectImageCommand { get; set; }
public DeletePictureCommand DeletePictureCommand { get; set; }
private Image newimage;
public Image NewImage
{
get { return newimage; }
set
{
newimage = value;
OnPropertyChanged("NewImage");
}
}
public PicListPageVM()
{
SelectImageCommand = new SelectImageCommand(this);
TakePictureCommand = new TakePictureCommand(this);
DeletePictureCommand = new DeletePictureCommand(this);
NewImage = new Image();
NewImage.IsOpaque = false;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
public async void SelectImage()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsPickPhotoSupported)
{
await App.Current.MainPage.DisplayAlert("Information", "Can't select pictures on this device", "OK");
return;
}
var mediaoptions = new PickMediaOptions()
{
PhotoSize = PhotoSize.Small
};
var selectedImageFile = await CrossMedia.Current.PickPhotoAsync(mediaoptions);
if (selectedImageFile == null)
{
await App.Current.MainPage.DisplayAlert("Information", "No image selected.", "OK");
return;
}
**var currentimage = ImageSource.FromStream(() => selectedImageFile.GetStream());
NewImage.Source = currentimage;**
}
使用MyStuff.Model;
使用MyStuff.ViewModel.Commands;
使用Plugin.Media;
使用Plugin.Media.Abstractions;
使用System.Collections.ObjectModel;
使用系统组件模型;
使用Xamarin.Forms;
命名空间MyStuff.ViewModel
{
公共类PicListPageVM:INotifyPropertyChanged
{
公共ObservableCollection PicLists{get;set;}
公共TakePictureCommand TakePictureCommand{get;set;}
public SelectImageCommand SelectImageCommand{get;set;}
公共DeletePictureCommand DeletePictureCommand{get;set;}
私人形象;
公众形象新形象
{
获取{return newimage;}
设置
{
新图像=值;
OnPropertyChanged(“NewImage”);
}
}
公共PicListPageVM()
{
SelectImageCommand=新建SelectImageCommand(此命令);
TakePictureCommand=新的TakePictureCommand(此);
DeletePictureCommand=新的DeletePictureCommand(该命令);
NewImage=新图像();
NewImage.IsOpaque=false;
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
公共异步void SelectImage()
{
等待CrossMedia.Current.Initialize();
如果(!CrossMedia.Current.ISPickPhotosSupported)
{
等待App.Current.MainPage.DisplayAlert(“信息”,“无法在此设备上选择图片”,“确定”);
返回;
}
var mediapoptions=new pickmediapoptions()
{
PhotoSize=PhotoSize.Small
};
var selectedImageFile=wait CrossMedia.Current.PickPhotoAsync(mediapoptions);
如果(selectedImageFile==null)
{
等待App.Current.MainPage.DisplayAlert(“信息”,“未选择图像”,“确定”);
返回;
}
**var currentimage=ImageSource.FromStream(()=>selectedImageFile.GetStream());
NewImage.Source=currentimage**
}
首先,您在绑定中拼错了属性名称(“NewImage”)(TwoWay绑定在这里也没有意义)
最后,选择映像时,请将VM属性设置为映像路径
NewImage = selectedImageFile.AlbumPath;
这个问题已经解决。多亏了@Jason和进一步的黑客攻击。关键问题是1)绑定中的输入错误2)绑定需要指向具有数据类型字符串而不是图像UI元素的属性,图像UI元素反过来指向文件路径,3)将绑定字符串设置为指向selectedImageFile.path而不是selectedImageFile.AlbumPath.Hi@Jason.Thank.。首先,我在反复查看拼写错误后,对它视而不见。这是修复的,我完全删除了该模式。(正确吗?)第二,所选图像仍然没有显示。更改NewImage数据类型对于绑定来说似乎是合乎逻辑的。将字符串属性NewImage设置为selectedImageFile.AlbumPath是否应该导致图像本身显示?是否还需要设置源?可能代码缺少另一个设置?很抱歉太密集了。我对Xamarin比较陌生。嗨@Jason。玩了一会儿,它现在运行正常。必须将NewImage=selectedImageFile.AlbumPath;更改为NewImage=selectedImageFile.Path;@JWDurbin嗨,如果你已经解决了这个问题,记得在有时间时标记答案。这将有助于其他人了解解决方案。
<Image x:Name="NewImage" Source="{Binding NewImmage, Mode=TwoWay}" .. />
private string newimage;
public string NewImage
{
get { return newimage; }
set
{
newimage = value;
OnPropertyChanged("NewImage");
}
}
NewImage = selectedImageFile.AlbumPath;