[英]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.