[英]How to load and bind a image contained in a portable library within an UWP app?
I'm doing an UWP app, and I'm having an hard time to load and bind an image with an UWP app, how should it be done? 我正在做一个UWP应用,但是我很难加载和绑定一个图像到UWP应用,应该怎么做?
My current structure: 我目前的结构:
MyApp.Model:
|
|-Models
|-MyModel.cs
|-MyModelContainer.cs
|-Resources
|-image1.png
|-image2.png
|-image3.png
My Xaml is in another project(win 10 universal app and reference this Portable class library.) 我的Xaml在另一个项目中(赢得10个通用应用程序,并引用此Portable类库。)
MyModelContainer
is only a singleton container that instantiate a IEnumerable<MyModel>
. MyModelContainer
只是实例化IEnumerable<MyModel>
的单例容器。 Here are their content: 这里是他们的内容:
public class MyModel{
public String Name{get;set;}
public ??????? Icon {get;set;}
}
public static class MyModelContainer{
private static IEnumerable<MyModel> _myModelList;
public static IEnumerable<MyModel> MyModelList{get{
if(_myModelList==null){
Initialize();
}
return _myModelList;
}}
private static Initialize(){
_myModelList = new List<MyModel>() {
new MyModel(){
Name = "Model one"
Icon = ???????
}
};
}
}
At some place in my XAML I receive a list of MyModel
, in a ItemsControl
: 在XAML的某个地方,我在
ItemsControl
收到MyModel
的列表:
<ItemsControl ItemsSource="{Binding MyModelListProperty}" >
<ItemsControl.ItemsPanel >
<ItemsPanelTemplate>
<StackPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate >
<Button Margin="10" MinHeight="50" MinWidth="40" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Image Source="{Binding Icon}" ></Image>
<TextBlock Grid.Row="1" Text="{Binding Name}" ></TextBlock>
</Grid>
</Button>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>^
My questions: 我的问题:
BitmapImage
? BitmapImage
吗? Icon = new BitmapImage(new Uri("ms-appx://MyApp.Model/Resources/image1.png"))
without any luck, I have no image loaded(and the ImageFailed event is triggered). Icon = new BitmapImage(new Uri("ms-appx://MyApp.Model/Resources/image1.png"))
没有任何运气,我没有加载任何图像(并且触发了ImageFailed事件)。 <Image/>
? <Image/>
? This is for an UWP(windows 10) app, not WPF, not win8. 这是针对UWP(windows 10)应用程序,而不是WPF,不是win8。
Thank you very much. 非常感谢你。
EDIT 编辑
Here is the folder structure 这是文件夹结构
MyApp == AstroApp
MyApp.Model == AstroApp.Model
MyModel = AstroSign
MyModelContainer = AstroManager
If your image is in the same project as your MyModelContainer, this should work: 如果您的图像与MyModelContainer在同一项目中,则此方法应该起作用:
public class MyModel{
public String Name{get;set;}
public ImageSource Icon {get;set;}
}
public static class MyModelContainer{
private static IEnumerable<MyModel> _myModelList;
public static IEnumerable<MyModel> MyModelList{get{
if(_myModelList==null){
Initialize();
}
return _myModelList;
}}
private static Initialize(){
_myModelList = new List<MyModel>() {
new MyModel(){
Name = "Model one"
Icon = new BitmapImage(new Uri("ms-appx:///Resources/image1.png"));
}
};
}
}
If your image is in another assembly, use this url: 如果您的图像在另一个程序集中,请使用以下网址:
Icon = new BitmapImage(new Uri("ms-appx:///NameOfProjectWithImage/Resources/image1.png"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.