2017-09-14 84 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