如何在跨平台应用程序中管理图像?

时间:2019-01-25 19:06:13

标签: c# image xamarin xamarin.forms xamarin.ios

我正在创建跨平台演示项目,但是在ios模拟器上尝试调试应用程序时遇到了问题。首先,我无法将Mac与Visual Studio配对,但是我只是将所有文件作为rar发送并在Mac上打开。 Android项目一切正常,但尝试在iphone模拟器应用上启动时会抛出

的异常
System Exception : Image: file"bgtest5.jpg" not found in app bundle. 

(Android版本在此Mac上运行正常)。根据我的问题,我将应用程序需要加载的所有图像放入ios解决方案的resources文件夹中,将它们标记为“ bundleResources”,我在调试器输出中看到我的observablecollection已被读取,然后引发图像异常...

MyModel类

    [PrimaryKey]
    public int PersonId { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public string Description { get; set; }
    public string PersonBackgroundImage { get; set; }
    public double ProgressCounter { get; set; }

ListViewModel类

            var personList = new List<PersonViewModel>
        {
             new PersonViewModel()
            {
                Name="Test", Surname="Test", Description= "TEsT", Background = "bgtest6.jpg", ProgressCounter =0.1, SavedClicked=0,Weight=1
            },
            new PersonViewModel()
            {
                Name="Test", Surname="Test", Description= "TEsT",Background = "bgtest6.jpg", ProgressCounter =0.1, SavedClicked=0,Weight=30
            },
            new PersonViewModel()
            {
                Name="Test", Surname="Test", Description= "TEsT",Background = "bgtest6.jpg", ProgressCounter =0.2, SavedClicked=0
            },
            new PersonViewModel()
            {
                Name="Test", Surname="Test", Description= "TEsT",Background = "bgtest6.jpg", ProgressCounter =0.3, SavedClicked=0,Weight=27
            },
        };
        Persons = new ObservableCollection<PersonViewModel>(personList);

PersonPage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="CommandDemo.Views.PersonPage"
         BackgroundImage="{Binding Person.Background}">
<ContentPage.Content>
    <StackLayout>
        <Label Text="{Binding Person.Description}"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               HorizontalTextAlignment="Center"
               VerticalTextAlignment="Center"
               FontSize="Medium"/>
        <Label Text="{Binding Person.ProgressCounter}"
               VerticalOptions="Center"
               HorizontalOptions="Center"
               HorizontalTextAlignment="Center"
               VerticalTextAlignment="Center"
               FontSize="Medium"/>
    </StackLayout>
</ContentPage.Content>

MyListPage

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="CommandDemo.Views.PersonListPage"
         BackgroundImage="bgtest5.jpg">
<NavigationPage.TitleView>
    <StackLayout Orientation="Horizontal">
        <Button Text="Numbers"
                Command="{Binding NavigateSumPageCommand}"
                CommandParameter="{Binding .}"/>
    </StackLayout>
</NavigationPage.TitleView>
<ContentPage.Content>
    <StackLayout Padding="10"
                 Margin="10">
        <ListView x:Name="personList"
                  ItemsSource="{Binding Persons}"
                  HasUnevenRows="True"
                  >
            <!--SelectedItem="{Binding SelectedPerson}"-->
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>

                        <StackLayout>
                            <StackLayout.GestureRecognizers>
                                <TapGestureRecognizer NumberOfTapsRequired="1"
                                                      Command="{Binding Source={x:Reference personList},Path=BindingContext.NavigateCommand}"
                                                      CommandParameter="{Binding .}"/>
                            </StackLayout.GestureRecognizers>
                            <Label Text="{Binding Name}"
                               HorizontalTextAlignment="Center"
                               VerticalTextAlignment="Center"
                               HorizontalOptions="Center"
                               VerticalOptions="Center"
                               Margin="5,5,5,5"/>
                            <ProgressBar Progress="{Binding ProgressCounter}"/>
                            <Button Text="Add Progress"
                                    Command="{Binding Source={x:Reference personList},Path=BindingContext.IncreaseProgressCommand}"
                                    CommandParameter="{Binding .}"/>
                        </StackLayout>

                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Label Text="{Binding SumCollected}"
               VerticalTextAlignment="Center"
               HorizontalTextAlignment="Center"
               VerticalOptions="Center"
               HorizontalOptions="Center"/>
        <Label Text="{Binding PieceCollected}"
               VerticalTextAlignment="Center"
               HorizontalTextAlignment="Center"
               VerticalOptions="Center"
               HorizontalOptions="Center"/>
        <Button Text="Numbers"
                Command="{Binding NavigateSumPageCommand}"
                CommandParameter="{Binding .}"/>
    </StackLayout>
</ContentPage.Content>

所有绑定均按预期在android项目上运行。由于加载应用程序时引发异常,因此无法加载ios项目。我是否缺少一些可在ios项目上使用的图像的价值转换器?对于这方面的任何帮助或指导如何实现我的目标,我将不胜感激! :)预先谢谢你

编辑:1 删除行BackgroundImage =“ bgtest5.jpg”项目后,该项目将加载到白色背景上,并且所有绑定的图像都将立即显示。哪里有问题?

0 个答案:

没有答案