2017-09-14 90 views
1

我是XAML的新手,每当我读到它时,通常都会使用某种容器作为根视图(StackLayout,ContentPage,ContentView,RelativeLayout等)。Xamarin.Forms XAML图像作为根元素

但是,我遇到了这个使用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但我需要添加FFImageLoading的xmlns命名空间,但我不能,因为命名空间的图片标签里面,不在外面。

回答

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(); 
     } 
    } 
} 
+1

@SharadaGururay非常感谢:)。有用 – pixel