Xamarin表单图像未在列表视图中显示

时间:2018-03-29 21:59:01

标签: c# android image xamarin xamarin.forms

我想在列表视图中显示/!\图标。不幸的是,它并没有出现在任何地方。

我100%肯定它在iOS和Android应用中的正确位置。我已经仔细检查过它是一个Android资源(如下面的截图所示) enter image description here

用于初始化图片的代码:alarmIcon = new Image { Source = "warning.png" };

初始化后,我将它放在一个AlarmPageItem对象中,该对象作为Image属性。这会添加到列表视图中。 我已经检查过我将绑定与AlarmPageItem属性进行了匹配。 (文字确实显示出来了。)

我不知道为什么它不起作用......

我尝试过的事情

  1. 检查过图像和来源声明的案例
  2. 检查构建操作并复制到输出目录
  3. 检查绑定匹配
  4. 尝试建立手机而不是xamarin现场播放器
  5. 检查图像位置
  6. 代码:

    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="OSIApp.Pages.AlarmPage"
             Title="Alarm Log">
    <ContentPage.Content>
        <StackLayout>
            <ListView x:Name="listView" 
                      ItemTapped="OnItemTapped" >
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Orientation="Horizontal">
                                <StackLayout>
                                    <Label x:Name="alarmTimeLabel"
                                           HorizontalOptions="Center"
                                           HorizontalTextAlignment="Center"
                                           Text="{Binding DateTime}"/>
                                    <Image x:Name="alarmIconImage" 
                                           Source="{Binding AlarmImage}"/>
                                </StackLayout>
                                <Label x:Name="alarmTextLabel"
                                       FontSize="14"
                                       VerticalOptions="FillAndExpand"
                                       VerticalTextAlignment="Center"
                                       HorizontalOptions="FillAndExpand"
                                       FontFamily="Avenir Next"
                                       FontAttributes="Bold"
                                       Text="{Binding AlarmText}"/>
                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </StackLayout>
    </ContentPage.Content>
    

        public partial class AlarmPage : ContentPage
    {
        private List<AlarmPageItem> listViewItems;
    
        private Image alarmIcon;
    
        public AlarmPage ()
        {
            listViewItems = new List<AlarmPageItem>();
            alarmIcon = new Image { Source = "warning.png" };
            PopulateList();
    
            InitializeComponent();
    
            listView.ItemsSource = listViewItems;
        }
    
        private void OnItemTapped(object sender, ItemTappedEventArgs e)
        {
            if (e == null) return; // has been set to null, do not 'process' tapped event
            Debug.WriteLine("Tapped: " + e.Item);
            ((ListView)sender).SelectedItem = null; // de-select the row
        }
    
        private void PopulateList()
        {
    
    
            listViewItems.Add(new AlarmPageItem() {AlarmImage = alarmIcon, AlarmText = "The front is too heavy", DateTime = DateTime.Now.ToShortTimeString()});
        }
    }
    

1 个答案:

答案 0 :(得分:3)

您绑定在XAML中的Image的“来源”以及您在代码中创建的Image对象。

通过将AlarmPageItem媒体资源设为AlarmImage或简单ImageSource来更新您的string课程。

类似的东西:

class AlarmPageItem
{
    public string AlarmImage { get; set; }

    public string AlarmText { get; set; }

    //Just a suggestion: Change this property name for something different as it 
    //can create some confusion.
    public DateTime DateTime { get; set; }

     ...

    // Add any other properties your class might have.
}

更新上述内容后,只需更改代码即可在PopulateList方法中设置图片名称

private void PopulateList()
{
    listViewItems.Add(new AlarmPageItem() {AlarmImage = "warning.png", AlarmText = "The front is too heavy", DateTime = DateTime.Now.ToShortTimeString()});
}

希望这会有所帮助.-