0

我有基于 PCL 的 Xamarin 解决方案。我需要做的是将图像加载到应用程序项目中的视图中。这些图像位于另一个 PCL 项目中。

基本上我有一些 GridView xamarin 视图。此视图应显示类似“卡片”的内容。所有数据都加载得很好,但图像。老实说,我尝试了所有我能想到的东西——绝对路径、嵌入式资源、相对路径、windows ms-appx/// 前缀。我没有尝试只对我不太有用的流。只有网络资源有效。

我的代码:

[assembly: ExportRenderer(typeof(GridViewRoles), typeof(GridViewRolesRenderer))]
namespace StrangerParty.Windows.Renderers
{
    public class GridViewRolesRenderer : ViewRenderer<GridViewRoles, GridView>
    {
        private GridView gridView;

        protected override void OnElementChanged(ElementChangedEventArgs<GridViewRoles> e)
        {
            base.OnElementChanged(e);

            if (this.Control == null)
            {
                this.gridView = new GridView();

                StringBuilder sb = new StringBuilder();
                sb.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">");
                sb.Append("<Grid Height=\"100\" Width=\"100\" >"); /*Background =\"#FF2E2E2E\"*/
                sb.Append("<Image x:Name=\"ImageRoleImage\" Stretch=\"UniformToFill\" Source=\"sampleForTesting.png\"/>");
                sb.Append("</Grid>");
                sb.Append("</DataTemplate>");

                DataTemplate datatemplate = (DataTemplate)XamlReader.Load(sb.ToString());

                this.gridView.ItemTemplate = datatemplate;

                SetNativeControl(this.gridView);
            }

            this.gridView.ItemsSource = Core.Instance.GameHandler.Game.Players;
        }
    }    
}

感谢您的帮助

4

1 回答 1

1

您可以PCL使用Assembly.GetManifestResourceStream. 根据您的要求,您需要在Converter后面使用类似的代码。

隐蔽者

public object Convert(object value, Type targetType, object parameter, string language)
{
    var temp = new BitmapImage();
    var assembly = typeof(ImageTest.App).GetTypeInfo().Assembly;
    Stream stream = assembly.GetManifestResourceStream(value as string);
    if (stream != null)
    {
        using (var memStream = new MemoryStream())
        {
            stream.CopyTo(memStream);
            memStream.Position = 0;
            temp.SetSource(memStream.AsRandomAccessStream());
        }
    }
    return temp;
}

下一步是创建DataTemplate,利用converter数据模板无法加载XamlLoader。所以你可以<ResourceDictionary>像下面的代码一样为数据模板创建。

....
 xmlns:local="using:ImageTest.UWP"
    RequestedTheme="Light">
    <Application.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="MyDataTemplate">
                <Grid >
                    <Grid.Resources>
                        <local:DateToStringConverter x:Key="MyConvert"/>
                    </Grid.Resources>
                    <Image Name="MyImage" Source="{Binding imageSource , Mode=OneWay, Converter={StaticResource MyConvert}}" />
                </Grid>
            </DataTemplate>
        </ResourceDictionary>
    </Application.Resources>

主页.xaml

 <GridView Name="MyGridView" ItemTemplate="{StaticResource MyDataTemplate}"/>

MainPage.xaml.cs

public MainViewModel viewModel { get; set; }
  public MainPage()
  {
      this.InitializeComponent();      
      viewModel = this.DataContext as MainViewModel;
      viewModel.items.Add(new Model() { imageSource = "ImageTest.hello.jpg" });
      viewModel.items.Add(new Model() { imageSource= "ImageTest.hello.jpg" });
          MyGridView.ItemsSource = viewModel.items;
  }

因此,您可以使用相同的方式DataTemplate通过加载<Application.Resources>就像后面的代码来创建您的。

var ItemTemplate = App.Current.Resources["MyDataTemplate"] as Windows.UI.Xaml.DataTemplate

如果它不起作用。请试一试我的演示

于 2017-01-17T02:03:40.340 回答