[英]How to load and bind a image contained in a portable library within an UWP app?
我正在做一个UWP应用,但是我很难加载和绑定一个图像到UWP应用,应该怎么做?
我目前的结构:
MyApp.Model:
|
|-Models
|-MyModel.cs
|-MyModelContainer.cs
|-Resources
|-image1.png
|-image2.png
|-image3.png
我的Xaml在另一个项目中(赢得10个通用应用程序,并引用此Portable类库。)
MyModelContainer
只是实例化IEnumerable<MyModel>
的单例容器。 这里是他们的内容:
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 = ???????
}
};
}
}
在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>^
我的问题:
BitmapImage
吗? Icon = new BitmapImage(new Uri("ms-appx://MyApp.Model/Resources/image1.png"))
没有任何运气,我没有加载任何图像(并且触发了ImageFailed事件)。 <Image/>
? 这是针对UWP(windows 10)应用程序,而不是WPF,不是win8。
非常感谢你。
编辑
这是文件夹结构
MyApp == AstroApp
MyApp.Model == AstroApp.Model
MyModel = AstroSign
MyModelContainer = AstroManager
如果您的图像与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"));
}
};
}
}
如果您的图像在另一个程序集中,请使用以下网址:
Icon = new BitmapImage(new Uri("ms-appx:///NameOfProjectWithImage/Resources/image1.png"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.