【发布时间】:2016-04-18 20:02:53
【问题描述】:
我正在做一个 UWP 应用,我很难用 UWP 应用加载和绑定图像,应该怎么做?
我目前的结构:
MyApp.Model:
|
|-Models
|-MyModel.cs
|-MyModelContainer.cs
|-Resources
|-image1.png
|-image2.png
|-image3.png
我的 Xaml 在另一个项目中(win 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
【问题讨论】:
-
试试这个
-
@LovetoCode ImageUrl是如何创建的?是String还是URI,是什么格式?
-
它在字符串中。或者只是将 url 提供给 Source 属性。 BitmapImage 将自动创建。
。让 Icon 为 "ms-appx:///MyApp.Model/Resources/image1.png" -
@LovetoCode 以及图像的构建操作应该是什么?
-
这对我有用 Source="ms-appx:///ClassLibrary2/Resources/image.png" 其中 ClassLibrary2 是不同的项目。您是否将模型项目引用添加到您的主项目。现在我只能想到这是问题
标签: c# xaml win-universal-app uwp-xaml