1

我是 XAML 的新手,在任何我读到它的地方,通常都会使用某种容器作为根视图(StackLayout、ContentPage、ContentView、RelativeLayout 等)。

但是,我遇到了这个使用 Image 作为根的 XAML。我们为什么要这样做?

<?xml version="1.0" encoding="utf-8" ?>
<Image
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:r="clr-namespace:My.Resources;assembly=My.Resources"
    x:Class="My.Views.Buttons.MyView"
        Source="MyImage.png"/>

我想用 FFImageLoading.CachedImage 替换这个 Image 但为此我需要添加 FFImageLoading xmlns 命名空间,但我不能,因为命名空间在 Image 标签内,而不是在外面。

4

1 回答 1

1

您可以在根标记中指定命名空间,并在 XAML 中的根标记本身中使用它。

例如:

<ffimageloading:CachedImage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="SomeAppNameSpace.CustomCachedImage" 
             Source="http://loremflickr.com/600/600/nature?filename=simple.jpg">
</ffimageloading:CachedImage>

只需确保您从后面代码中的正确类派生:

namespace SomeAppNameSpace
{
    [XamlCompilation(XamlCompilationOptions.Compile)]
    public partial class CustomCachedImage : CachedImage
    {
        public CustomCachedImage()
        {
            InitializeComponent();
        }
    }
}
于 2017-09-14T20:59:43.427 回答