我在互联网上搜索了很多但似乎Google没有答案..
那么如何从我的UWP项目部分访问嵌入式图像(在PCL部分中)?
答案 0 :(得分:1)
您可以使用Assembly.GetCallingAssembly()
从“本机”平台程序集访问基于PCL的程序集,从而访问其嵌入资源。
依赖接口:
public interface IResource
{
bool CheckEmbeddedAccess(string imageName);
}
平台实施:
public bool CheckEmbeddedAccess(string imageName)
{
var callingAssembly = Assembly.GetCallingAssembly();
System.IO.Stream file = callingAssembly.GetManifestResourceStream(imageName);
if (file != null)
{
// do something with stream
return true;
}
return false;
}
PCL的用法:
var exists = DependencyService.Get<IResource>().CheckEmbeddedAccess("ImageResource.Face.png");
注意:确保使用正确的“资源ID”,因为默认情况下,当您将资源添加到项目时,资源会附加程序集名称(点表示法)。要查看所有实际资源ID,请在依赖服务实现中使用它来迭代它们:
foreach (string resourceName in callingAssembly.GetManifestResourceNames())
{
Console.WriteLine(resourceName);
}
答案 1 :(得分:0)
感谢@SushiHangover!
我正在处理嵌入式图片,但由于它是渲染器,因此我将a and not b
放入我的对象中。 Si我确实喜欢它并最终有效:)
SourcePath
然后我用这部分
处理它using ImageCircleProject.CustomControl;
using ImageCircleProject.UWP.CustomRenderer;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.ApplicationModel;
using Windows.Storage;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Shapes;
using Xamarin.Forms;
using Xamarin.Forms.Platform.UWP;
[assembly: ExportRenderer(typeof(CustomImageCircle), typeof(CustomImageCircleRenderer))]
namespace ImageCircleProject.UWP.CustomRenderer
{
public class CustomImageCircleRenderer : ViewRenderer<CustomImageCircle, Ellipse>
{
private CustomImageCircle customImageCircle;
protected override void OnElementChanged(ElementChangedEventArgs<CustomImageCircle> e)
{
base.OnElementChanged(e);
if (e.OldElement != null)
{
}
if (e.NewElement != null)
{
customImageCircle = e.NewElement as CustomImageCircle;
Ellipse ellipse = new Ellipse();
SetNativeControl(ellipse);
}
}
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected async override void OnElementPropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (Control != null)
{
var min = Math.Min(Element.Width, Element.Height) / 2.0f;
if (min <= 0)
return;
double radius;
if (Element.Width < Element.Height)
{
radius = Element.Width;
}
else
{
radius = Element.Height;
}
Control.Width = radius;
Control.Height = radius;
// That will be our fallback fill if can't make sense of the ImageSource.
Control.Fill = new SolidColorBrush(Windows.UI.Color.FromArgb(255, 52, 152, 219));
BitmapImage bitmapImage = null;
// Handle file images
if (Element.Source is FileImageSource)
{
FileImageSource fi = Element.Source as FileImageSource;
string myFile = System.IO.Path.Combine(Package.Current.InstalledLocation.Path, fi.File);
StorageFolder myFolder = await StorageFolder.GetFolderFromPathAsync(System.IO.Path.GetDirectoryName(myFile));
using (Stream s = await myFolder.OpenStreamForReadAsync(System.IO.Path.GetFileName(myFile)))
{
var memStream = new MemoryStream();
await s.CopyToAsync(memStream);
memStream.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.SetSource(memStream.AsRandomAccessStream());
}
}
// handle embedded images
else if (Element.Source is StreamImageSource)
{
using (Stream s = await GetStreamFromImageSourceAsync(Element.Source as StreamImageSource))
{
var memStream = new MemoryStream();
await s.CopyToAsync(memStream);
memStream.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.SetSource(memStream.AsRandomAccessStream());
}
}
// Handle uri images
else if (Element.Source is UriImageSource)
{
bitmapImage = new BitmapImage((Element.Source as UriImageSource).Uri);
}
if (bitmapImage != null)
Control.Fill = new ImageBrush() { ImageSource = bitmapImage };
}
}
private static async Task<Stream> GetStreamFromImageSourceAsync(StreamImageSource imageSource, CancellationToken cancellationToken = default(CancellationToken))
{
if (imageSource.Stream != null)
{
return await imageSource.Stream(cancellationToken);
}
return null;
}
}
}
因为// handle embedded images
else if (Element.Source is StreamImageSource)
{
using (Stream s = await GetStreamFromImageSourceAsync(Element.Source as StreamImageSource))
{
var memStream = new MemoryStream();
await s.CopyToAsync(memStream);
memStream.Position = 0;
bitmapImage = new BitmapImage();
bitmapImage.SetSource(memStream.AsRandomAccessStream());
}
}
已经存在,我用它来重新创建图像